简体   繁体   English

拒绝连接的paramiko例外

[英]paramiko exception for connection refused

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

This is my code. 这是我的代码。 It was working fine. 运行正常。 If i give wrong userid and password - Perfectly throwing expection. 如果我输入了错误的用户名和密码-完全抛出期望。

But when hostname is not valid, it's throwing error as "Connection refused" and not sure what type of exception have to raise. 但是,当主机名无效时,它将引发错误,如“连接被拒绝”,并且不确定必须引发哪种类型的异常。 Could some please help. 可以请一些帮助。

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

socket.error is spelled socket_error like this: socket.error拼写socket_error是这样的:

from socket import error as socket_error

and then: 接着:

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

The error message is telling you about error numbers ( socket_err.errno ) so you can check that eg compare to errno.ECONNREFUSED . 错误消息告诉您错误号( socket_err.errno ),因此您可以检查与errno.ECONNREFUSED比较。

Use this 用这个

import errno

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM