小编Nan*_*oni的帖子

异常后如何继续循环?

我有一个代码,其中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)
Run Code Online (Sandbox Code Playgroud)

这无法正常工作,如果连接失败,它将一次又一次地循环同一主机,直到建立连接为止,我该如何解决?

python loops exception-handling

5
推荐指数
1
解决办法
1万
查看次数

标签 统计

exception-handling ×1

loops ×1

python ×1