urllib.error.rst 2.13 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.
Jeremy Hylton's avatar
Jeremy Hylton committed
6
.. moduleauthor:: Jeremy Hylton <jeremy@alum.mit.edu>
7 8 9
.. sectionauthor:: Senthil Kumaran <orsenthil@gmail.com>


10
The :mod:`urllib.error` module defines the exception classes for exceptions
11
raised by :mod:`urllib.request`.  The base exception class is :exc:`URLError`.
12 13 14 15 16

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

.. exception:: URLError

17
   The handlers raise this exception (or derived exceptions) when they run into
18
   a problem.  It is a subclass of :exc:`OSError`.
19 20 21

   .. attribute:: reason

22
      The reason for this error.  It can be a message string or another
23 24 25 26 27
      exception instance.

   .. versionchanged:: 3.3
      :exc:`URLError` has been made a subclass of :exc:`OSError` instead
      of :exc:`IOError`.
28 29 30 31


.. exception:: HTTPError

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

   .. attribute:: code

40 41 42 43
      An HTTP status code as defined in `RFC 2616
      <http://www.faqs.org/rfcs/rfc2616.html>`_.  This numeric value corresponds
      to a value found in the dictionary of codes as found in
      :attr:`http.server.BaseHTTPRequestHandler.responses`.
44

45 46 47 48
   .. attribute:: reason

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

49 50
   .. attribute:: headers

51
      The HTTP response headers for the HTTP request that caused the
52 53 54 55
      :exc:`HTTPError`.

      .. versionadded:: 3.4

56
.. exception:: ContentTooShortError(msg, content)
57

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