1

I'm connecting to an external FTP/FTPS server using python3 ftplib.FTP_TLS class. When the connection is established in passive mode, the server responds with an internal network IP address like 10.10.XX.XX.

Since I am outside of the network I can't access the server on the provided IP address and ftplib hangs up. Setting the FTPS connection as active doesn't work.

What is the best way to force ftplib to use the original hostname or the external IP address?

nbwoodward
  • 2,816
  • 1
  • 16
  • 24
  • Yep thanks it is a duplicate, upvoted the other answer so it gets bumped up google a bit. – nbwoodward May 20 '19 at 20:22
  • 1
    But your duplicate question is good to have anyway, as it helps to find the solution to those that know the root cause (what the other OP did not). – Martin Prikryl May 20 '19 at 20:29

1 Answers1

2

The solution I used was from this article. You override the makepasv() method to ignore the returned IP address and use the original host:

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

ftp = FTP_TLS_IgnoreHost('host', 'user', 'password')

There are probably other good solutions, but I thought this was pretty slick.

nbwoodward
  • 2,816
  • 1
  • 16
  • 24