How to use nntplib module? - python

When I use first example in Python's nntplib module documentation, there are some errors.
>>> from nntplib import NNTP
>>> s = NNTP('news.gmane.org')
>>> resp, count, first, last, name = s.group('gmane.comp.python.committers')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\nntplib.py", line 354, in group
resp = self.shortcmd('GROUP ' + name)
File "C:\Python27\lib\nntplib.py", line 268, in shortcmd
return self.getresp()
File "C:\Python27\lib\nntplib.py", line 223, in getresp
resp = self.getline()
File "C:\Python27\lib\nntplib.py", line 215, in getline
if not line: raise EOFError
EOFError
Why does this happen?

Try s = NNTP('news.gmane.org', readermode=True)
As mentioned in the docs:
If the optional flag readermode is true, then a mode reader command is
sent before authentication is performed. Reader mode is sometimes
necessary if you are connecting to an NNTP server on the local machine
and intend to call reader-specific commands, such as group. If you get
unexpected NNTPPermanentErrors, you might need to set readermode.

Related

Getting "TypeError: 'NoneType' object is not iterable" while doing parallel ssh

I am trying to do parallel ssh on servers. While doing this, i am getting "TypeError: 'NoneType' object is not iterable" this error. Kindly help.
My script is below
from pssh import ParallelSSHClient
from pssh.exceptions import AuthenticationException, UnknownHostException, ConnectionErrorException
def parallelsshjob():
client = ParallelSSHClient(['10.84.226.72','10.84.226.74'], user = 'root', password = 'XXX')
try:
output = client.run_command('racadm getsvctag', sudo=True)
print output
except (AuthenticationException, UnknownHostException, ConnectionErrorException):
pass
#print output
if __name__ == '__main__':
parallelsshjob()
And the Traceback is below
Traceback (most recent call last):
File "parallelssh.py", line 17, in <module>
parallelsshjob()
File "parallelssh.py", line 10, in parallelsshjob
output = client.run_command('racadm getsvctag', sudo=True)
File "/Library/Python/2.7/site-packages/pssh/pssh_client.py", line 520, in run_command
raise ex
TypeError: 'NoneType' object is not iterable
Help me with the solution and also suggest me to use ssh-agent in this same script. Thanks in advance.
From reading the code and debugging a bit on my laptop, I believe the issue is that you don't have a file called ~/.ssh/config. It seems that parallel-ssh has a dependency on OpenSSH configuration, and this is the error you get when that file is missing.
read_openssh_config returns None here: https://github.com/pkittenis/parallel-ssh/blob/master/pssh/utils.py#L79
In turn, SSHClient.__init__ blows up when trying to unpack the values it expects to receive: https://github.com/pkittenis/parallel-ssh/blob/master/pssh/ssh_client.py#L97.
The fix is presumably to get some sort of OpenSSH config file in place, but I'm sorry to say I know nothing about that.
EDIT
After cleaning up some of parallel-ssh's exception handling, here's a better stack trace for the error:
Traceback (most recent call last):
File "test.py", line 11, in <module>
parallelsshjob()
File "test.py", line 7, in parallelsshjob
output = client.run_command('racadm getsvctag', sudo=True)
File "/Users/smarx/test/pssh/venv/lib/python2.7/site-packages/pssh/pssh_client.py", line 517, in run_command
self.get_output(cmd, output)
File "/Users/smarx/test/pssh/venv/lib/python2.7/site-packages/pssh/pssh_client.py", line 601, in get_output
(channel, host, stdout, stderr, stdin) = cmd.get()
File "/Users/smarx/test/pssh/venv/lib/python2.7/site-packages/gevent/greenlet.py", line 480, in get
self._raise_exception()
File "/Users/smarx/test/pssh/venv/lib/python2.7/site-packages/gevent/greenlet.py", line 171, in _raise_exception
reraise(*self.exc_info)
File "/Users/smarx/test/pssh/venv/lib/python2.7/site-packages/gevent/greenlet.py", line 534, in run
result = self._run(*self.args, **self.kwargs)
File "/Users/smarx/test/pssh/venv/lib/python2.7/site-packages/pssh/pssh_client.py", line 559, in _exec_command
channel_timeout=self.channel_timeout)
File "/Users/smarx/test/pssh/venv/lib/python2.7/site-packages/pssh/ssh_client.py", line 98, in __init__
host, config_file=_openssh_config_file)
TypeError: 'NoneType' object is not iterable
This was seemingly a regression in the 0.92.0 version of the library which is now resolved in 0.92.1. Previous versions also work. OpenSSH config should not be a dependency.
To answer your SSH agent question, if there is one running and enabled in the user session it gets used automatically. If you would prefer to provide a private key programmatically can do the following
from pssh import ParallelSSHClient
from pssh.utils import load_private_key
pkey = load_private_key('my_private_key')
client = ParallelSSHClient(hosts, pkey=pkey)
Can also provide an agent with multiple keys programmatically, per below
from pssh import ParallelSSHClient
from pssh.utils import load_private_key
from pssh.agent import SSHAgent
pkey = load_private_key('my_private_key')
agent = SSHAgent()
agent.add_key(pkey)
client = ParallelSSHClient(hosts, agent=agent)
See documentation for more examples.

