2

I want to return an error code when the following error gets raised:

Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
 File "UserManagementRemote.py", line 202, in create_group
  ssh.connect(hostname, username=user, password=remotepass)
 File "/usr/lib/python2.6/site-packages/paramiko/client.py", line 290, in connect
   sock.connect(addr)
 File "<string>", line 1, in connect
socket.error: [Errno 113] No route to host
>>>

But I'm currently having trouble catching the error raised.

try:
   ssh = paramiko.SSHClient()
   ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
   ssh.connect(hostname, username=user, password=remotepass)
except paramiko.AuthenticationException:
   return 259
except socket.error:
   return 261
chan = ssh.get_transport().open_session()
chan.exec_command(command)
codest = chan.recv_exit_status()
ssh.close()
return codest

Resulting on this:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "UserManagementRemote.py", line 207, in create_group
  except socket.error:
NameError: global name 'socket' is not defined
>>>

Any ideas?

2
  • 1
    Have you imported the socket module? Commented Dec 13, 2011 at 14:57
  • I thought I didn't need to import any additional modules because when there's no exception the code functions as expected. Commented Dec 13, 2011 at 15:47

1 Answer 1

7

Do

import socket

in the module where you do the exception handling.

To prevent this problem in the future, run pyflakes on all your source files. That will catch a lot of other errors as well.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, because it never complained until I did hit the exception I thought I had everything I needed already imported.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.