ftplib question (cannot open data connection)

Laszlo Nagy gandalf at shopzeus.com
Fri Jan 11 08:57:11 EST 2008


  Hi All,

I'm using a simple program that uploads a file on a remote ftp server. 
This is an example (not the whole program):


def store(self,hostname,username,password,destdir,srcpath):
    self.ftp = ftplib.FTP(hostname)
    self.ftp.login(username,password)
    self.ftp.set_pasv(False)
    self.ftp.cwd(destdir)
    fobj = file(srcpath,"rb")
    destname = os.path.split(srcpath)[1]
    self.ftp.storbinary("STOR "+destname,fobj)


The ftp server cannot use passive connections, and I can do nothing 
about that. Here is the problem: I can connect to this ftp server from 
my home computer, which is behind a NAT firewall. I can also connect to 
it from another computer, but I'm not able to upload any file. I tried 
to debug with a simple "ftp -v -d" command line program and apparently 
the problem is with the "EPRT" command:

ftp> ls
---> EPRT |1|195.228.74.135|55749|
200 Port command successful.
---> LIST
425 Cannot open data connection.
ftp>

Well, the port number given by EPRT is bad - it is a closed port on this 
computer. I can open a small port range for this, but I would not like 
to open all ports and disable the firewall completely.

Here are my questions:

1. How can I instruct ftplib to use specific ports for incoming 
connections? (For example, ports between 55000 and 56000).
2. How it is possible that the same program works from another computer 
that is behind a NAT firewall?

Thanks,

   Laszlo




More information about the Python-list mailing list