pythonwinscpftp-clientftplibfilezilla

Specify external IP address for active mode in Python ftplib


I'm using active mode for transfers with my FTP server. In WinSCP and FileZilla there is an option for network external IP. If I give my IP in those boxes, I'm able to connect to the FTP server with active mode. But in Python I don’t know how to assign that IP. I can assign ftp.setpasv(false), but it's not retrieving directories but shows you are logged in. But when try to run ftp.dir(), it throws error

Exception has occurred: TimeoutError [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. This is the error. I tried to set source address via ftp.connect function, but its not working, i tried to ignore return ip like some answers i found in stack overflow, but not working .


Solution

  • Re-implement the FTP.sendport to ignore host argument and send your desired IP address instead. Like this:

    class MyFTP(FTP):
        def sendport(self, host, port):
            return super(MyFTP, self).sendport("192.0.2.0", port)
    
    ftp = MyFTP(ftp_server)
    
    # the rest of the code is the same
    

    Btw, FTP servers are not "in ... mode". They can perfectly support both modes at the same time. Much better solution is to use the passive mode.

    This might help you with passive mode problems:
    Cannot list FTP directory using ftplib – but FTP client works