繁体   English   中英

使用 pysftp,如何为连接指定超时值?

[英]With pysftp, how can I specify a timeout value for the connection?

使用 pysftp,我看到了如何在您已经连接后为任何命令设置超时,但我没有看到如何为连接本身设置超时。 我觉得我在某处丢失了一些东西。 只是为了尝试一下,我尝试将timeout=3添加到Connection方法中并出现错误,并尝试使用cnopts.timeout=3并且什么也没做。 作为记录,我在 Windows 上使用 Python 3,如果这会影响任何事情的话。

如果有帮助,这里有一些简单的测试代码,您可以进行试验。 (按原样,连接在大约 30 秒后超时。)

import pysftp

print("\n"*25)
cnopts=pysftp.CnOpts()
# - I know this next line is insecure, it's just for this test program.
cnopts.hostkeys = None

print('Connecting...')
# - 8.8.8.8 is Google's public DNS server. It doesn't respond to sftp requests at all,
# - so it's a good test case for how long a connection timeout takes.
with pysftp.Connection('8.8.8.8', username='anonymous', password='password',
                    cnopts=cnopts) as SFTP:
    print("Wait, how did you get this far?")
print("Done.")

pysftp 似乎不允许设置连接超时。

您可以直接使用Paramikopysftp 只是 Paramiko 的包装器)。

SSHClient.connect方法有超时参数。

ssh = paramiko.SSHClient()
ssh.connect(host, username=username, password=password, timeout=timeout)
sftp = ssh.open_sftp()

你可以这样做:

Connection.timeout (35000)

暂无
暂无

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

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