我有一個代碼,其中im循環通過主機列表和附加連接到連接列表,如果有連接錯誤,我想跳過它並繼續與主機列表中的下一個主機。異常後如何繼續循環?
繼承人什麼,我現在有:
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'](https://docs.python.org/3/reference/simple_stmts.html#continue)。 – Mark
我不明白爲什麼它會永遠循環同一個主機? – Sraw
我沒有看到任何代碼再次循環相同的主機 – Sanket