我正在使用 Python ftplib 开发一个隐式 TLS 连接程序。我尝试了问题中提供的解决方案python-ftp-implicit-tls-connection-issue (包括 Rg Glpj 和 Juan Moreno 的答案)来建立联系。但是当我调用 retrline
或 retrbinary
像这样登录到 ftp 服务器后(FTP_ITLS
是 FTP_TLS
的子类):
58 server = FTP_ITLS()
59 server.connect(host="x.x.x.x", port=990)
60 server.login(user="user", passwd="******")
61 server.prot_p()
62
63 server.cwd("doc")
64 print(server.retrlines('LIST'))
65 # server.retrbinary('RETR contents.7z', open('contents.7z', 'wb').write)
66 server.quit()
我收到 EOF 错误:Traceback (most recent call last):
File "D:/Coding/test/itls.py", line 64, in <module>
print(server.retrlines('LIST'))
File "D:\Python\Python27\lib\ftplib.py", line 735, in retrlines
conn = self.transfercmd(cmd)
File "D:\Python\Python27\lib\ftplib.py", line 376, in transfercmd
return self.ntransfercmd(cmd, rest)[0]
File "D:\Python\Python27\lib\ftplib.py", line 713, in ntransfercmd
server_hostname=self.host)
File "D:\Python\Python27\lib\ssl.py", line 352, in wrap_socket
_context=self)
File "D:\Python\Python27\lib\ssl.py", line 579, in __init__
self.do_handshake()
File "D:\Python\Python27\lib\ssl.py", line 808, in do_handshake
self._sslobj.do_handshake()
ssl.SSLEOFError: EOF occurred in violation of protocol (_ssl.c:590)
似乎 ftplib 使用 PROTOCOL_SSLv23
作为 Python 2.7 中的默认协议(protocol),我尝试过PROTOCOL_TLSv1、PROTOCOL_TLSv1_1 和 PROTOCOL_TLSv1_2,但它们都不起作用。我还尝试了覆盖
ntransfercmd
和 auth
, 或设置 ctx = ssl._create_stdlib_context(ssl.PROTOCOL_TLSv1)
正如 Steffen Ullrich 所说的 connect-to-ftp-tls-1-2-server-with-ftplib ,但错误从未消失。那我能做什么?谢谢。
最佳答案
我在尝试连接到 FileZilla FTP 服务器时遇到了这个问题。 FileZilla 在“FTP over TLS 设置”中有一个设置,称为“使用 PROT P 时需要在数据连接上恢复 TLS session ”。禁用此选项可解决此问题。
如果您无法控制服务器,请查看 FTPS with Python ftplib - Session reuse required其中介绍了如何启用 session 重用。然而,这似乎需要 Python 3.6+。
关于python - EOF 发生违反 Python ftplib 协议(protocol),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46530801/