| 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 <[email protected]> |
| 8 |
.. sectionauthor:: Senthil Kumaran <[email protected]> |
| 9 |
|
| 10 |
**Source code:** :source:`Lib/urllib/error.py` |
| 11 |
|
| 12 |
-------------- |
| 13 |
|
| 14 |
The :mod:`!urllib.error` module defines the exception classes for exceptions |
| 15 |
raised by :mod:`urllib.request`. The base exception class is :exc:`URLError`. |
| 16 |
|
| 17 |
The 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` used to be a subtype of :exc:`IOError`, which is now an |
| 31 |
alias of :exc:`OSError`. |
| 32 |
|
| 33 |
|
| 34 |
.. exception:: HTTPError(url, code, msg, hdrs, fp) |
| 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:: url |
| 43 |
|
| 44 |
Contains the request URL. |
| 45 |
An alias for *filename* attribute. |
| 46 |
|
| 47 |
.. attribute:: code |
| 48 |
|
| 49 |
An HTTP status code as defined in :rfc:`2616`. This numeric value corresponds |
| 50 |
to a value found in the dictionary of codes as found in |
| 51 |
:attr:`http.server.BaseHTTPRequestHandler.responses`. |
| 52 |
|
| 53 |
.. attribute:: reason |
| 54 |
|
| 55 |
This is usually a string explaining the reason for this error. |
| 56 |
An alias for *msg* attribute. |
| 57 |
|
| 58 |
.. attribute:: headers |
| 59 |
|
| 60 |
The HTTP response headers for the HTTP request that caused the |
| 61 |
:exc:`HTTPError`. |
| 62 |
An alias for *hdrs* attribute. |
| 63 |
|
| 64 |
.. versionadded:: 3.4 |
| 65 |
|
| 66 |
.. attribute:: fp |
| 67 |
|
| 68 |
A file-like object where the HTTP error body can be read from. |
| 69 |
|
| 70 |
.. exception:: ContentTooShortError(msg, content) |
| 71 |
|
| 72 |
This exception is raised when the :func:`~urllib.request.urlretrieve` |
| 73 |
function detects that |
| 74 |
the amount of the downloaded data is less than the expected amount (given by |
| 75 |
the *Content-Length* header). |
| 76 |
|
| 77 |
.. attribute:: content |
| 78 |
|
| 79 |
The downloaded (and supposedly truncated) data. |