最新的SFTP python包和最佳实践

2024-04-27 22:57:37 发布

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

我一直在寻找SFTP python包,ftpretty对我来说很好: https://pypi.org/project/ftpretty/ 但我想使用更安全的协议

PYSTFP显然有点过时(编辑:PYSTFP似乎仍在频繁使用,有关错误,请参见下文): 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", line 132, in __init__
    raise SSHException("EOF during negotiation")
paramiko.ssh_exception.SSHException: EOF during negotiation

Process finished with exit code 1

到目前为止,我已经了解到,我可能需要一个密钥文件,我可以首先使用filezilla连接到我的Web空间来找到它: 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

编辑:不提供主机密钥,如下所示:

import pysftp
cnopts = pysftp.CnOpts()
cnopts.hostkeys = None

myHostname = "host"
myUsername = "user"
myPassword = "pass"


with pysftp.Connection(host=myHostname, username=myUsername, password=myPassword, cnopts=cnopts, port=22) as sftp:
    print("Connection succesfully stablished ... ")
    sftp.put('C:\TEMP\Capture.PNG', preserve_mtime=True)

由于未提供主机密钥,仍然向我抛出一个错误:

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()

Tags: inpyselfvenvlibpackageslinesite
1条回答
网友
1楼 · 发布于 2024-04-27 22:57:37

对于第一个错误,它似乎是pysftp中的一个bug

您可以查看第76行的Connectionhere,第134行定义了属性_sftp_live,因此这肯定是运行时发生的错误,没有正确验证。我也能找到这个related error,这很可能解释了这个问题的原因;如果要显式修复,则错误中还会提到解决方案

我仍然会考虑使用^ {CD4}}。它确实使用了TLS作为安全性和非常安全的包装,您只需通过将secure参数设置为True(secure=True)即可启用它,默认情况下,该参数设置为False

相关问题 更多 >