FTP_TLS client hangs even though curl is working

58 views Asked by At

We are able to connect to a server which is using explicit FTPS using curl with the following command:

> curl --ssl --list-only --user USER:PASSWD ftp://ftp.COMPANY.com:port/public

Yet this code hangs for a bit after logging "prot_p complete", and then returns "Connection timed out".

import ftplib

with ftplib.FTP_TLS() as ftps:
    print('Connecting')
    ftps.connect(URL, PORT)
    print('Connected')
  
    print('Logging in')
    ftps.login(LOGIN, PASSWD)
    print('Logged in')

    ftps.prot_p()
    print('prot_p complete')

    ftps.retrlines('LIST')
1

There are 1 answers

0
bpeikes On

After doing some research, it appears that ftplib does not play well with Microsoft's FTP server when using FTPS. To get it to work properly, you need to trick ftplib by forcing it to use EPSV mode. This can be done by setting the address family of the client to IPV6:

import ftplib
import socket

with ftplib.FTP_TLS() as ftps:
   print('Connecting')
   ftps.connect(URL, PORT)
   print('Connected')

   print('Logging in')
   ftps.login(LOGIN, PASSWD)
   print('Logged in')

   ftps.prot_p()
   print('prot_p complete')

   #This is needed for Windows FTPS, tricke ftplib into using EPSV mode
   ftps.af = socket.AF_INET6

   ftps.retrlines('LIST')