request not valid

facing this with Python 2.7 and ftplib? i won’t waste your time; let’s fix it.

the error shows up when passive mode returns an address your client can’t route to. ftplib trusts that address; your network doesn’t. so we stop trusting it — subclass FTP_TLS, keep the port from makepasv, and force the host you already connected to.

import ftplib
class FTP_TLS_UNROUTABLE_HOST(ftplib.FTP_TLS, object):
    def makepasv(self):
        _, port = super(FTP_TLS_UNROUTABLE_HOST, self).makepasv()
        return self.host, port

then open the session with the new class:

session = FTP_TLS_UNROUTABLE_HOST('host','username','password')

from there, use the session like normal; it shouldn’t keep crying about “the requested address is not valid in its context.”

same pattern also helps with:

500 Illegal PORT command.
The requested address is not valid in its context.

ugly problem; small override. that’s usually the right ratio.