0

I connected to an FTP server that uses TLS Explicit encryption using the ftplib from Python, i can switch between paths, but i can't retrieve the files and directories from it.

from ftplib import FTP_TLS
ftps = FTP_TLS('HOST')
ftps.login('USER', 'PASS')
ftps.prot_p()
ftps.cwd('path/to/files/')
print(ftps.sendcmd('PWD')) #print current path

ftps.retrlines('LIST') #code stop here

When i debug, the code stop in this line on the retrlines function from ftplib.py:

with self.transfercmd(cmd) as conn, \
         conn.makefile('r', encoding=self.encoding) as fp:

I have tried to use other functions, like ftp.nlst(), but the same issue happens.

mpioski
  • 28
  • 1
  • 6

1 Answers1

1

Below solution worked for the @mpioski and it is working for Python 2.7.12 and Python 3.5.2 for TLS encryption.

from ftplib import FTP_TLS

# replace original makepasv function with one which always returns
# the peerhost of the control connections as peerhost for the data
# connection
_old_makepasv = FTP_TLS.makepasv
def _new_makepasv(self):
    host,port = _old_makepasv(self)
    host = self.sock.getpeername()[0]
    return host,port
FTP_TLS.makepasv = _new_makepasv

ftp = FTP_TLS(ipAddress)
ftp.login(...)
ftp.nlst()

Following code is works for me, for non TLS encryption, we get all files listed of that location.

from ftplib import FTP
ftp = FTP(str(ftp_hostname),str(ftp_username),str(ftp_password))
ftp.cwd(str(ftp_location))
files_list = ftp.nlst()
for filename in files_list:
    print(filename) 
Anup Yadav
  • 2,825
  • 3
  • 21
  • 30
  • I have tried this, but i can't even connect because the ftp use TLS Explicit encryption, so i need to import FTP_TLS. – mpioski Jan 24 '18 at 13:33
  • oh! Okay, then try these 2 https://stackoverflow.com/questions/44057732/connecting-to-explicit-ftp-over-tls-in-python OR https://stackoverflow.com/questions/5534830/ftpes-ftp-over-explicit-tls-ssl-in-python – Anup Yadav Jan 24 '18 at 13:37
  • Thanks man, the method in https://stackoverflow.com/questions/44057732/connecting-to-explicit-ftp-over-tls-in-python works! – mpioski Jan 24 '18 at 13:50
  • 2
    @AnupYadav you should [edit] your answer (you can link to the other question) so your answer stands for itself. The comments aren't supposed to stay forever. that would make your answer better & complete. – Jean-François Fabre Jan 24 '18 at 13:52
  • Thanks @Jean-FrançoisFabre Will edit the answer. – Anup Yadav Jan 24 '18 at 14:11