1:mod:`urllib.error` --- Exception classes raised by urllib.request 2================================================================== 3 4.. module:: urllib.error 5 :synopsis: Exception classes raised by urllib.request. 6 7.. moduleauthor:: Jeremy Hylton <jeremy@alum.mit.edu> 8.. sectionauthor:: Senthil Kumaran <orsenthil@gmail.com> 9 10**Source code:** :source:`Lib/urllib/error.py` 11 12-------------- 13 14The :mod:`urllib.error` module defines the exception classes for exceptions 15raised by :mod:`urllib.request`. The base exception class is :exc:`URLError`. 16 17The following exceptions are raised by :mod:`urllib.error` as appropriate: 18 19.. exception:: URLError 20 21 The handlers raise this exception (or derived exceptions) when they run into 22 a problem. It is a subclass of :exc:`OSError`. 23 24 .. attribute:: reason 25 26 The reason for this error. It can be a message string or another 27 exception instance. 28 29 .. versionchanged:: 3.3 30 :exc:`URLError` has been made a subclass of :exc:`OSError` instead 31 of :exc:`IOError`. 32 33 34.. exception:: HTTPError 35 36 Though being an exception (a subclass of :exc:`URLError`), an 37 :exc:`HTTPError` can also function as a non-exceptional file-like return 38 value (the same thing that :func:`~urllib.request.urlopen` returns). This 39 is useful when handling exotic HTTP errors, such as requests for 40 authentication. 41 42 .. attribute:: code 43 44 An HTTP status code as defined in :rfc:`2616`. This numeric value corresponds 45 to a value found in the dictionary of codes as found in 46 :attr:`http.server.BaseHTTPRequestHandler.responses`. 47 48 .. attribute:: reason 49 50 This is usually a string explaining the reason for this error. 51 52 .. attribute:: headers 53 54 The HTTP response headers for the HTTP request that caused the 55 :exc:`HTTPError`. 56 57 .. versionadded:: 3.4 58 59.. exception:: ContentTooShortError(msg, content) 60 61 This exception is raised when the :func:`~urllib.request.urlretrieve` 62 function detects that 63 the amount of the downloaded data is less than the expected amount (given by 64 the *Content-Length* header). The :attr:`content` attribute stores the 65 downloaded (and supposedly truncated) data. 66 67