kafka-python raise kafka.errors.ConsumerFetchSizeTooSmall

I'm writing a simple code in python 2.7 that is consuming messaging from an apache kafka topic. The code is the following:
from kafka import SimpleConsumer,KafkaClient
group = "my_group_test"
client = KafkaClient('localhost:9092')
cons = SimpleConsumer(client, group, "my_topic")
messages = cons.get_messages(count=1000,block=False)
But is raising this exception:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/kafka/consumer/simple.py", line 285, in get_messages
update_offset=False)
File "/usr/local/lib/python2.7/dist-packages/kafka/consumer/simple.py", line 320, in _get_message
self._fetch()
File "/usr/local/lib/python2.7/dist-packages/kafka/consumer/simple.py", line 425, in _fetch
raise ConsumerFetchSizeTooSmall()
kafka.errors.ConsumerFetchSizeTooSmall
How could i modify this parameter (ConsumerFetchSize) in order to make this code work?
I found a solution,
using the parameter max_buffer_size in the SimpleConsumer.
The working code is:
#the size is adherent with my need but is totally arbitrary
cons = SimpleConsumer(client, group, "my_topic",max_buffer_size=9932768)

Unable to ping managed nodes using ansible-2.0

I downloaded the ansible-2.0.0-0.2.alpha2.tar.gz and installed it on my control machine. However now I'm not able to ping any of my machines. Previously using v1.9.2 i could communicate with them. Now it gives the following error:
Unexpected Exception: lstat() argument 1 must be encoded string without NULL bytes, not str
the full traceback was:
Traceback (most recent call last):
File "/usr/bin/ansible", line 79, in
sys.exit(cli.run())
File "/usr/lib/python2.6/site-packages/ansible/cli/adhoc.py", line 111, in run
inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list=self.options.inventory)
File "/usr/lib/python2.6/site-packages/ansible/inventory/init.py", line 77, in init
self.parse_inventory(host_list)
File "/usr/lib/python2.6/site-packages/ansible/inventory/init.py", line 133, in parse_inventory
host.vars = combine_vars(host.vars, self.get_host_variables(host.name))
File "/usr/lib/python2.6/site-packages/ansible/inventory/init.py", line 499, in get_host_variables
self.vars_per_host[hostname] = self.get_host_variables(hostname, vault_password=vault_password)
File "/usr/lib/python2.6/site-packages/ansible/inventory/__init.py", line 529, in get_host_variables
vars = combine_vars(vars, self.get_host_vars(host))
File "/usr/lib/python2.6/site-packages/ansible/inventory/__init_.py", line 653, in get_host_vars
return self.get_hostgroup_vars(host=host, group=None, new_pb_basedir=new_pb_basedir)
File "/usr/lib/python2.6/site-packages/ansible/inventory/__init_.py", line 702, in _get_hostgroup_vars
base_path = os.path.realpath(os.path.join(basedir, "host_vars/%s" % host.name))
File "/usr/lib64/python2.6/posixpath.py", line 365, in realpath
if islink(component):
File "/usr/lib64/python2.6/posixpath.py", line 132, in islink
st = os.lstat(path)
TypeError: lstat() argument 1 must be encoded string without NULL bytes, not str
Any help would be appreciated.
This is a known bug due to some Unicode changes made to the playbook parser in 2.0. Several versions of Python shipped with a version of shlex.split() that fails horribly on Unicode input- you likely have one of them installed. The bug has been worked around and will be included in the next drop. See https://github.com/ansible/ansible/issues/12257

Python lib execute error

