Cannot list FTP directory using ftplib – but FTP client works

后端 未结 1 1795
梦谈多话
梦谈多话 2020-12-02 00:34

I\'m trying to connect to an FTP but I am unable to run any commands.

ftp_server = ip
ftp_username = username
ftp_password = password

ftp = ftplib.FTP(ftp_         


        
相关标签:
1条回答
  • 2020-12-02 01:06

    Status: Server sent passive reply with unroutable address

    The above means that the FTP server is misconfigured. It sends its internal network IP to outside network (to the client – FileZilla or Python ftplib), where it is invalid. FileZilla can detect that and automatically fall back to the original IP address of the server.

    Python ftplib does not do this kind of detection.

    You need to fix your FTP server to return the correct IP address.


    If it is not feasible to fix the server (it's not yours and the admin is not cooperative), you can make ftplib ignore the returned (invalid) IP address and use the original address instead by overriding FTP.makepasv:

    class SmartFTP(FTP):
        def makepasv(self):
            invalidhost, port = super(SmartFTP, self).makepasv()
            return self.host, port
    
    ftp = SmartFTP(ftp_server)
    
    # the rest of the code is the same
    

    Another solution may be to use IPv6. See Python 3.8.5 FTPS connection.

    0 讨论(0)
提交回复
热议问题