Cannot list FTP directory using ftplib – but FTP client works

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.

For a different problem with similar consequences, see vsftpd returns 0,0,0,0 in response to PASV.

Leave a Comment