我的代码:
import pysftp s = pysftp.Connection(host='test.rebex.net', username='demo', password='password') data = s.listdir() s.close() for i in data: print i
我尝试使用pysftp连接到SFTP服务器时出错.
这应该是直截了当的,但我得到以下错误:
Traceback (most recent call last):
File "/Users/gavinhinfey/Documents/Python Files/sftp_test.py", line 3, in
s = pysftp.Connection(host='test.rebex.net', username='demo', password='password')
File "build/bdist.macosx-10.6-intel/egg/pysftp.py", line 55, in __init__
File "build/bdist.macosx-10.5-intel/egg/paramiko/transport.py", line 303, in __init__
paramiko.SSHException: Unable to connect to test.rebex.net: [Errno 60] Operation timed out
Exception AttributeError: "'Connection' object has no attribute '_tranport_live'" in > ignored
我尝试使用不同版本的python(主要是2.7),我已经安装了所有依赖项,并尝试了大量的sftp连接.我正在使用OS X 10.9.1.
该初始错误似乎是连接远程服务器的问题(SSHException).第二个(AttributeError)来自连接失败时发生的代码中的错误.它已在最新版本的pysftp中修复
https://pypi.python.org/pypi/pysftp
pip install -U pysftp
是你的朋友.
更新包对我来说不起作用,因为它已经是最新的(至少是python 2.7的最新版本)
在这里找到了更好的方法.
1)您可以手动将ssh密钥添加到known_hosts文件中
ssh test.rebex.net
2)或者你可以设置一个标志来忽略它
import pysftp cnopts = pysftp.CnOpts() cnopts.hostkeys = None # disable host key checking. with pysftp.Connection('host', username='me',private_key=private_key, private_key_pass=private_key_password, cnopts=cnopts) as sftp # do stuff here