如何解决最新的 SFTP python 包和最佳实践
我一直在寻找 SFTP python 包,ftpretty 对我来说很好用: https://pypi.org/project/ftpretty/ 但我想使用更安全的协议。
PySftp 显然有点过时了(编辑:看来 pysftp 仍然经常使用,关于错误请见下文): https://bitbucket.org/dundeemt/pysftp/src/master/
并在 Win10、PyCharm、Python3.6 上抛出了几个错误:
C:\Users\bobin\PycharmProjects\classtest\venv\lib\site-packages\pysftp\__init__.py:61: UserWarning: Failed to load HostKeys from C:\Users\bobin\.ssh\known_hosts. You will need to explicitly load HostKeys (cnopts.hostkeys.load(filename)) or disableHostKey checking (cnopts.hostkeys = None).
warnings.warn(wmsg,UserWarning)
Traceback (most recent call last):
File "C:/Users/bobin/PycharmProjects/classtest/pysftptest.py",line 7,in <module>
with pysftp.Connection(host=myHostname,username=myUsername,password=myPassword) as sftp:
File "C:\Users\bobin\PycharmProjects\classtest\venv\lib\site-packages\pysftp\__init__.py",line 132,in __init__
self._tconnect['hostkey'] = self._cnopts.get_hostkey(host)
File "C:\Users\bobin\PycharmProjects\classtest\venv\lib\site-packages\pysftp\__init__.py",line 71,in get_hostkey
raise SSHException("No hostkey for host %s found." % host)
paramiko.ssh_exception.SSHException: No hostkey for host s233.goserver.host found.
Exception ignored in: <bound method Connection.__del__ of <pysftp.Connection object at 0x00000235B0695048>>
Traceback (most recent call last):
File "C:\Users\bobin\PycharmProjects\classtest\venv\lib\site-packages\pysftp\__init__.py",line 1013,in __del__
self.close()
File "C:\Users\bobin\PycharmProjects\classtest\venv\lib\site-packages\pysftp\__init__.py",line 784,in close
if self._sftp_live:
AttributeError: 'Connection' object has no attribute '_sftp_live'
Process finished with exit code 1
这个线程似乎与我有关,但它已经 12 岁了: SFTP in Python? (platform independent) 而且 paramiko 包也给我带来了错误:
Traceback (most recent call last):
File "C:\Users\bobin\PycharmProjects\classtest\venv\lib\site-packages\paramiko\sftp_client.py",line 130,in __init__
server_version = self._send_version()
File "C:\Users\bobin\PycharmProjects\classtest\venv\lib\site-packages\paramiko\sftp.py",line 134,in _send_version
t,data = self._read_packet()
File "C:\Users\bobin\PycharmProjects\classtest\venv\lib\site-packages\paramiko\sftp.py",line 201,in _read_packet
x = self._read_all(4)
File "C:\Users\bobin\PycharmProjects\classtest\venv\lib\site-packages\paramiko\sftp.py",line 188,in _read_all
raise EOFError()
EOFError
During handling of the above exception,another exception occurred:
Traceback (most recent call last):
File "C:/Users/bobin/PycharmProjects/classtest/paramikotest.py",line 12,in <module>
sftp = paramiko.SFTPClient.from_transport(transport)
File "C:\Users\bobin\PycharmProjects\classtest\venv\lib\site-packages\paramiko\sftp_client.py",line 170,in from_transport
return cls(chan)
File "C:\Users\bobin\PycharmProjects\classtest\venv\lib\site-packages\paramiko\sftp_client.py",in __init__
raise SSHException("EOF during negotiation")
paramiko.ssh_exception.SSHException: EOF during negotiation
Process finished with exit code 1
到目前为止我明白我可能需要一个密钥文件,我可以通过首先使用例如连接到我的网络空间来找到它。文件目录: How To Extract SFTP SSH Key From Key Cache in FileZilla FTP Client
我的问题是:如何与我的主机 webgo 建立 SFTP 连接:https://www.webgo.de/hilfe/content/76/52/de/was-ist-sftp.html
编辑:不提供 host_key 如下:
import pysftp
cnopts = pysftp.CnOpts()
cnopts.hostkeys = None
myHostname = "host"
myUsername = "user"
myPassword = "pass"
with pysftp.Connection(host=myHostname,password=myPassword,cnopts=cnopts,port=22) as sftp:
print("Connection succesfully stablished ... ")
sftp.put('C:\TEMP\Capture.PNG',preserve_mtime=True)
仍然因为没有提供 host_keys 给我一个错误:
C:\Users\bobin\PycharmProjects\classtest\venv\lib\site-packages\pysftp\__init__.py:61: UserWarning: Failed to load HostKeys from C:\Users\bobin\.ssh\known_hosts. You will need to explicitly load HostKeys (cnopts.hostkeys.load(filename)) or disableHostKey checking (cnopts.hostkeys = None).
warnings.warn(wmsg,UserWarning)
EDIT2:尝试过 Prettyftp,但我的提供商拒绝连接:
Traceback (most recent call last):
File "C:/Users/bobin/PycharmProjects/classtest/testftp.py",line 15,in <module>
f.put('C:\TEMP\Capture.PNG','Capture230.PNG')
File "C:\Users\bobin\PycharmProjects\classtest\venv\lib\site-packages\ftpretty.py",line 119,in put
self.conn.storbinary('STOR %s' % remote_file,local_file)
File "C:\Users\bobin\AppData\Local\Programs\Python\Python36\lib\ftplib.py",line 513,in storbinary
return self.voidresp()
File "C:\Users\bobin\AppData\Local\Programs\Python\Python36\lib\ftplib.py",line 249,in voidresp
resp = self.getresp()
File "C:\Users\bobin\AppData\Local\Programs\Python\Python36\lib\ftplib.py",line 242,in getresp
raise error_temp(resp)
ftplib.error_temp: 425 Unable to build data connection: Operation not permitted
使用以下代码片段,再次设置secure=False:
from ftpretty import ftpretty
# Minimal
f = ftpretty('host','user','pass',port=21,secure=True)
f.put('C:\TEMP\Capture.PNG','Capture230.PNG')
f.close()
解决方法
对于第一个错误,它似乎是 pysftp
中的一个错误。
你可以看看第76行的Connection
类here,属性_sftp_live
是在第134行定义的,所以这肯定是运行时发生的错误而不是正确验证。我还找到了这个 related error,这可能解释了这个问题的原因;如果您想明确修复它,错误中也会提到解决方案。
我仍会考虑使用 ftpretty
。它确实使用 TLS
来保证安全性和非常安全的包装器,您可以通过将 secure 参数设置为 True (secure=True
) 来启用它 - 默认情况下设置为 False
。>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。