exception classes raised by urllib the base exception class is urlerror which inherits from oserror it doesn t define any behavior of its own but is the base class for all exceptions defined in this package httperror is an exception class that is also a valid http response instance it behaves this way because http protocol errors are valid responses with a status code headers and a body in some contexts an application may want to handle an exception like a regular response urlerror is a subtype of oserror but it doesn t share any of the implementation need to override init and str it sets self args for compatibility with other oserror subclasses but args doesn t have the typical format with errno in slot 0 and strerror in slot 1 this may be better than nothing raised when http error occurs but also acts like nonerror return superinit urllib response addinfourl init def initself url code msg hdrs fp self code code self msg msg self hdrs hdrs self fp fp self filename url if fp is none fp io bytesio self superinitfp hdrs url code def strself return http error s s self code self msg def reprself return httperror s r self code self msg since urlerror specifies a reason attribute httperror should also provide this attribute see issue13211 for discussion property def reasonself return self msg property def headersself return self hdrs headers setter def headersself headers self hdrs headers class contenttooshorterrorurlerror urlerror is a sub type of oserror but it doesn t share any of the implementation need to override __init__ and __str__ it sets self args for compatibility with other oserror subclasses but args doesn t have the typical format with errno in slot 0 and strerror in slot 1 this may be better than nothing raised when http error occurs but also acts like non error return since urlerror specifies a reason attribute httperror should also provide this attribute see issue13211 for discussion exception raised when downloaded size does not match content length
import io
import urllib.response

__all__ = ['URLError', 'HTTPError', 'ContentTooShortError']


class URLError(OSError):
    
    
    
    
    
    def __init__(self, reason, filename=None):
        self.args = reason,
        self.reason = reason
        if filename is not None:
            self.filename = filename

    def __str__(self):
        return '<urlopen error %s>' % self.reason


class HTTPError(URLError, urllib.response.addinfourl):
    
    __super_init = urllib.response.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
        if fp is None:
            fp = io.BytesIO()
        self.__super_init(fp, hdrs, url, code)

    def __str__(self):
        return 'HTTP Error %s: %s' % (self.code, self.msg)

    def __repr__(self):
        return '<HTTPError %s: %r>' % (self.code, self.msg)

    
    
    @property
    def reason(self):
        return self.msg

    @property
    def headers(self):
        return self.hdrs

    @headers.setter
    def headers(self, headers):
        self.hdrs = headers


class ContentTooShortError(URLError):
    
    def __init__(self, message, content):
        URLError.__init__(self, message)
        self.content = content