1

I'm having problems with FTP_TLS (ftplib) in Python 2.7.3.

Summary of findings (all connection attempts performed over the internet):

  • FileZilla to home web server - works
  • FileZilla to GoDaddy shared hosting server - works
  • Python to home web server - works
  • Python to GoDaddy shared hosting server - fails (see stack trace below)

The following code shows how I reproduce the problem. When connecting to my home server, this code generates an identical log on my personal FTP as FileZilla. (Only when connecting to the GoDaddy site does it result in an EOF exception).

from ftplib import FTP_TLS
o = FTP_TLS(ftpServer,ftpUsername,ftpPassword,ftpPort)
o.voidcmd('SYST')
o.voidcmd('FEAT')
o.prot_p()
o.voidcmd('PWD')
o.retrbinary('MLSD', open('OUTTEST', 'wb').write)
>>> o.retrbinary('MLSD', open('OUTTEST', 'wb').write)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/ftplib.py", line 703, in retrbinary
    return self.voidresp()
  File "/usr/lib/python2.7/ftplib.py", line 225, in voidresp
    resp = self.getresp()
  File "/usr/lib/python2.7/ftplib.py", line 211, in getresp
    resp = self.getmultiline()
  File "/usr/lib/python2.7/ftplib.py", line 197, in getmultiline
    line = self.getline()
  File "/usr/lib/python2.7/ftplib.py", line 187, in getline
    if not line: raise EOFError

I read that the EOF error is given when the server closes the pipe. It's also similar to what happens if you change to prot_p and then try to issue a plain text command (although as far as I can tell, this isn't the case here).

I don't understand what's different between my code and FileZilla. The fact that all attempts are performed over the internet reassures me that it isn't related to firewalls. Furthermore, FileZilla works, so from a technical perspective the connection is possible, I'm just having a hard time achieving it with Python.

My code works with the GoDaddy FTP if I don't issue the prot_p switch.

Additional information:

  • GoDaddy doesn't provide technical FTP logs (only usage logs)
  • My code has been working flawlessly for over a year, this just started happening two months ago.
  • The GoDaddy FTP server identifies as "Pure-FTPd [privsep] [TLS]"

Typical FileZilla Server Log after running my code.

