Pysftp:操作定时ou

2024-06-01 06:37:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用以下脚本连接到服务器:

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或类似的客户端进行访问,它仍然可以工作;尽管它显示以下关于主机密钥算法和指纹的警告:

enter image description here

但是,脚本运行不正常,并报告以下错误:

SSHException: Unable to connect to sftp://my_ip:my_port: [Errno 60] Operation timed out

即使我只尝试使用host=my\u ip。另外,如果我没有设置cnopts,它就找不到主机密钥。你知道吗

我该怎么解决?你知道吗


Tags: toimportip服务器脚本hostdataport
1条回答
网友
1楼 · 发布于 2024-06-01 06:37:00

^{} constructorhost参数是:

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

相关问题 更多 >