2011-03-29 2 views

답변

5

내가 옵션 2와 같은 코드를 사용, 아래 ...하지만 포괄적 인 답변을, Michael Foord's urllib2 page

보면, 당신은 많은 정보와 당신이 좋아 분기를 추가 할 수 있습니다 e.code 또는 e.reason

옵션을보고하여 절을 제외하고 1 :

from urllib2 import Request, urlopen, URLError, HTTPError 
req = Request(someurl) 
try: 
    response = urlopen(req) 
except HTTPError, e: 
    print 'The server couldn\'t fulfill the request.' 
    print 'Error code: ', e.code 
except URLError, e: 
    print 'We failed to reach a server.' 
    print 'Reason: ', e.reason 
else: 
    # everything is fine 

옵션 2 :

0 12,378,
+0

나는'timeout'에 해당하는'reason' 코드를 찾아 볼 수있는 당신이 말해 주시겠습니까? – satoru

+0

나는 당신이 HTTP Timeout에 대해 이야기하고 있다고 가정하고 있는데, 이는 HTTPError에서 e.code = 408이다. –

+0

아마 e.reason == 'time out'을 시도해야 할 것이다. '__str__'이' % self.reason'와 같은'URLError' 소스를 읽었고 추적 결과''을 볼 수 있습니다. – satoru

0

나는 시간 제한 오류 및 기타 URLError을 차별화하기 위해 다음 코드를 사용

except URLError, e: 
    if e.reason.message == 'timed out': 
     # handle timed out exception 
    else: 
     # other URLError