Python Pysftp Error

和自甴很熟 提交于 2019-11-29 14:29:41

问题


My code:

import pysftp 
s = pysftp.Connection(host='test.rebex.net', username='demo', password='password') 
data = s.listdir() 
s.close() 
for i in data: 
    print i

I'm getting an error trying to connect to a SFTP server using pysftp.

This should be straight forward enough but I get the error below:

Traceback (most recent call last):
  File "/Users/gavinhinfey/Documents/Python Files/sftp_test.py", line 3, in <module>
    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 <bound     method Connection.__del__ of <pysftp.Connection object at 0x101a5a810>> ignored

I've tried using different versions of python (mostly 2.7), I have all dependencies installed and I tried numerous sftp connections. I'm using OS X 10.9.1.


回答1:


That initial error appears to be a problem connecting with the remote server (SSHException). The second (AttributeError), is from a bug in the code that occurs when the connection fails. It is fixed in the latest version of pysftp

https://pypi.python.org/pypi/pysftp

pip install -U pysftp

is your friend.




回答2:


updating the package didn't work for me, as it was already up-to-date (latest for python 2.7 at least)

Found a better aproach here.

1) You can manualy add the ssh key to the known_hosts file

ssh test.rebex.net

2) Or you can set a flag to ignore it

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


来源:https://stackoverflow.com/questions/21092850/python-pysftp-error

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!