Search code examples
pythondockerflaskftpftplib

FTP error "500 Illegal PORT command" while trying to connect from a dockerized Python script


I've created a Python Flask script which moves some file from an FTP server to another when certain condition happens. (both the FTP servers are in two different Docker) On my local machine the script works fine but when I try to put it in a Docker it boot up but when a request arrives I get two errors:

ftplib.error_perm: 500 Illegal PORT command.
ftplib.error_perm: 550 Illegal PORT command. Probably the file is no longer avaible on the FTP server?

Since that from my local machine works well I was thinking that it could be a problem with docker.

This is the code which triggers the copy:

for filename, n in n_request:
    print(self.color + "File name: " + filename + " Count: " + str(n) + RESET, flush=True)
    if n >= int(self.count):
        print(self.color + "Mooving file: " + filename + " from server: " + str(self.server) + RESET, flush=True)
        start = time.time()
        check = move_file(filename, self.server, self.client)
        end = time.time()

And this is the move_file function:

def move_file(filename, server, client):
    from_ftp = FTP_handler(server[0], server[1], DEFAULT_USER, DEFAULT_PSW)
    to_ftp = FTP_handler(client[1], client[2], DEFAULT_USER, DEFAULT_PSW)
    return transfer_file(from_ftp, to_ftp, filename)

The connection is set to be in passive mode.


Solution

  • If you get "500 Illegal PORT command.", you are using the active mode, not the passive mode. Double check that and make sure to really use the passive mode.

    For an explanation of the error message (even though in different context), see:
    ftp_get: Illegal PORT command.