【发布时间】:2017-07-03 06:14:24
【问题描述】:
这是我的代码,我按照 python 文档中编写的方式处理了异常,但有时我不知道会发生什么,但我的代码停留在 print "SERVER RESPONSE" 行并且不会继续前进,我必须强行停下来。它甚至没有抛出任何异常。它只是在终端上打印了 try: 中的 print "SERVER RESPONSE" 块后停止。
def upload(filename1,sampleFile,unknown_path,predictiona,predictionb):
curr_time = (time.strftime("%H:%M:%S"))
curr_day = (time.strftime("%Y-%m-%d"))
register_openers()
datagen, headers = multipart_encode({"sampleFile": open(sampleFile), "name": filename1, "userID":'19','date': curr_day,'time': curr_time})
print"header",headers
request = urllib2.Request("http://videoupload.hopto.org:5000/api/Sync_log", datagen, headers)
try:
print "SERVER RESPONSE"
response = urllib2.urlopen(request)
html=response.read()
except URLError , e:
if hasattr(e, 'reason'):
print 'We failed to reach a server.'
print 'Reason: ', e.reason
elif hasattr(e, 'code'):
print 'The server couldn\'t fulfill the request.'
print 'Error code: ', e.code
else:
print "response ",response
print "html ",html
【问题讨论】:
标签: python exception-handling server urllib2 urllib