urllib.error.rst 2.15 KB
Newer Older
1 2 3 4
:mod:`urllib.error` --- Exception classes raised by urllib.request
==================================================================

.. module:: urllib.error
5
   :synopsis: Exception classes raised by urllib.request.
6

Jeremy Hylton's avatar
Jeremy Hylton committed
7
.. moduleauthor:: Jeremy Hylton <jeremy@alum.mit.edu>
8 9
.. sectionauthor:: Senthil Kumaran <orsenthil@gmail.com>

10 11 12
**Source code:** :source:`Lib/urllib/error.py`

--------------
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 18 19 20

The following exceptions are raised by :mod:`urllib.error` as appropriate:

.. exception:: URLError

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 25

   .. attribute:: reason

26
      The reason for this error.  It can be a message string or another
27 28 29 30 31
      exception instance.

   .. versionchanged:: 3.3
      :exc:`URLError` has been made a subclass of :exc:`OSError` instead
      of :exc:`IOError`.
32 33 34 35


.. exception:: HTTPError

36 37
   Though being an exception (a subclass of :exc:`URLError`), an
   :exc:`HTTPError` can also function as a non-exceptional file-like return
38 39 40
   value (the same thing that :func:`~urllib.request.urlopen` returns).  This
   is useful when handling exotic HTTP errors, such as requests for
   authentication.
41 42 43

   .. attribute:: code

44
      An HTTP status code as defined in :rfc:`2616`.  This numeric value corresponds
45 46
      to a value found in the dictionary of codes as found in
      :attr:`http.server.BaseHTTPRequestHandler.responses`.
47

48 49 50 51
   .. attribute:: reason

      This is usually a string explaining the reason for this error.

52 53
   .. attribute:: headers

54
      The HTTP response headers for the HTTP request that caused the
55 56 57 58
      :exc:`HTTPError`.

      .. versionadded:: 3.4

59
.. exception:: ContentTooShortError(msg, content)
60

61 62
   This exception is raised when the :func:`~urllib.request.urlretrieve`
   function detects that
63 64 65
   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.
66