【发布时间】:2018-03-20 09:52:26
【问题描述】:
我有一个代码,其中我循环通过主机列表并将连接附加到连接列表,如果有连接错误,我想跳过它并继续主机列表中的下一个主机。
这是我现在拥有的:
def do_connect(self):
"""Connect to all hosts in the hosts list"""
for host in self.hosts:
try:
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(host['ip'], port=int(host['port']), username=host['user'], timeout=2)
except:
pass
#client.connect(host['ip'], port=int(host['port']), username=host['user'], password=host['passwd'])
finally:
if paramiko.SSHException():
pass
else:
self.connections.append(client)
这不能正常工作,如果连接失败,它只会一次又一次地循环同一个主机,直到它建立连接,我该如何解决这个问题?
【问题讨论】:
-
使用
continue。 -
我不明白为什么它会永远循环同一个主机?
-
我没有看到任何再次循环同一主机的代码
-
但确实如此,假设我在主机列表中有 host1、host2,如果无法建立与 host1 的连接,它会由于某种原因不断尝试一次又一次地循环同一个 host1。
标签: python loops exception-handling