dan*_*dan 2 python error-handling exception paramiko
我想在引发以下错误时返回错误代码:
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
>>>
Run Code Online (Sandbox Code Playgroud)
但我目前无法捕捉到引发的错误.
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
Run Code Online (Sandbox Code Playgroud)
结果如下:
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
>>>
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
做
import socket
Run Code Online (Sandbox Code Playgroud)
在您执行异常处理的模块中.
为了防止将来出现此问题,请在所有源文件上运行pyflakes.这也会遇到很多其他错误.
| 归档时间: |
|
| 查看次数: |
12351 次 |
| 最近记录: |