(000132)18/10/2016 15:44:19 - (not logged in) (IP_ADDRESS)> Connected on port 21, sending welcome message...
(000132)18/10/2016 15:44:19 - (not logged in) (IP_ADDRESS)> 220-FileZilla Server 0.9.57 beta
(000132)18/10/2016 15:44:19 - (not logged in) (IP_ADDRESS)> 220-written by Tim Kosse (Tim.Kosse@gmx.de)
(000132)18/10/2016 15:44:19 - (not logged in) (IP_ADDRESS)> 220 Please visit https://filezilla-project.org/
(000132)18/10/2016 15:44:19 - (not logged in) (IP_ADDRESS)> AUTH TLS
(000132)18/10/2016 15:44:19 - (not logged in) (IP_ADDRESS)> 234 Using authentication type TLS
(000132)18/10/2016 15:44:19 - (not logged in) (IP_ADDRESS)> SSL connection established
(000132)18/10/2016 15:44:19 - (not logged in) (IP_ADDRESS)> USER ********************
(000132)18/10/2016 15:44:19 - (not logged in) (IP_ADDRESS)> 331 Password required for USER_NAME
(000132)18/10/2016 15:44:19 - (not logged in) (IP_ADDRESS)> PASS ********************
(000132)18/10/2016 15:44:19 - USER_NAME (IP_ADDRESS)> 230 Logged on
(000131)18/10/2016 15:44:19 - USER_NAME (IP_ADDRESS)> disconnected.
(000132)18/10/2016 15:44:19 - USER_NAME (IP_ADDRESS)> SYST
(000132)18/10/2016 15:44:19 - USER_NAME (IP_ADDRESS)> 215 UNIX emulated by FileZilla
(000132)18/10/2016 15:44:19 - USER_NAME (IP_ADDRESS)> FEAT
(000132)18/10/2016 15:44:19 - USER_NAME (IP_ADDRESS)> 211-Features:
(000132)18/10/2016 15:44:19 - USER_NAME (IP_ADDRESS)>  MDTM
(000132)18/10/2016 15:44:19 - USER_NAME (IP_ADDRESS)>  REST STREAM
(000132)18/10/2016 15:44:19 - USER_NAME (IP_ADDRESS)>  SIZE
(000132)18/10/2016 15:44:19 - USER_NAME (IP_ADDRESS)>  MODE Z
(000132)18/10/2016 15:44:19 - USER_NAME (IP_ADDRESS)>  MLST type*;size*;modify*;
(000132)18/10/2016 15:44:19 - USER_NAME (IP_ADDRESS)>  MLSD
(000132)18/10/2016 15:44:19 - USER_NAME (IP_ADDRESS)>  AUTH SSL
(000132)18/10/2016 15:44:19 - USER_NAME (IP_ADDRESS)>  AUTH TLS
(000132)18/10/2016 15:44:19 - USER_NAME (IP_ADDRESS)>  PROT
(000132)18/10/2016 15:44:19 - USER_NAME (IP_ADDRESS)>  PBSZ
(000132)18/10/2016 15:44:19 - USER_NAME (IP_ADDRESS)>  UTF8
(000132)18/10/2016 15:44:19 - USER_NAME (IP_ADDRESS)>  CLNT
(000132)18/10/2016 15:44:19 - USER_NAME (IP_ADDRESS)>  MFMT
(000132)18/10/2016 15:44:19 - USER_NAME (IP_ADDRESS)>  EPSV
(000132)18/10/2016 15:44:19 - USER_NAME (IP_ADDRESS)>  EPRT
(000132)18/10/2016 15:44:19 - USER_NAME (IP_ADDRESS)> 211 End
(000132)18/10/2016 15:44:19 - USER_NAME (IP_ADDRESS)> PBSZ 0
(000132)18/10/2016 15:44:19 - USER_NAME (IP_ADDRESS)> 200 PBSZ=0
(000132)18/10/2016 15:44:19 - USER_NAME (IP_ADDRESS)> PROT P
(000132)18/10/2016 15:44:19 - USER_NAME (IP_ADDRESS)> 200 Protection level set to P
(000132)18/10/2016 15:44:19 - USER_NAME (IP_ADDRESS)> PWD
(000132)18/10/2016 15:44:19 - USER_NAME (IP_ADDRESS)> 257 "/" is current directory.
(000132)18/10/2016 15:44:22 - USER_NAME (IP_ADDRESS)> TYPE I
(000132)18/10/2016 15:44:22 - USER_NAME (IP_ADDRESS)> 200 Type set to I
(000132)18/10/2016 15:44:22 - USER_NAME (IP_ADDRESS)> PASV
(000132)18/10/2016 15:44:22 - USER_NAME (IP_ADDRESS)> 227 Entering Passive Mode (xxx,xxx,xxx,xxx,xxx,xxx)
(000132)18/10/2016 15:44:22 - USER_NAME (IP_ADDRESS)> MLSD
(000132)18/10/2016 15:44:22 - USER_NAME (IP_ADDRESS)> 150 Opening data channel for directory listing of "/"
(000132)18/10/2016 15:44:22 - USER_NAME (IP_ADDRESS)> SSL connection for data connection established
(000132)18/10/2016 15:44:22 - USER_NAME (IP_ADDRESS)> 226 Successfully transferred "/"
admin0389
  • 51
  • 5
  • I just noticed a different error is given on Windows >>> o.retrbinary('MLSD', open('OUTTEST', 'wb').write) Traceback (most recent call last): File "", line 1, in File "C:\Program Files\Python\lib\ftplib.py", line 449, in retrbinary conn.unwrap() File "C:\Program Files\Python\lib\ssl.py", line 788, in unwrap s = self._sslobj.shutdown() OSError: [Errno 0] Error That led me to here http://stackoverflow.com/questions/32913943/error-0-when-using-ftplib where user dtf comments that there's a compatibility issue with PureFTP. I'm looking into this now. – admin0389 Oct 18 '16 at 15:44
  • Ok, I'm pretty sure this relates to SSL session reuse. Unfortunately ftplib doesn't support this feature (and it really should) As a test I tried to conenct to my home FileZilla server and it works fine, but when I select "Require TLS session resumption on data connection when using PROT P" under "FTP over TLS settings" within the FileZilla server settings, I get teh same error as when trying to connect to the GoDaddy server. – admin0389 Oct 18 '16 at 16:46

2 Answers2

1

As I came to find out, this issue happens because the GoDaddy FTP server insists on FTP Session Resumption. It's a real pain, but I can forgive them because it is a necessary security feature.

At the moment, Python3 doesn't support SSL session resumption out of the box (although you'll notice SSLContext has been added since 2.7 so I think it will come. https://docs.python.org/3/library/ssl.html#ssl.SSLContext).

There is a bug thread about this issue here: https://bugs.python.org/issue19500

admin0389
  • 51
  • 5
  • Sorry, just wanted add that from reading the above page, a patch has been developed and should be present in Python 3.6. – admin0389 Oct 21 '16 at 19:11
0

I think you need to set the FTP mode to passive, here's a similar error but in Perl

The difference is explained in what-is-the-difference-between-active-and-passive-ftp

Community
  • 1
  • 1
bluemonki
  • 91
  • 3
  • I believe passive mode is on, (you can see it in the FileZilla server log). Regardless, I added the line o.set_pasv(true) after the initial connection line and it didn't appear to have any effect. – admin0389 Oct 18 '16 at 15:44