이것은 내 코드 어디에서 파이썬 문서로 쓰여지는지 예외를 처리하지만 어떤 경우에는 발생하지만 내 코드는 print "SERVER RESPONSE"
라인에 갇혀 앞으로 앞으로 나아 가지 않으며 강제로 중지해야합니다. 아무 예외도 던지지 않습니다.이 블록이 인쇄 된 후 print "SERVER RESPONSE"
에서 try:
블록이 인쇄됩니다.Python : urllib2에서 서버가 던진 예외를 처리하는 방법은 무엇입니까?
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
'print "SERVER RESPONSE를 실행 한 후"작동이 멈추고 실행이 일시 중지됩니다. 둘째, 마지막 'else'는'try' 블록 바로 아래에 있습니다. 그들의 들여 쓰기가 일치합니다. 실종 된 것이 있으면 알려주세요. –
간단히 마무리하는 것이 아니라 일시 중지 된 것을 어떻게 알 수 있습니까? –
나는 방금 내 전체 프로그램의 일부를 게시했기 때문에. 그것의 크고이 기능을 실행 한 후에도 다른 일을하고 있습니다. 그리고 제 이전의 주석을 읽어주십시오. 다시 편집했습니다. –