tags:

views:

234

answers:

2

I'm using the ftplib module to upload files:

    files = [ a.txt , b.txt , c.txt ]

    s = ftplib.FTP(ftp_server , ftp_user , ftp_pw) # Connect to FTP
    for i in range(len(files)):
            f = open(files[i], 'rb')
            stor = 'stor ' + files[i]
            s.storbinary(stor, f)
            f.close() # close file
    s.quit() # close ftp

How do I catch the following error?

socket.error: [Errno 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

And what other errors are common when using the FTP module that I should also catch?

Thanks for any help or pointers.

+2  A: 
import socket

try:
    s = ftplib.FTP(ftp_server , ftp_user , ftp_pw) # Connect to FTP
except socket.error, e:
    print "do something with %s" % e

this will catch all socket errors (whatever their "errno" -- ones 10000 and up are quite Windows specific, they're very different on Unix).

See the docs for other exceptions that may be raised; they're all in the tuple ftplib.all_errors (as is socket.error and the last biggie, IOError) so you can handily catch them all with except ftplib.all_errors, e:.

Alex Martelli