Python Ftp Implicit Tls Connection Issue

Connecting to 'Explicit FTP over TLS' in Python (??)

*get* '227 Entering Passive Mode (10,19,1,137,195,128).\n'

The problem is that the server is returning the wrong IP address inside the response to the PASV command. This is typical for servers in some internal network behind some firewall. In this case 10.19.1.137 is returned, which is an IP address usable in local networks only.

This is a broken setup of the FTP server. But unfortunately such broken setup is common so many clients work around it by ignoring the given IP address in the response and using instead the IP address of the control connection. ftplib has no support for such workaround. But one can monkey patch it to provide such support:

from ftplib import FTP_TLS

# replace original makepasv function with one which always returns
# the peerhost of the control connections as peerhost for the data
# connection
_old_makepasv = FTP_TLS.makepasv
def _new_makepasv(self):
host,port = _old_makepasv(self)
host = self.sock.getpeername()[0]
return host,port
FTP_TLS.makepasv = _new_makepasv

ftp = FTP_TLS(ipAddress)
ftp.login(...)
ftp.nlst()

This is successfully tested with Python 2.7.12 and Python 3.5.2

When trying to download files using a Python FTP_TLS object getting AttributeError?

Your use of the timeout arg (which is an int) is in the PLACE of the context variable in this line:

FTP_TLS.__init__(self, host, user, passwd, acct, keyfile, certfile, timeout)

Should be:

FTP_TLS.__init__(self, host, user, passwd, acct, keyfile, certfile, context, timeout)

See:
https://docs.python.org/2/library/ftplib.html



Related Topics



Leave a reply



Submit