Python ftplib timing out

Ian Burris picture Ian Burris · Aug 10, 2010 · Viewed 15.4k times · Source

I'm trying to use ftplib to get a file listing and download any new files since my last check. The code I'm trying to run so far is:

#!/usr/bin/env python
from ftplib import FTP
import sys

host = 'ftp.***.com'
user = '***'
passwd = '***'

try:
    ftp = FTP(host)
    ftp.login(user, passwd)
except:
    print 'Error connecting to FTP server'
    sys.exit()

try:
    ftp.retrlines('LIST')
except:
    print 'Error fetching file listing'
    ftp.quit()
    sys.exit()

ftp.quit() 

Whenever I run this it times out when I try to retrieve the listing. Any ideas?

Answer

Jitendra Joshi picture Jitendra Joshi · Mar 22, 2012

If Passive Mode is failing for some reason try:

ftp.set_pasv(False)

to use Active Mode.