Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 17th, 2012  |  syntax: None  |  size: 1.19 KB  |  hits: 19  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How do I access response headers on an error using urlib2?
  2. try:
  3.     request = urllib2.Request( url=self.uri+url, headers=self.headers )
  4.     r = urllib2.urlopen(request)
  5.     xml = r.read()
  6.     return parseString( xml )
  7. except urllib2.HTTPError as err:
  8.     logger.debug("EXCEPTION: %s" % err.read() )
  9.        
  10. logger.debug(err.headers)
  11.        
  12. (Pdb) pp err.__dict__
  13. {'__iter__': <bound method _fileobject.__iter__ of <socket._fileobject object at 0x2b9a8e923950>>,
  14.  'code': 404,
  15.  'fileno': <bound method _fileobject.fileno of <socket._fileobject object at 0x2b9a8e923950>>,
  16.  'fp': <addinfourl at 47942867504160 whose fp = <socket._fileobject object at 0x2b9a8e923950>>,
  17.  'hdrs': <httplib.HTTPMessage instance at 0x2b9a91964a70>,
  18.  'headers': <httplib.HTTPMessage instance at 0x2b9a91964a70>,
  19.  'msg': 'Not Found',
  20.  'next': <bound method _fileobject.next of <socket._fileobject object at 0x2b9a8e923950>>,
  21.  'read': <bound method _fileobject.read of <socket._fileobject object at 0x2b9a8e923950>>,
  22.  'readline': <bound method _fileobject.readline of <socket._fileobject object at 0x2b9a8e923950>>,
  23.  'readlines': <bound method _fileobject.readlines of <socket._fileobject object at 0x2b9a8e923950>>,
  24.  'url': 'http://www.heise.de/fo'}