I made this python lib and it had this function with uses urllib and urllib2 but when i execute the lib's functions from python shell i get this error
>>> from sabermanlib import geturl
>>> geturl("roblox.com","ggg.html")
Traceback (most recent call last):
File "<pyshell#11>", line 1, in <module>
geturl("roblox.com","ggg.html")
File "sabermanlib.py", line 21, in geturl
urllib.urlretrieve(Address,File)
File "C:\Users\Andres\Desktop\ddd\Portable Python 2.7.5.1\App\lib\urllib.py", line 94, in urlretrieve
return _urlopener.retrieve(url, filename, reporthook, data)
File "C:\Users\Andres\Desktop\ddd\Portable Python 2.7.5.1\App\lib\urllib.py", line 240, in retrieve
fp = self.open(url, data)
File "C:\Users\Andres\Desktop\ddd\Portable Python 2.7.5.1\App\lib\urllib.py", line 208, in open
return getattr(self, name)(url)
File "C:\Users\Andres\Desktop\ddd\Portable Python 2.7.5.1\App\lib\urllib.py", line 463, in open_file
return self.open_local_file(url)
File "C:\Users\Andres\Desktop\ddd\Portable Python 2.7.5.1\App\lib\urllib.py", line 477, in open_local_file
raise IOError(e.errno, e.strerror, e.filename)
IOError: [Errno 2] The system cannot find the file specified: 'roblox.com'
>>>
and here's the code for the lib i made:
import urllib
import urllib2
def geturl(Address,File):
urllib.urlretrieve(Address,File)
EDIT 2
I cant understand why i get this error in the python shell executing:
geturl(Address,File)
You don't want urllib.urlretrieve. This takes a file-like object. Instead, you want urllib.urlopen:
>>> help(urllib.urlopen)
urlopen(url, data=None, proxies=None)
Create a file-like object for the specified URL to read from.
Additionally, if you want to download and save a document, you'll need a more robust geturl function:
def geturl(Address, FileName):
html_data = urllib.urlopen(Address).read() # Open the URL
with open(FileName, 'wb') as f: # Open the file
f.write(html_data) # Write data from URL to file
geturl(u'http://roblox.com') # URL's must contain the full URI, including http://

Python Fabric run command returns "binascii.Error: Incorrect padding"

I'm trying to connect to connect to amazon EC2 via fabric using the script below. But I'm met with a problem that I'm not sure how to solve it.
import os
from fabric.api import run, env, local, cd
WORK = os.getenv('HOME') + '/Work/myproject/'
env.user = 'ubuntu'
env.hosts = [
'128.248.268.288'
]
env.key_filename = [
'%s/aws/myproject.pem' % WORK
]
def deploy():
print("Executing on %(host)s as %(user)s" % env)
with cd('/sites/myproject.com/code/'):
run('ls')
This is the traceback. I'm not sure how to solve the problem.
Traceback (most recent call last):
File "/Library/Python/2.6/site-packages/fabric/main.py", line 540, in main
commands[name](*args, **kwargs)
File "/Users/mickeyckm/Work/myproject/codes/giivee/fabfile.py", line 18, in deploy
run('ls')
File "/Library/Python/2.6/site-packages/fabric/network.py", line 391, in host_prompting_wrapper
return func(*args, **kwargs)
File "/Library/Python/2.6/site-packages/fabric/operations.py", line 422, in run
channel = connections[env.host_string]._transport.open_session()
File "/Library/Python/2.6/site-packages/fabric/network.py", line 65, in __getitem__
self[real_key] = connect(user, host, port)
File "/Library/Python/2.6/site-packages/fabric/network.py", line 140, in connect
client.load_system_host_keys()
File "/Library/Python/2.6/site-packages/paramiko/client.py", line 151, in load_system_host_keys
self._system_host_keys.load(filename)
File "/Library/Python/2.6/site-packages/paramiko/hostkeys.py", line 155, in load
e = HostKeyEntry.from_line(line)
File "/Library/Python/2.6/site-packages/paramiko/hostkeys.py", line 67, in from_line
key = RSAKey(data=base64.decodestring(key))
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/base64.py", line 321, in decodestring
return binascii.a2b_base64(s)
binascii.Error: Incorrect padding
Any help/hint would be great appreciated.
Cheers,
Mickey
I saw some places where Incorrect Padding error was resulted from binascii module and it was mostly when the string you pass has some extraneous white-space characters.
>>> import binascii
>>> binascii.a2b_base64('a')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
binascii.Error: Incorrect padding
In your case, it the various properties you set for your env object. Do something like this for your key file location and see if that works.
filelocation = os.path.join(WORK,'aws/myproject.pem')
env.key_filename = [filelocation]
Look at your ~/.ssh/known_hosts file. It may contain lines with duplicate entries or be corrupted in some other way.
I had a similar problem and tracked it down to some corruption in my .ssh/known_hosts file.
I thus added to my .bashrc
alias deploy='mv ~/.ssh/known_hosts ~/.ssh/known_hosts.tmp; fab <myfabscript>; mv ~/.ssh/known_hosts.old ~/.ssh/known_hosts'
(obviously putting the right fabric script where <myfabscript> is) and now all works fine when I simply run "deploy"!

Categories