diff --git a/Doc/library/urllib.error.rst b/Doc/library/urllib.error.rst --- a/Doc/library/urllib.error.rst +++ b/Doc/library/urllib.error.rst @@ -36,15 +36,22 @@ The following exceptions are raised by : .. attribute:: code An HTTP status code as defined in `RFC 2616 `_. This numeric value corresponds to a value found in the dictionary of codes as found in :attr:`http.server.BaseHTTPRequestHandler.responses`. + .. attribute:: headers + + The HTTP response headers for the HTTP request that cause the + :exc:`HTTPError`. + + .. versionadded:: 3.4 + .. exception:: ContentTooShortError(msg, content) This exception is raised when the :func:`urlretrieve` function detects that the amount of the downloaded data is less than the expected amount (given by the *Content-Length* header). The :attr:`content` attribute stores the downloaded (and supposedly truncated) data. diff --git a/Lib/urllib/error.py b/Lib/urllib/error.py --- a/Lib/urllib/error.py +++ b/Lib/urllib/error.py @@ -56,13 +56,21 @@ class HTTPError(URLError, urllib.respons return 'HTTP Error %s: %s' % (self.code, self.msg) # since URLError specifies a .reason attribute, HTTPError should also # provide this attribute. See issue13211 for discussion. @property def reason(self): return self.msg + @property + def headers(self): + return self.hdrs + + @headers.setter + def headers(self, headers): + self.hdrs = headers + # exception raised when downloaded size does not match content-length class ContentTooShortError(URLError): def __init__(self, message, content): URLError.__init__(self, message) self.content = content