Python 连接被拒绝的paramiko异常

Python 连接被拒绝的paramiko异常,python,exception,paramiko,Python,Exception,Paramiko,这是我的密码。它工作得很好。 如果我给出了错误的用户ID和密码——这完全是一种期待 但当主机名无效时,它会抛出“连接被拒绝”的错误,并且不确定必须引发什么类型的异常。请给我一些 错误 import paramiko try: ssh =paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(hostname='10.10.1.0',username

这是我的密码。它工作得很好。 如果我给出了错误的用户ID和密码——这完全是一种期待

但当主机名无效时,它会抛出“连接被拒绝”的错误,并且不确定必须引发什么类型的异常。请给我一些

错误

import paramiko
try:
    ssh =paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect(hostname='10.10.1.0',username='test',password='test1234')
    print 'Successfully connected'
except paramiko.AuthenticationException:
    print "Authentication failed, please verify your credentials"
except paramiko.SSHException as sshException:
    print "Unable to establish SSH connection: %s" % sshException
回溯(最近一次呼叫最后一次):
文件“”,第4行,在
文件“/usr/lib/python2.6/site packages/paramiko/client.py”,第290行,在connect中
sock.connect(地址)
文件“”,第1行,在connect中
socket.error:[Errno 111]连接被拒绝

socket.error
的拼写如下:

Traceback (most recent call last):
  File "<stdin>", line 4, in <module>
  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 111] Connection refused
然后:

from socket import error as socket_error
错误消息告诉您有关错误号(
socket\u err.errno
),因此您可以检查,例如,与
errno.econnreference
进行比较

try:
    # as you were
except socket_error as socket_err:
    # do something
import errno

try:
    # Do something
except socket.error, v:
    e_code = v[0]
    if e_code == errno.ECONNREFUSED:
        print "Connection Refused"