how to handle socket errors as exceptions in python + paramiko?
13,913
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.
Author by
dan
Updated on June 13, 2022Comments
-
dan almost 2 years
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?
-
dan over 12 yearsThanks, because it never complained until I did hit the exception I thought I had everything I needed already imported.
-
Fred Foo over 12 years@DanielSanabria: if this solved the problem, then please click the checkmark next to the answer.