
Untitled
By: a guest on
Jun 17th, 2012 | syntax:
None | size: 1.19 KB | hits: 19 | expires: Never
How do I access response headers on an error using urlib2?
try:
request = urllib2.Request( url=self.uri+url, headers=self.headers )
r = urllib2.urlopen(request)
xml = r.read()
return parseString( xml )
except urllib2.HTTPError as err:
logger.debug("EXCEPTION: %s" % err.read() )
logger.debug(err.headers)
(Pdb) pp err.__dict__
{'__iter__': <bound method _fileobject.__iter__ of <socket._fileobject object at 0x2b9a8e923950>>,
'code': 404,
'fileno': <bound method _fileobject.fileno of <socket._fileobject object at 0x2b9a8e923950>>,
'fp': <addinfourl at 47942867504160 whose fp = <socket._fileobject object at 0x2b9a8e923950>>,
'hdrs': <httplib.HTTPMessage instance at 0x2b9a91964a70>,
'headers': <httplib.HTTPMessage instance at 0x2b9a91964a70>,
'msg': 'Not Found',
'next': <bound method _fileobject.next of <socket._fileobject object at 0x2b9a8e923950>>,
'read': <bound method _fileobject.read of <socket._fileobject object at 0x2b9a8e923950>>,
'readline': <bound method _fileobject.readline of <socket._fileobject object at 0x2b9a8e923950>>,
'readlines': <bound method _fileobject.readlines of <socket._fileobject object at 0x2b9a8e923950>>,
'url': 'http://www.heise.de/fo'}