我如何设置一个10秒的超时,如果它无法上传或超时再试一次?
当前代码:
print "Uploading LIST{}.html".format(counter)
ftp_session = ftplib.FTP('ftp.website.com','admin@website.com','password123')
rss_ftp_file = open('OUTPUT/LISTS/LIST{}.html'.format(counter),'r')
ftp_session.cwd("/LISTS/")
ftp_session.storlines('STOR LIST{}.html.tmp'.format(counter), rss_ftp_file)
rss_ftp_file.close()
ftp_session.rename('LIST{}.html.tmp'.format(counter), 'LIST{}.html'.format(counter))
ftp_session.quit()试过以下几点
for i in range(3):
try:
print "Uploading LIST{}.html".format(counter)
ftp_session = ftplib.FTP('ftp.website.com','admin@website.com','password123','',60)
rss_ftp_file = open('OUTPUT/LISTS/LIST{}.html'.format(counter),'r')
ftp_session.cwd("/LISTS/")
ftp_session.storlines('STOR LIST{}.html.tmp'.format(counter), rss_ftp_file)
rss_ftp_file.close()
ftp_session.rename('LIST{}.html.tmp'.format(counter), 'LIST{}.html'.format(counter))
ftp_session.quit()
break
except:
continue
else:
open('OUTPUT/LISTS/LIST{}.html'.format(counter),'w').close()但是它上传每个列表3次,它应该上传列表,如果它超时,那么它应该再次尝试,但是如果它超时3次,它应该删除列表文件中的内容,如“否则”中所示。如果成功上传,就不应该再试一次,也不应该传递the语句
谢谢
发布于 2013-12-07 02:53:59
您可以在timeout构造函数中指定FTP参数(只要您正在运行2.6+)。
class ftplib.FTP([host[, user[, passwd[, acct[, timeout]]]]])返回FTP类的新实例。当给出host时,方法调用connect(host)。当给出user时,方法调用login(user, passwd, acct)(其中passwd和acct默认为空字符串,当不给定时)。可选的timeout参数指定以秒为单位的超时,用于阻塞操作,如连接尝试(如果未指定,则将使用全局默认超时设置)。
我相信,随后的阻塞操作比您的timeout还要长,将引发socket.timeout异常。您可以捕获这些,并根据需要重试。
https://stackoverflow.com/questions/20436922
复制相似问题