How to catch FTP errors? for example socket.error: [Errno 10060]

I use 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 can 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, which I also have to catch?

Thanks for any help or pointers.

+3
source share
2 answers
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 (regardless of their "errno" - those 10000 and above are quite specific for Windows, they are very different on Unix).

, ; ftplib.all_errors ( socket.error biggie, IOError), except ftplib.all_errors, e:.

+3

. , , socket.error

s.storbinary(stor, f)

.

+2

Source: https://habr.com/ru/post/1745206/


All Articles