Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking a Python FTP connection

Tags:

python

ftplib

I have a FTP connection from which I am downloading many files and processing them in between. I'd like to be able to check that my FTP connection hasn't timed out in between. So the code looks something like:

conn = FTP(host='blah')
conn.connect()
for item in list_of_items:
    myfile = open('filename', 'w')
    conn.retrbinary('stuff", myfile)
    ### do some parsing ###

How can I check my FTP connection in case it timed out during the ### do some parsing ### line?

like image 327
Leo Mizuhara Avatar asked Mar 02 '13 03:03

Leo Mizuhara


People also ask

How do I know if FTP is working Python?

Send a NOOP command. This does nothing but check that the connection is still going and if you do it periodically it can keep the connection alive. If there is a problem with the connection then the FTP object will throw an exception.

How do I start an FTP server in Python?

Configuring and Starting an FTP Server First, SSH in to your server as root and install the Python pyftpdlib library. Next, log out of your server as root. The rest of your steps should be done while logged in as your app's system user. You can now start the FTP server.

How do I transfer files using FTP in Python?

FTP(File Transfer Protocol) To transfer a file, 2 TCP connections are used by FTP in parallel: control connection and data connection. For uploading and downloading the file, we will use ftplib Module in Python. It is an in-built module in Python.


1 Answers

Send a NOOP command. This does nothing but check that the connection is still going and if you do it periodically it can keep the connection alive.

For example:

   conn.voidcmd("NOOP")

If there is a problem with the connection then the FTP object will throw an exception. You can see from the documentation that exceptions are thrown if there is an error:

socket.error and IOError: These are raised by the socket connection and are most likely the ones you are interested in.

exception ftplib.error_reply: Exception raised when an unexpected reply is received from the server.

exception ftplib.error_temp: Exception raised when an error code signifying a temporary error (response codes in the range 400–499) is received.

exception ftplib.error_perm: Exception raised when an error code signifying a permanent error (response codes in the range 500–599) is received.

exception ftplib.error_proto: Exception raised when a reply is received from the server that does not fit the response specifications of the File Transfer Protocol, i.e. begin with a digit in the range 1–5.

Therefore you can use a try-catch block to detect the error and handle it accordingly.

For example this sample of code will catch an IOError, tell you about it and then retry the operation:

retry = True
while (retry):
    try:
        conn = FTP('blah')
        conn.connect()
        for item in list_of_items:
            myfile = open('filename', 'w')
            conn.retrbinary('stuff', myfile)   
            ### do some parsing ###

        retry = False

    except IOError as e:
        print "I/O error({0}): {1}".format(e.errno, e.strerror)
        print "Retrying..."
        retry = True
like image 66
Sean Dawson Avatar answered Sep 25 '22 04:09

Sean Dawson