我正在尝试使用以下脚本连接到服务器:
import pysftp
cnopts = pysftp.CnOpts()
cnopts.hostkeys = None
srv = pysftp.Connection(host="sftp://my_ip:my_port",
username="alessandro", password="alessandro", port=2222,
cnopts=cnopts)
data = srv.listdir()
srv.close()
for i in data:
print (i)
如果我尝试使用相同的凭据通过 FileZilla 或类似的客户端进行访问,它会起作用;即使它显示以下警告,涉及主机 key 算法和指纹:
但是,该脚本无法正常运行并报告以下错误:
SSHException: Unable to connect to sftp://my_ip:my_port: [Errno 60] Operation timed out
即使我仅尝试使用host = my_ip。另外,如果我不设置 cnopts,它就找不到主机 key 。
如何解决?
最佳答案
Connection
constructor 的 host
参数是:
The Hostname or IP of the remote machine.
不是URL。
所以应该是:
srv = pysftp.Connection(host="my_ip",
username="alessandro", password="alessandro", port=2222,
cnopts=cnopts)
<小时/>
此外,请勿设置cnopts.hostkeys = None
,除非您不关心安全性。
正确的解决方案请参见Verify host key with pysftp 。
关于python - Pysftp:操作超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58053075/