我正在将图像文件上传到服务器,但是当超时发生时,它会终止上传并移动到下一个。如果发生超时,我想再次上传图像。我想尝试在最大上传三次,并且在三次尝试后如果仍然发生超时则抛出异常然后移动到下一个。
这是我的代码
def upload(filename2,sampleFile,curr_time,curr_day,username,password,user_id):
register_openers()
datagen, headers = multipart_encode({"sampleFile": open(sampleFile), "name": filename2, "userID": user_id,'date': curr_day,'time': curr_time, 'username': username,'password': password})
request = urllib2.Request("http://videoupload.hopto.org:5001/api/Synclog", datagen, headers)
try:
response = urllib2.urlopen(request, timeout = 20)
html=response.read()
except URLError , e:
if hasattr(e, 'reason'):
print ('Reason: ', e.reason)
elif hasattr(e, 'code'):
print ('Error code: ', e.code)
except Exception:
print ('generic exception: ' + traceback.format_exc())
return