如何捕获FTP错误?例如,socket.error:[Errno 10060]

时间:2010-05-13 01:31:39

标签: python ftp ftplib

我正在使用ftplib模块上传文件:

    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

如何捕获以下错误?

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

使用FTP模块时我还应该注意哪些其他错误?

感谢您提供任何帮助或指示。

2 个答案:

答案 0 :(得分:3)

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

这将捕获所有套接字错误(无论它们的“错误” - 10000及以上是特定于Windows的,它们在Unix上非常不同)。

有关可能引发的其他例外情况,请参阅the docs;他们都在元组中ftplib.all_errors(就像socket.error和最后一个大事,IOError)所以你可以用except ftplib.all_errors, e:轻松地抓住它们。

答案 1 :(得分:2)

我有类似的错误。我必须做的是在

行捕获socket.error
s.storbinary(stor, f)

以及初始连接。

相关问题