Catch"无法打开与网关的连接"在sshtunnel.py中

时间:2015-12-03 17:30:26

标签: python ssh paramiko

使用sshtunnel.py时不时地设置隧道失败,因为网关(ssh_host)在我第一次连接时抱怨。在放弃之前,我想给它一些重试:

for attempt in range(5):
    try:
        forwarder.start()
    except Exception, e:
        print 'Error (trying again in five seconds):\n' + format(e.message))
        time.sleep(5)
    else:
        break
else:
    print 'Failed to setup a connection to the gateway'
    sys.exit(1)

但是,未检测到错误'。我看了一下sshtunnel.py代码,发现following代码捕获了相关的Paramiko异常:

except paramiko.ssh_exception.AuthenticationException:
    self.logger.error('Could not open connection to gateway')
    return

如何在我的try:

中发现这一点

1 个答案:

答案 0 :(得分:0)

SSHTunnel.py项目成员advised我将forwarder._check_is_started()添加到我的代码中:

for attempt in range(5):
    try:
        forwarder.start()
        forwarder._check_is_started()
    except BaseSSHTunnelForwarderError as e:
        print 'Error (trying again in five seconds):\n' + format(e.message))
        time.sleep(5)
    else:
        break
else:
    print 'Failed to setup a connection to the gateway'
    sys.exit(1)