EOF occurred in violation of protocol with Python ftplib
Asked Answered
K

1

12

I'm working on an implicit TLS connection program with Python ftplib. I tried the solution provided in question python-ftp-implicit-tls-connection-issue(including Rg Glpj's and Juan Moreno's answers) to make the connection. But when I call retrline or retrbinary after logging into the ftp server like this(FTP_ITLS is the subclass of 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()

I got an EOF error:

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)

As it seems ftplib uses PROTOCOL_SSLv23 as the default protocol in Python 2.7, I tried PROTOCOL_TLSv1, PROTOCOL_TLSv1_1 and PROTOCOL_TLSv1_2, but none of them worked. And I also tried overriding ntransfercmd and auth, or setting ctx = ssl._create_stdlib_context(ssl.PROTOCOL_TLSv1) as Steffen Ullrich said in question connect-to-ftp-tls-1-2-server-with-ftplib, but the error never disappeared. What can I do then? Thanks.

Koy answered 7/12, 2015 at 9:40 Comment(0)
S
7

I ran into this trying to connect to a FileZilla FTP server. FileZilla has a setting in the "FTP over TLS settings" called "Require TLS session resumption on data connection when using PROT P". Disabling this option fixed this problem.

If you don't have control over the server, check out FTPS with Python ftplib - Session reuse required which goes over how to enable session reuse. This seems to require Python 3.6+, however.

Spake answered 25/8, 2017 at 18:38 Comment(1)
This answer points to the correct solution in the second paragraph. – Do not follow the first advice. The "session resumption" requirement is a security feature. You do not want to turn it off.Evenfall

© 2022 - 2024 — McMap. All rights reserved.