cpython/Doc/library/http.client.rst

529 lines
33 KiB
ReStructuredText
Raw Normal View History

2008-05-26 13:32:26 -03:00
:mod:`http.client` --- HTTP protocol client
===========================================
2007-08-15 11:28:22 -03:00
2008-05-26 13:32:26 -03:00
.. module:: http.client
2007-08-15 11:28:22 -03:00
:synopsis: HTTP and HTTPS protocol client (requires sockets).
.. index::
pair: HTTP; protocol
2008-05-26 13:32:26 -03:00
single: HTTP; http.client (standard module)
2007-08-15 11:28:22 -03:00
.. index:: module: urllib.request
2007-08-15 11:28:22 -03:00
This module defines classes which implement the client side of the HTTP and
HTTPS protocols. It is normally not used directly --- the module
:mod:`urllib.request` uses it to handle URLs that use HTTP and HTTPS.
2007-08-15 11:28:22 -03:00
.. note::
HTTPS support is only available if the :mod:`socket` module was compiled with
SSL support.
The module provides the following classes:
.. class:: HTTPConnection(host, port=None, strict=None[, timeout])
2007-08-15 11:28:22 -03:00
An :class:`HTTPConnection` instance represents one transaction with an HTTP
Merged revisions 62998-63003,63005-63006,63009-63012,63014-63017,63019-63020,63022-63024,63026-63029,63031-63041,63043-63045,63047-63054,63056-63062 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r62998 | andrew.kuchling | 2008-05-10 15:51:55 -0400 (Sat, 10 May 2008) | 7 lines #1858 from Tarek Ziade: Allow multiple repositories in .pypirc; see http://wiki.python.org/moin/EnhancedPyPI for discussion. The patch is slightly revised from Tarek's last patch: I've simplified the PyPIRCCommand.finalize_options() method to not look at sys.argv. Tests still pass. ........ r63000 | alexandre.vassalotti | 2008-05-10 15:59:16 -0400 (Sat, 10 May 2008) | 5 lines Cleaned up io._BytesIO.write(). I am amazed that the old code, for inserting null-bytes, actually worked. Who wrote that thing? Oh, it is me... doh. ........ r63002 | brett.cannon | 2008-05-10 16:52:01 -0400 (Sat, 10 May 2008) | 2 lines Revert r62998 as it broke the build (seems distutils.config is missing). ........ r63014 | andrew.kuchling | 2008-05-10 18:12:38 -0400 (Sat, 10 May 2008) | 1 line #1858: add distutils.config module ........ r63027 | brett.cannon | 2008-05-10 21:09:32 -0400 (Sat, 10 May 2008) | 2 lines Flesh out the 3.0 deprecation to suggest using the ctypes module. ........ r63028 | skip.montanaro | 2008-05-10 22:59:30 -0400 (Sat, 10 May 2008) | 4 lines Copied two versions of the example from the interactive session. Delete one. ........ r63037 | georg.brandl | 2008-05-11 03:02:17 -0400 (Sun, 11 May 2008) | 2 lines reload() takes the module itself. ........ r63038 | alexandre.vassalotti | 2008-05-11 03:06:04 -0400 (Sun, 11 May 2008) | 4 lines Added test framework for handling module renames. Factored the import guard in test_py3kwarn.TestStdlibRemovals into a context manager, namely test_support.CleanImport. ........ r63039 | georg.brandl | 2008-05-11 03:06:05 -0400 (Sun, 11 May 2008) | 2 lines #2742: ``''`` is not converted to NULL in getaddrinfo. ........ r63040 | alexandre.vassalotti | 2008-05-11 03:08:12 -0400 (Sun, 11 May 2008) | 2 lines Fixed typo in a comment of test_support.CleanImport. ........ r63041 | alexandre.vassalotti | 2008-05-11 03:10:25 -0400 (Sun, 11 May 2008) | 2 lines Removed a dead line of code. ........ r63043 | georg.brandl | 2008-05-11 04:47:53 -0400 (Sun, 11 May 2008) | 2 lines #2812: document property.getter/setter/deleter. ........ r63049 | georg.brandl | 2008-05-11 05:06:30 -0400 (Sun, 11 May 2008) | 2 lines #1153769: document PEP 237 changes to string formatting. ........ r63050 | georg.brandl | 2008-05-11 05:11:40 -0400 (Sun, 11 May 2008) | 2 lines #2809: elaborate str.split docstring a bit. ........ r63051 | georg.brandl | 2008-05-11 06:13:59 -0400 (Sun, 11 May 2008) | 2 lines Fix typo. ........ r63052 | georg.brandl | 2008-05-11 06:33:27 -0400 (Sun, 11 May 2008) | 2 lines #2709: clarification. ........ r63053 | georg.brandl | 2008-05-11 06:42:28 -0400 (Sun, 11 May 2008) | 2 lines #2659: add ``break_on_hyphens`` to TextWrapper. ........ r63057 | georg.brandl | 2008-05-11 06:59:39 -0400 (Sun, 11 May 2008) | 2 lines #2741: clarification of value range for address_family. ........ r63058 | georg.brandl | 2008-05-11 07:09:35 -0400 (Sun, 11 May 2008) | 2 lines #2452: timeout is used for all blocking operations. ........ r63059 | andrew.kuchling | 2008-05-11 09:33:56 -0400 (Sun, 11 May 2008) | 2 lines #1792: Improve performance of marshal.dumps() on large objects by increasing the size of the buffer more quickly. ........ r63060 | andrew.kuchling | 2008-05-11 10:00:00 -0400 (Sun, 11 May 2008) | 1 line #1858: re-apply patch for this, adding the missing files ........ r63061 | benjamin.peterson | 2008-05-11 10:13:25 -0400 (Sun, 11 May 2008) | 2 lines Add the "until" command to pdb ........ r63062 | georg.brandl | 2008-05-11 10:17:13 -0400 (Sun, 11 May 2008) | 2 lines Add some sentence endings. ........
2008-05-15 21:03:33 -03:00
server. It should be instantiated passing it a host and optional port
number. If no port number is passed, the port is extracted from the host
string if it has the form ``host:port``, else the default HTTP port (80) is
Merged revisions 67154,67157-67159,67175-67176,67189,67224-67227,67234 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r67154 | hirokazu.yamamoto | 2008-11-07 21:46:17 -0600 (Fri, 07 Nov 2008) | 1 line Issue #4071: ntpath.abspath returned an empty string for long unicode path. ........ r67157 | georg.brandl | 2008-11-08 05:47:44 -0600 (Sat, 08 Nov 2008) | 2 lines Don't use "HOWTO" as the title for all howto .tex files. ........ r67158 | georg.brandl | 2008-11-08 05:48:20 -0600 (Sat, 08 Nov 2008) | 2 lines Update "Documenting" a bit. Concentrate on Python-specifics. ........ r67159 | georg.brandl | 2008-11-08 06:52:25 -0600 (Sat, 08 Nov 2008) | 2 lines Fix warning. ........ r67175 | benjamin.peterson | 2008-11-08 19:44:32 -0600 (Sat, 08 Nov 2008) | 1 line update link ........ r67176 | benjamin.peterson | 2008-11-08 19:52:32 -0600 (Sat, 08 Nov 2008) | 1 line fix comment ........ r67189 | benjamin.peterson | 2008-11-11 15:56:06 -0600 (Tue, 11 Nov 2008) | 1 line use correct name ........ r67224 | georg.brandl | 2008-11-15 02:10:04 -0600 (Sat, 15 Nov 2008) | 2 lines #4324: fix getlocale() argument. ........ r67225 | brett.cannon | 2008-11-15 16:33:25 -0600 (Sat, 15 Nov 2008) | 1 line Clarify the docs for the 'strict' argument to httplib.HTTPConnection. ........ r67226 | brett.cannon | 2008-11-15 16:40:44 -0600 (Sat, 15 Nov 2008) | 4 lines The docs for httplib.HTTPConnection.putheader() have claimed for quite a while that their could be an arbitrary number of values passed in. Turns out the code did not match that. The code now matches the docs. ........ r67227 | georg.brandl | 2008-11-16 02:00:17 -0600 (Sun, 16 Nov 2008) | 2 lines #4316: fix configure.in markup problem. ........ r67234 | benjamin.peterson | 2008-11-16 11:54:55 -0600 (Sun, 16 Nov 2008) | 1 line run autoconf ........
2008-11-16 14:33:53 -04:00
used. When True, the optional parameter *strict* (which defaults to a false
value) causes ``BadStatusLine`` to
Merged revisions 62998-63003,63005-63006,63009-63012,63014-63017,63019-63020,63022-63024,63026-63029,63031-63041,63043-63045,63047-63054,63056-63062 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r62998 | andrew.kuchling | 2008-05-10 15:51:55 -0400 (Sat, 10 May 2008) | 7 lines #1858 from Tarek Ziade: Allow multiple repositories in .pypirc; see http://wiki.python.org/moin/EnhancedPyPI for discussion. The patch is slightly revised from Tarek's last patch: I've simplified the PyPIRCCommand.finalize_options() method to not look at sys.argv. Tests still pass. ........ r63000 | alexandre.vassalotti | 2008-05-10 15:59:16 -0400 (Sat, 10 May 2008) | 5 lines Cleaned up io._BytesIO.write(). I am amazed that the old code, for inserting null-bytes, actually worked. Who wrote that thing? Oh, it is me... doh. ........ r63002 | brett.cannon | 2008-05-10 16:52:01 -0400 (Sat, 10 May 2008) | 2 lines Revert r62998 as it broke the build (seems distutils.config is missing). ........ r63014 | andrew.kuchling | 2008-05-10 18:12:38 -0400 (Sat, 10 May 2008) | 1 line #1858: add distutils.config module ........ r63027 | brett.cannon | 2008-05-10 21:09:32 -0400 (Sat, 10 May 2008) | 2 lines Flesh out the 3.0 deprecation to suggest using the ctypes module. ........ r63028 | skip.montanaro | 2008-05-10 22:59:30 -0400 (Sat, 10 May 2008) | 4 lines Copied two versions of the example from the interactive session. Delete one. ........ r63037 | georg.brandl | 2008-05-11 03:02:17 -0400 (Sun, 11 May 2008) | 2 lines reload() takes the module itself. ........ r63038 | alexandre.vassalotti | 2008-05-11 03:06:04 -0400 (Sun, 11 May 2008) | 4 lines Added test framework for handling module renames. Factored the import guard in test_py3kwarn.TestStdlibRemovals into a context manager, namely test_support.CleanImport. ........ r63039 | georg.brandl | 2008-05-11 03:06:05 -0400 (Sun, 11 May 2008) | 2 lines #2742: ``''`` is not converted to NULL in getaddrinfo. ........ r63040 | alexandre.vassalotti | 2008-05-11 03:08:12 -0400 (Sun, 11 May 2008) | 2 lines Fixed typo in a comment of test_support.CleanImport. ........ r63041 | alexandre.vassalotti | 2008-05-11 03:10:25 -0400 (Sun, 11 May 2008) | 2 lines Removed a dead line of code. ........ r63043 | georg.brandl | 2008-05-11 04:47:53 -0400 (Sun, 11 May 2008) | 2 lines #2812: document property.getter/setter/deleter. ........ r63049 | georg.brandl | 2008-05-11 05:06:30 -0400 (Sun, 11 May 2008) | 2 lines #1153769: document PEP 237 changes to string formatting. ........ r63050 | georg.brandl | 2008-05-11 05:11:40 -0400 (Sun, 11 May 2008) | 2 lines #2809: elaborate str.split docstring a bit. ........ r63051 | georg.brandl | 2008-05-11 06:13:59 -0400 (Sun, 11 May 2008) | 2 lines Fix typo. ........ r63052 | georg.brandl | 2008-05-11 06:33:27 -0400 (Sun, 11 May 2008) | 2 lines #2709: clarification. ........ r63053 | georg.brandl | 2008-05-11 06:42:28 -0400 (Sun, 11 May 2008) | 2 lines #2659: add ``break_on_hyphens`` to TextWrapper. ........ r63057 | georg.brandl | 2008-05-11 06:59:39 -0400 (Sun, 11 May 2008) | 2 lines #2741: clarification of value range for address_family. ........ r63058 | georg.brandl | 2008-05-11 07:09:35 -0400 (Sun, 11 May 2008) | 2 lines #2452: timeout is used for all blocking operations. ........ r63059 | andrew.kuchling | 2008-05-11 09:33:56 -0400 (Sun, 11 May 2008) | 2 lines #1792: Improve performance of marshal.dumps() on large objects by increasing the size of the buffer more quickly. ........ r63060 | andrew.kuchling | 2008-05-11 10:00:00 -0400 (Sun, 11 May 2008) | 1 line #1858: re-apply patch for this, adding the missing files ........ r63061 | benjamin.peterson | 2008-05-11 10:13:25 -0400 (Sun, 11 May 2008) | 2 lines Add the "until" command to pdb ........ r63062 | georg.brandl | 2008-05-11 10:17:13 -0400 (Sun, 11 May 2008) | 2 lines Add some sentence endings. ........
2008-05-15 21:03:33 -03:00
be raised if the status line can't be parsed as a valid HTTP/1.0 or 1.1
status line. If the optional *timeout* parameter is given, blocking
operations (like connection attempts) will timeout after that many seconds
Merged revisions 63562,63570,63728,63734,63784,63788,63802,63817,63827,63839,63887,63975,63998 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r63562 | martin.v.loewis | 2008-05-23 17:06:50 +0200 (Fri, 23 May 2008) | 2 lines Patch #1722225: Support QNX 6. ........ r63570 | trent.nelson | 2008-05-23 22:33:14 +0200 (Fri, 23 May 2008) | 1 line Introduce a user macro named $(externalsDir), which should point to the root directory of where all the external sources should live. Developers can change this value if their external sources live elsewhere. The default of '..\..' matches the current status quo. ........ r63728 | gregory.p.smith | 2008-05-26 23:16:34 +0200 (Mon, 26 May 2008) | 4 lines Fix issue2589: there was a potential integer overflow leading to memory corruption on esoteric platforms and incorrect behavior on normal platforms. ........ r63734 | gregory.p.smith | 2008-05-27 00:07:28 +0200 (Tue, 27 May 2008) | 3 lines Fix issue2588: Do not execute str[size-1] = '\0' when a 0 size is passed in. (The assert won't prevent this in non-debug builds). ........ r63784 | raymond.hettinger | 2008-05-29 10:38:23 +0200 (Thu, 29 May 2008) | 1 line Fix two typos. ........ r63788 | facundo.batista | 2008-05-29 18:39:26 +0200 (Thu, 29 May 2008) | 6 lines Fixed the semantic of timeout for socket.create_connection and all the upper level libraries that use it, including urllib2. Added and fixed some tests, and changed docs correspondingly. Thanks to John J Lee for the patch and the pusing, :) ........ r63802 | mark.dickinson | 2008-05-30 04:46:53 +0200 (Fri, 30 May 2008) | 2 lines Fix typo in testSum ........ r63817 | raymond.hettinger | 2008-05-30 20:20:50 +0200 (Fri, 30 May 2008) | 8 lines * Mark intermedidate computes values (hi, lo, yr) as volatile. * Expand comments. * Swap variable names in the sum_exact code so that x and y are consistently chosen as the larger and smaller magnitude values respectively. ........ r63827 | raymond.hettinger | 2008-05-31 05:24:31 +0200 (Sat, 31 May 2008) | 1 line Implement heapq in terms of less-than (to match list.sort()). ........ r63839 | gerhard.haering | 2008-05-31 23:33:27 +0200 (Sat, 31 May 2008) | 2 lines Fixed rowcount for SELECT statements. They're -1 now (again), for better DB-API 2.0 compliance. ........ r63887 | gregory.p.smith | 2008-06-02 06:05:52 +0200 (Mon, 02 Jun 2008) | 4 lines Fix issue 2782: be less strict about the format string type in strftime. Accept unicode and anything else ParseTuple "s#" can deal with. This matches the time.strftime behavior. ........ r63975 | neal.norwitz | 2008-06-06 06:47:01 +0200 (Fri, 06 Jun 2008) | 3 lines Aldo Cortesi confirmed this is still needed for OpenBSD 4.2 and 4.3. (I didn't regen configure, since I don't have a working autoconf.) ........ r63998 | raymond.hettinger | 2008-06-06 23:47:51 +0200 (Fri, 06 Jun 2008) | 1 line Issue 3501: Make heapq support both __le__ and __lt__. ........
2008-06-10 14:40:04 -03:00
(if it is not given, the global default timeout setting is used).
2007-08-15 11:28:22 -03:00
For example, the following calls all create instances that connect to the server
at the same host and port::
2008-05-26 13:32:26 -03:00
>>> h1 = http.client.HTTPConnection('www.cwi.nl')
>>> h2 = http.client.HTTPConnection('www.cwi.nl:80')
>>> h3 = http.client.HTTPConnection('www.cwi.nl', 80)
>>> h3 = http.client.HTTPConnection('www.cwi.nl', 80, timeout=10)
2007-08-15 11:28:22 -03:00
.. class:: HTTPSConnection(host, port=None, key_file=None, cert_file=None, strict=None[, timeout])
2007-08-15 11:28:22 -03:00
A subclass of :class:`HTTPConnection` that uses SSL for communication with
secure servers. Default port is ``443``. *key_file* is the name of a PEM
formatted file that contains your private key. *cert_file* is a PEM formatted
certificate chain file.
.. note::
2007-08-15 11:28:22 -03:00
This does not do any certificate verification.
2007-08-15 11:28:22 -03:00
.. class:: HTTPResponse(sock, debuglevel=0, strict=0, method=None, url=None)
2007-08-15 11:28:22 -03:00
Class whose instances are returned upon successful connection. Not
instantiated directly by user.
2007-08-15 11:28:22 -03:00
The following exceptions are raised as appropriate:
.. exception:: HTTPException
The base class of the other exceptions in this module. It is a subclass of
:exc:`Exception`.
.. exception:: NotConnected
A subclass of :exc:`HTTPException`.
.. exception:: InvalidURL
A subclass of :exc:`HTTPException`, raised if a port is given and is either
non-numeric or empty.
.. exception:: UnknownProtocol
A subclass of :exc:`HTTPException`.
.. exception:: UnknownTransferEncoding
A subclass of :exc:`HTTPException`.
.. exception:: UnimplementedFileMode
A subclass of :exc:`HTTPException`.
.. exception:: IncompleteRead
A subclass of :exc:`HTTPException`.
.. exception:: ImproperConnectionState
A subclass of :exc:`HTTPException`.
.. exception:: CannotSendRequest
A subclass of :exc:`ImproperConnectionState`.
.. exception:: CannotSendHeader
A subclass of :exc:`ImproperConnectionState`.
.. exception:: ResponseNotReady
A subclass of :exc:`ImproperConnectionState`.
.. exception:: BadStatusLine
A subclass of :exc:`HTTPException`. Raised if a server responds with a HTTP
status code that we don't understand.
The constants defined in this module are:
.. data:: HTTP_PORT
The default port for the HTTP protocol (always ``80``).
.. data:: HTTPS_PORT
The default port for the HTTPS protocol (always ``443``).
and also the following constants for integer status codes:
+------------------------------------------+---------+-----------------------------------------------------------------------+
| Constant | Value | Definition |
+==========================================+=========+=======================================================================+
| :const:`CONTINUE` | ``100`` | HTTP/1.1, `RFC 2616, Section |
| | | 10.1.1 |
| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.1.1>`_ |
+------------------------------------------+---------+-----------------------------------------------------------------------+
| :const:`SWITCHING_PROTOCOLS` | ``101`` | HTTP/1.1, `RFC 2616, Section |
| | | 10.1.2 |
| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.1.2>`_ |
+------------------------------------------+---------+-----------------------------------------------------------------------+
| :const:`PROCESSING` | ``102`` | WEBDAV, `RFC 2518, Section 10.1 |
| | | <http://www.webdav.org/specs/rfc2518.html#STATUS_102>`_ |
+------------------------------------------+---------+-----------------------------------------------------------------------+
| :const:`OK` | ``200`` | HTTP/1.1, `RFC 2616, Section |
| | | 10.2.1 |
| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.1>`_ |
+------------------------------------------+---------+-----------------------------------------------------------------------+
| :const:`CREATED` | ``201`` | HTTP/1.1, `RFC 2616, Section |
| | | 10.2.2 |
| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.2>`_ |
+------------------------------------------+---------+-----------------------------------------------------------------------+
| :const:`ACCEPTED` | ``202`` | HTTP/1.1, `RFC 2616, Section |
| | | 10.2.3 |
| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.3>`_ |
+------------------------------------------+---------+-----------------------------------------------------------------------+
| :const:`NON_AUTHORITATIVE_INFORMATION` | ``203`` | HTTP/1.1, `RFC 2616, Section |
| | | 10.2.4 |
| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.4>`_ |
+------------------------------------------+---------+-----------------------------------------------------------------------+
| :const:`NO_CONTENT` | ``204`` | HTTP/1.1, `RFC 2616, Section |
| | | 10.2.5 |
| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.5>`_ |
+------------------------------------------+---------+-----------------------------------------------------------------------+
| :const:`RESET_CONTENT` | ``205`` | HTTP/1.1, `RFC 2616, Section |
| | | 10.2.6 |
| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.6>`_ |
+------------------------------------------+---------+-----------------------------------------------------------------------+
| :const:`PARTIAL_CONTENT` | ``206`` | HTTP/1.1, `RFC 2616, Section |
| | | 10.2.7 |
| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.7>`_ |
+------------------------------------------+---------+-----------------------------------------------------------------------+
| :const:`MULTI_STATUS` | ``207`` | WEBDAV `RFC 2518, Section 10.2 |
| | | <http://www.webdav.org/specs/rfc2518.html#STATUS_207>`_ |
+------------------------------------------+---------+-----------------------------------------------------------------------+
| :const:`IM_USED` | ``226`` | Delta encoding in HTTP, |
| | | :rfc:`3229`, Section 10.4.1 |
+------------------------------------------+---------+-----------------------------------------------------------------------+
| :const:`MULTIPLE_CHOICES` | ``300`` | HTTP/1.1, `RFC 2616, Section |
| | | 10.3.1 |
| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.1>`_ |
+------------------------------------------+---------+-----------------------------------------------------------------------+
| :const:`MOVED_PERMANENTLY` | ``301`` | HTTP/1.1, `RFC 2616, Section |
| | | 10.3.2 |
| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.2>`_ |
+------------------------------------------+---------+-----------------------------------------------------------------------+
| :const:`FOUND` | ``302`` | HTTP/1.1, `RFC 2616, Section |
| | | 10.3.3 |
| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.3>`_ |
+------------------------------------------+---------+-----------------------------------------------------------------------+
| :const:`SEE_OTHER` | ``303`` | HTTP/1.1, `RFC 2616, Section |
| | | 10.3.4 |
| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.4>`_ |
+------------------------------------------+---------+-----------------------------------------------------------------------+
| :const:`NOT_MODIFIED` | ``304`` | HTTP/1.1, `RFC 2616, Section |
| | | 10.3.5 |
| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.5>`_ |
+------------------------------------------+---------+-----------------------------------------------------------------------+
| :const:`USE_PROXY` | ``305`` | HTTP/1.1, `RFC 2616, Section |
| | | 10.3.6 |
| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.6>`_ |
+------------------------------------------+---------+-----------------------------------------------------------------------+
| :const:`TEMPORARY_REDIRECT` | ``307`` | HTTP/1.1, `RFC 2616, Section |
| | | 10.3.8 |
| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.8>`_ |
+------------------------------------------+---------+-----------------------------------------------------------------------+
| :const:`BAD_REQUEST` | ``400`` | HTTP/1.1, `RFC 2616, Section |
| | | 10.4.1 |
| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.1>`_ |
+------------------------------------------+---------+-----------------------------------------------------------------------+
| :const:`UNAUTHORIZED` | ``401`` | HTTP/1.1, `RFC 2616, Section |
| | | 10.4.2 |
| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.2>`_ |
+------------------------------------------+---------+-----------------------------------------------------------------------+
| :const:`PAYMENT_REQUIRED` | ``402`` | HTTP/1.1, `RFC 2616, Section |
| | | 10.4.3 |
| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.3>`_ |
+------------------------------------------+---------+-----------------------------------------------------------------------+
| :const:`FORBIDDEN` | ``403`` | HTTP/1.1, `RFC 2616, Section |
| | | 10.4.4 |
| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.4>`_ |
+------------------------------------------+---------+-----------------------------------------------------------------------+
| :const:`NOT_FOUND` | ``404`` | HTTP/1.1, `RFC 2616, Section |
| | | 10.4.5 |
| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.5>`_ |
+------------------------------------------+---------+-----------------------------------------------------------------------+
| :const:`METHOD_NOT_ALLOWED` | ``405`` | HTTP/1.1, `RFC 2616, Section |
| | | 10.4.6 |
| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.6>`_ |
+------------------------------------------+---------+-----------------------------------------------------------------------+
| :const:`NOT_ACCEPTABLE` | ``406`` | HTTP/1.1, `RFC 2616, Section |
| | | 10.4.7 |
| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.7>`_ |
+------------------------------------------+---------+-----------------------------------------------------------------------+
| :const:`PROXY_AUTHENTICATION_REQUIRED` | ``407`` | HTTP/1.1, `RFC 2616, Section |
| | | 10.4.8 |
| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.8>`_ |
+------------------------------------------+---------+-----------------------------------------------------------------------+
| :const:`REQUEST_TIMEOUT` | ``408`` | HTTP/1.1, `RFC 2616, Section |
| | | 10.4.9 |
| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.9>`_ |
+------------------------------------------+---------+-----------------------------------------------------------------------+
| :const:`CONFLICT` | ``409`` | HTTP/1.1, `RFC 2616, Section |
| | | 10.4.10 |
| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.10>`_ |
+------------------------------------------+---------+-----------------------------------------------------------------------+
| :const:`GONE` | ``410`` | HTTP/1.1, `RFC 2616, Section |
| | | 10.4.11 |
| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.11>`_ |
+------------------------------------------+---------+-----------------------------------------------------------------------+
| :const:`LENGTH_REQUIRED` | ``411`` | HTTP/1.1, `RFC 2616, Section |
| | | 10.4.12 |
| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.12>`_ |
+------------------------------------------+---------+-----------------------------------------------------------------------+
| :const:`PRECONDITION_FAILED` | ``412`` | HTTP/1.1, `RFC 2616, Section |
| | | 10.4.13 |
| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.13>`_ |
+------------------------------------------+---------+-----------------------------------------------------------------------+
| :const:`REQUEST_ENTITY_TOO_LARGE` | ``413`` | HTTP/1.1, `RFC 2616, Section |
| | | 10.4.14 |
| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.14>`_ |
+------------------------------------------+---------+-----------------------------------------------------------------------+
| :const:`REQUEST_URI_TOO_LONG` | ``414`` | HTTP/1.1, `RFC 2616, Section |
| | | 10.4.15 |
| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.15>`_ |
+------------------------------------------+---------+-----------------------------------------------------------------------+
| :const:`UNSUPPORTED_MEDIA_TYPE` | ``415`` | HTTP/1.1, `RFC 2616, Section |
| | | 10.4.16 |
| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.16>`_ |
+------------------------------------------+---------+-----------------------------------------------------------------------+
| :const:`REQUESTED_RANGE_NOT_SATISFIABLE` | ``416`` | HTTP/1.1, `RFC 2616, Section |
| | | 10.4.17 |
| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.17>`_ |
+------------------------------------------+---------+-----------------------------------------------------------------------+
| :const:`EXPECTATION_FAILED` | ``417`` | HTTP/1.1, `RFC 2616, Section |
| | | 10.4.18 |
| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.18>`_ |
+------------------------------------------+---------+-----------------------------------------------------------------------+
| :const:`UNPROCESSABLE_ENTITY` | ``422`` | WEBDAV, `RFC 2518, Section 10.3 |
| | | <http://www.webdav.org/specs/rfc2518.html#STATUS_422>`_ |
+------------------------------------------+---------+-----------------------------------------------------------------------+
| :const:`LOCKED` | ``423`` | WEBDAV `RFC 2518, Section 10.4 |
| | | <http://www.webdav.org/specs/rfc2518.html#STATUS_423>`_ |
+------------------------------------------+---------+-----------------------------------------------------------------------+
| :const:`FAILED_DEPENDENCY` | ``424`` | WEBDAV, `RFC 2518, Section 10.5 |
| | | <http://www.webdav.org/specs/rfc2518.html#STATUS_424>`_ |
+------------------------------------------+---------+-----------------------------------------------------------------------+
| :const:`UPGRADE_REQUIRED` | ``426`` | HTTP Upgrade to TLS, |
| | | :rfc:`2817`, Section 6 |
+------------------------------------------+---------+-----------------------------------------------------------------------+
| :const:`INTERNAL_SERVER_ERROR` | ``500`` | HTTP/1.1, `RFC 2616, Section |
| | | 10.5.1 |
| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.1>`_ |
+------------------------------------------+---------+-----------------------------------------------------------------------+
| :const:`NOT_IMPLEMENTED` | ``501`` | HTTP/1.1, `RFC 2616, Section |
| | | 10.5.2 |
| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.2>`_ |
+------------------------------------------+---------+-----------------------------------------------------------------------+
| :const:`BAD_GATEWAY` | ``502`` | HTTP/1.1 `RFC 2616, Section |
| | | 10.5.3 |
| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.3>`_ |
+------------------------------------------+---------+-----------------------------------------------------------------------+
| :const:`SERVICE_UNAVAILABLE` | ``503`` | HTTP/1.1, `RFC 2616, Section |
| | | 10.5.4 |
| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.4>`_ |
+------------------------------------------+---------+-----------------------------------------------------------------------+
| :const:`GATEWAY_TIMEOUT` | ``504`` | HTTP/1.1 `RFC 2616, Section |
| | | 10.5.5 |
| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.5>`_ |
+------------------------------------------+---------+-----------------------------------------------------------------------+
| :const:`HTTP_VERSION_NOT_SUPPORTED` | ``505`` | HTTP/1.1, `RFC 2616, Section |
| | | 10.5.6 |
| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.6>`_ |
+------------------------------------------+---------+-----------------------------------------------------------------------+
| :const:`INSUFFICIENT_STORAGE` | ``507`` | WEBDAV, `RFC 2518, Section 10.6 |
| | | <http://www.webdav.org/specs/rfc2518.html#STATUS_507>`_ |
+------------------------------------------+---------+-----------------------------------------------------------------------+
| :const:`NOT_EXTENDED` | ``510`` | An HTTP Extension Framework, |
| | | :rfc:`2774`, Section 7 |
+------------------------------------------+---------+-----------------------------------------------------------------------+
.. data:: responses
This dictionary maps the HTTP 1.1 status codes to the W3C names.
2008-05-26 13:32:26 -03:00
Example: ``http.client.responses[http.client.NOT_FOUND]`` is ``'Not Found'``.
2007-08-15 11:28:22 -03:00
.. _httpconnection-objects:
HTTPConnection Objects
----------------------
:class:`HTTPConnection` instances have the following methods:
.. method:: HTTPConnection.request(method, url, body=None, headers={})
2007-08-15 11:28:22 -03:00
This will send a request to the server using the HTTP request
method *method* and the selector *url*. If the *body* argument is
present, it should be string or bytes object of data to send after
the headers are finished. Strings are encoded as ISO-8859-1, the
default charset for HTTP. To use other encodings, pass a bytes
object. The Content-Length header is set to the length of the
string.
The *body* may also be an open file object, in which case the
contents of the file is sent; this file object should support
``fileno()`` and ``read()`` methods. The header Content-Length is
automatically set to the length of the file as reported by
stat.
The *headers* argument should be a mapping of extra HTTP
headers to send with the request.
2007-08-15 11:28:22 -03:00
.. method:: HTTPConnection.getresponse()
Should be called after a request is sent to get the response from the server.
Returns an :class:`HTTPResponse` instance.
.. note::
Note that you must have read the whole response before you can send a new
request to the server.
.. method:: HTTPConnection.set_debuglevel(level)
Set the debugging level (the amount of debugging output printed). The default
debug level is ``0``, meaning no debugging output is printed.
.. method:: HTTPConnection.connect()
Connect to the server specified when the object was created.
.. method:: HTTPConnection.close()
Close the connection to the server.
As an alternative to using the :meth:`request` method described above, you can
also send your request step by step, by using the four functions below.
.. method:: HTTPConnection.putrequest(request, selector, skip_host=False, skip_accept_encoding=False)
2007-08-15 11:28:22 -03:00
This should be the first call after the connection to the server has been made.
It sends a line to the server consisting of the *request* string, the *selector*
string, and the HTTP version (``HTTP/1.1``). To disable automatic sending of
``Host:`` or ``Accept-Encoding:`` headers (for example to accept additional
content encodings), specify *skip_host* or *skip_accept_encoding* with non-False
values.
.. method:: HTTPConnection.putheader(header, argument[, ...])
Send an :rfc:`822`\ -style header to the server. It sends a line to the server
consisting of the header, a colon and a space, and the first argument. If more
arguments are given, continuation lines are sent, each consisting of a tab and
an argument.
.. method:: HTTPConnection.endheaders()
Send a blank line to the server, signalling the end of the headers.
.. method:: HTTPConnection.send(data)
Send data to the server. This should be used directly only after the
:meth:`endheaders` method has been called and before :meth:`getresponse` is
called.
.. _httpresponse-objects:
HTTPResponse Objects
--------------------
An :class:`HTTPResponse` instance wraps the HTTP response from the
server. It provides access to the request headers and the entity
body. The response is an iterable object and can be used in a with
statement.
2007-08-15 11:28:22 -03:00
.. method:: HTTPResponse.read([amt])
Reads and returns the response body, or up to the next *amt* bytes.
.. method:: HTTPResponse.getheader(name, default=None)
2007-08-15 11:28:22 -03:00
Get the contents of the header *name*, or *default* if there is no matching
header.
.. method:: HTTPResponse.getheaders()
Return a list of (header, value) tuples.
.. attribute:: HTTPResponse.msg
A :class:`http.client.HTTPMessage` instance containing the response
headers. :class:`http.client.HTTPMessage` is a subclass of
:class:`email.message.Message`.
2007-08-15 11:28:22 -03:00
.. attribute:: HTTPResponse.version
HTTP protocol version used by server. 10 for HTTP/1.0, 11 for HTTP/1.1.
.. attribute:: HTTPResponse.status
Status code returned by server.
.. attribute:: HTTPResponse.reason
Reason phrase returned by server.
.. attribute:: HTTPResponse.debuglevel
A debugging hook. If `debuglevel` is greater than zero, messages
will be printed to stdout as the response is read and parsed.
2007-08-15 11:28:22 -03:00
Examples
--------
Here is an example session that uses the ``GET`` method::
2008-05-26 13:32:26 -03:00
>>> import http.client
>>> conn = http.client.HTTPConnection("www.python.org")
2007-08-15 11:28:22 -03:00
>>> conn.request("GET", "/index.html")
>>> r1 = conn.getresponse()
>>> print(r1.status, r1.reason)
2007-08-15 11:28:22 -03:00
200 OK
>>> data1 = r1.read()
>>> conn.request("GET", "/parrot.spam")
>>> r2 = conn.getresponse()
>>> print(r2.status, r2.reason)
2007-08-15 11:28:22 -03:00
404 Not Found
>>> data2 = r2.read()
>>> conn.close()
Here is an example session that shows how to ``POST`` requests::
>>> import http.client, urllib.parse
>>> params = urllib.parse.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})
2007-08-15 11:28:22 -03:00
>>> headers = {"Content-type": "application/x-www-form-urlencoded",
... "Accept": "text/plain"}
2008-05-26 13:32:26 -03:00
>>> conn = http.client.HTTPConnection("musi-cal.mojam.com:80")
2007-08-15 11:28:22 -03:00
>>> conn.request("POST", "/cgi-bin/query", params, headers)
>>> response = conn.getresponse()
>>> print(response.status, response.reason)
2007-08-15 11:28:22 -03:00
200 OK
>>> data = response.read()
>>> conn.close()
.. _httpmessage-objects:
HTTPMessage Objects
-------------------
2009-04-01 21:24:00 -03:00
An :class:`http.client.HTTPMessage` instance holds the headers from an HTTP
response. It is implemented using the :class:`email.message.Message` class.
2009-04-01 21:24:00 -03:00
.. XXX Define the methods that clients can depend upon between versions.