Checking a Python FTP connection

血红的双手。 提交于 2019-12-03 07:45:39
Sean Dawson

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
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!