Request Not Valid

Introduction

Facing this issue with Python2.7 and Ftplib ? I won’t waste your time talking shit! Let’s fix it.

Create a new class inheriting Ftplib’s FTP_TLS class.

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

Now, use this new class to create a new session.

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

Now ? Do whatever you want with your session :) It won’t cry for The requested address is not valid in its context.

Why is this happening ?

This issue persists because ftplib is unable to make a connction with the server in passive mode because of an unroutable address. To fix this, we’re forcing it to use the provided hostname regardless of whatever address is returned.

Updates:

It works for these cases as well.

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