Do HTTPError objects always have a read method?
steven at REMOVE.THIS.cybersource.com.au
Thu Sep 18 03:48:27 CEST 2008
I understood that HTTPError objects always have a read method, so they
can be treated as if they were a webpage. E.g. something like this
address = 'http://www.yahoo.com/spamspamspamspamspam'
conn = urllib2.urlopen(address)
except urllib2.HTTPError, e:
conn = e
print conn.read() # Print the requested page, or the error page.
But in the source code to urllib2 (Python 2.5):
class HTTPError(URLError, addinfourl):
"""Raised when HTTP error occurs, but also acts like non-error
__super_init = addinfourl.__init__
def __init__(self, url, code, msg, hdrs, fp):
self.code = code
self.msg = msg
self.hdrs = hdrs
self.fp = fp
self.filename = url
# The addinfourl classes depend on fp being a valid file
# object. In some cases, the HTTPError may not have a valid
# file object. If this happens, the simplest workaround is to
# not initialize the base classes.
if fp is not None:
self.__super_init(fp, hdrs, url)
return 'HTTP Error %s: %s' % (self.code, self.msg)
That tells me that HTTPError objects aren't guaranteed to include a file-
like interface. That makes me unhappy.
Under what circumstances do HTTPError objects not have a valid file
object? How common is this? Does anyone have an example of a URL that
fails in that fashion?
More information about the Python-list