python-如何捕获FTP错误?例如,socket.error:[Errno 10060]
作者:互联网
我正在使用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模块时,我还应该捕捉哪些其他常见错误?
感谢您的帮助或指点.
解决方法:
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
这将捕获所有套接字错误(无论它们的“ errno”是什么-10000及更高版本都是Windows特定的,在Unix上它们是非常不同的).
有关可能引发的其他异常,请参见the docs.它们全都在ftplib.all_errors元组中(socket.error和最后一个笨蛋IOError也是如此),因此您可以方便地用除ftplib.all_errors外的所有内容捕获它们,例如e:.
标签:ftp,ftplib,python 来源: https://codeday.me/bug/20191024/1917767.html