Python ftplib timing out
Asked Answered
C

2

13

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?

Certification answered 10/8, 2010 at 17:43 Comment(0)
M
14

Most likely a conflict between Active and Passive mode. Make sure that one of the following is true:

  1. The server supports PASV mode and your client is setting PASV mode
  2. If the server does not support passive mode, then your firewall must support active mode FTP transfers.

EDIT: I looked at the docs, and found that in Python 2.1 and later the default is passive mode. What server are you talking to, and di you know if it supports passive mode?

In active mode (non-PASV) the client sends a PORT command telling the server to initiate the DATA connection on that port, which requires your firewall be aware of the PORT command so it can forward the incoming DATA connection to you -- few firewalls support this. In passive mode the client opens the DATA connection and the server uses it (the server is "passive" in opening the data connection).

Just in case you're not using passive mode, do a ftp.set_pasv(True) and see if that makes a difference.

Mathilda answered 10/8, 2010 at 18:27 Comment(1)
You're right it was a problem with the mode. The server I was trying to talk to required Active. Thanks for the help!Certification
W
23

If Passive Mode is failing for some reason try:

ftp.set_pasv(False)

to use Active Mode.

Wilbertwilborn answered 22/3, 2012 at 7:19 Comment(2)
I spend 3h trying to make it work on my mac, this fixed it. THANK YOU!!!!!Andy
this saved my brain from total collapse, thanks!Chrysoberyl
M
14

Most likely a conflict between Active and Passive mode. Make sure that one of the following is true:

  1. The server supports PASV mode and your client is setting PASV mode
  2. If the server does not support passive mode, then your firewall must support active mode FTP transfers.

EDIT: I looked at the docs, and found that in Python 2.1 and later the default is passive mode. What server are you talking to, and di you know if it supports passive mode?

In active mode (non-PASV) the client sends a PORT command telling the server to initiate the DATA connection on that port, which requires your firewall be aware of the PORT command so it can forward the incoming DATA connection to you -- few firewalls support this. In passive mode the client opens the DATA connection and the server uses it (the server is "passive" in opening the data connection).

Just in case you're not using passive mode, do a ftp.set_pasv(True) and see if that makes a difference.

Mathilda answered 10/8, 2010 at 18:27 Comment(1)
You're right it was a problem with the mode. The server I was trying to talk to required Active. Thanks for the help!Certification

© 2022 - 2024 — McMap. All rights reserved.