[Tutor] FTP question (slightly OT)

Paul Schewietzek tetsuo2k6 at web.de
Tue Aug 19 15:16:44 CEST 2008

Hello everyone,

I put together the following, most of it is from different howtos on the

#!/usr/bin/env python
# -*- coding: utf-8 -*-

def gettext(ftp, filename, outfile=None):
        if outfile is None:
                outfile = sys.stdout
        ftp.retrlines("RETR %s" % filename, lambda s, w=outfile.write:

def main():
        from ftplib import FTP
        import datetime
        ftp = FTP("ftp.ftpserver.com")
        ftp.login("my_username", "my_passwd")

        filename = "name_of_textfile"
        outfile = open(filename, "w")

        gettext(ftp, filename, outfile)

if __name__ == "__main__":

The script actually runs fine when "ftp.ftpserver.com" == "my_server_a",
however when "ftp.ftpserver.com" == "my_server_b", it produces:

Traceback (most recent call last):
  File "/usr/local/bin/get_afendis_STATS_from_ftp.py", line 24, in ?
  File "/usr/local/bin/get_afendis_STATS_from_ftp.py", line 20, in main
    gettext(ftp, filename, outfile)
  File "/usr/local/bin/get_afendis_STATS_from_ftp.py", line 7, in
    ftp.retrlines("RETR %s" % filename, lambda s, w=outfile.write: w(s
  File "/usr/lib/python2.4/ftplib.py", line 396, in retrlines
    conn = self.transfercmd(cmd)
  File "/usr/lib/python2.4/ftplib.py", line 345, in transfercmd
    return self.ntransfercmd(cmd, rest)[0]
  File "/usr/lib/python2.4/ftplib.py", line 324, in ntransfercmd
  File "<string>", line 1, in connect
socket.error: (110, 'Connection timed out')

So it looks like "my_server_b" has some different requirements or
something? I don't have much experience with the ftp-protocol, so I
thought maybe you guys could point me in the right direction?

The files are definitely existent on the servers, and when I connect to
them via the shell, I can up- and download files as I want.

Seems like the call to ftp.retrlines() somehow doesn't work...

I'm also not sure about ftp.set_pasv(1).

Regards, Paul

More information about the Tutor mailing list