Ben 2. Seçenek gibi kod kullanın aşağıda ... ama kapsamlı bir cevap için, Michael Foord's urllib2 page
bakmak, sen kadar zeka ve istediğiniz gibi dallanma ekleyebilir e.code
veya e.reason
Seçenek bakarak maddeleri hariç, 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
Seçenek 2:
from urllib import urlencode
from urllib2 import Request
# insert other code here...
error = False
error_code = ""
try:
if method.upper()=="GET":
response = urlopen(req)
elif method.upper()=="POST":
response = urlopen(req,data)
except IOError, e:
if hasattr(e, 'reason'):
#print 'We failed to reach a server.'
#print 'Reason: ', e.reason
error = True
error_code = e.reason
elif hasattr(e, 'code'):
#print 'The server couldn\'t fulfill the request.'
#print 'Error code: ', e.code
error = True
error_code = e.code
else:
# info is dictionary of server parameters, such as 'content-type', etc...
info = response.info().dict
page = response.read()
ben 'timeout' için ilgili' reason' kodunu bakabilirsiniz nerede söyler misiniz? – satoru
Bir HTTP Zaman Aşımı'ndan bahsediyorsunuz, yani bir HTTPError –
'dan e.code = 408. Belki de e.reason == 'time out' denemeliyim. __str__' gibi self.reason' gibi giden 'URLError' kaynağını okudum ve tracebackimde' '' ı görebiliyorum. –
satoru