【问题标题】:EOF occurred in violation of protocol with Python ftplibEOF 发生违反 Python ftplib 协议
【发布时间】:2016-03-11 20:54:17
【问题描述】:

我正在使用 Python ftplib 开发一个隐式 TLS 连接程序。我尝试了问题python-ftp-implicit-tls-connection-issue(包括Rg Glpj 和Juan Moreno 的答案)提供的解决方案来建立连接。但是当我像这样登录ftp服务器后调用retrlineretrbinary时(FTP_ITLSFTP_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_TLSv1、PROTOCOL_TLSv1_1 和 PROTOCOL_TLSv1_2,但它们都不起作用。而且我还尝试覆盖ntransfercmdauth,或设置ctx = ssl._create_stdlib_context(ssl.PROTOCOL_TLSv1),正如Steffen Ullrich 在connect-to-ftp-tls-1-2-server-with-ftplib 中所说的那样,但错误从未消失。那我能做什么呢?谢谢。

【问题讨论】:

    标签: python python-2.7 ftp ftplib


    【解决方案1】:

    我在尝试连接到 FileZilla FTP 服务器时遇到了这个问题。 FileZilla 在“FTP over TLS 设置” 中有一个设置,名为“使用 PROT P 时需要在数据连接上恢复 TLS 会话”。禁用此选项可解决此问题。

    如果您无法控制服务器,请查看FTPS with Python ftplib - Session reuse required,其中介绍了如何启用会话重用。不过,这似乎需要 Python 3.6+。

    【讨论】:

    • 这个答案指向了第二段中的正确解决方案。 – 不要遵循第一个建议。 “会话恢复”要求是一项安全功能。您不想将其关闭。
    猜你喜欢
    • 2016-01-29
    • 2017-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-09
    • 2013-05-20
    • 1970-01-01
    相关资源
    最近更新 更多