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
|
|
|
|
2008-06-23 01:41:59 -03:00
|
|
|
.. index:: module: urllib.request
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2011-01-27 16:38:46 -04:00
|
|
|
**Source code:** :source:`Lib/http/client.py`
|
|
|
|
|
|
|
|
--------------
|
|
|
|
|
2007-08-15 11:28:22 -03:00
|
|
|
This module defines classes which implement the client side of the HTTP and
|
2008-06-23 01:41:59 -03:00
|
|
|
HTTPS protocols. It is normally not used directly --- the module
|
2008-06-23 08:23:31 -03:00
|
|
|
:mod:`urllib.request` uses it to handle URLs that use HTTP and HTTPS.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
2010-10-13 07:39:21 -03:00
|
|
|
HTTPS support is only available if Python was compiled with SSL support
|
|
|
|
(through the :mod:`ssl` module).
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
The module provides the following classes:
|
|
|
|
|
|
|
|
|
2013-03-18 18:11:41 -03:00
|
|
|
.. class:: HTTPConnection(host, port=None[, timeout], \
|
2012-09-14 00:32:35 -03:00
|
|
|
source_address=None)
|
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
|
2010-12-17 13:35:56 -04:00
|
|
|
used. If the optional *timeout* parameter is given, blocking
|
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
|
|
|
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).
|
2011-01-29 20:39:00 -04:00
|
|
|
The optional *source_address* parameter may be a tuple of a (host, port)
|
2010-01-02 23:28:29 -04:00
|
|
|
to use as the source address the HTTP connection is made from.
|
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::
|
|
|
|
|
2014-03-22 14:19:11 -03:00
|
|
|
>>> h1 = http.client.HTTPConnection('www.python.org')
|
|
|
|
>>> h2 = http.client.HTTPConnection('www.python.org:80')
|
|
|
|
>>> h3 = http.client.HTTPConnection('www.python.org', 80)
|
|
|
|
>>> h4 = http.client.HTTPConnection('www.python.org', 80, timeout=10)
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2010-01-02 23:28:29 -04:00
|
|
|
.. versionchanged:: 3.2
|
|
|
|
*source_address* was added.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2013-03-19 05:22:56 -03:00
|
|
|
.. versionchanged:: 3.4
|
2014-01-03 14:04:25 -04:00
|
|
|
The *strict* parameter was removed. HTTP 0.9-style "Simple Responses" are
|
|
|
|
not longer supported.
|
2010-12-17 13:35:56 -04:00
|
|
|
|
2010-01-02 23:28:29 -04:00
|
|
|
|
2012-09-14 00:32:35 -03:00
|
|
|
.. class:: HTTPSConnection(host, port=None, key_file=None, \
|
2013-03-18 18:11:41 -03:00
|
|
|
cert_file=None[, timeout], \
|
2012-09-14 00:32:35 -03:00
|
|
|
source_address=None, *, context=None, \
|
|
|
|
check_hostname=None)
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
A subclass of :class:`HTTPConnection` that uses SSL for communication with
|
2010-10-13 07:36:15 -03:00
|
|
|
secure servers. Default port is ``443``. If *context* is specified, it
|
|
|
|
must be a :class:`ssl.SSLContext` instance describing the various SSL
|
2014-03-22 14:19:11 -03:00
|
|
|
options.
|
2010-10-13 07:36:15 -03:00
|
|
|
|
|
|
|
*key_file* and *cert_file* are deprecated, please use
|
2014-03-22 14:19:11 -03:00
|
|
|
:meth:`ssl.SSLContext.load_cert_chain` instead, or let
|
|
|
|
:func:`ssl.create_default_context` select the system's trusted CA
|
|
|
|
certificates for you.
|
2010-10-13 07:36:15 -03:00
|
|
|
|
2014-03-22 14:19:11 -03:00
|
|
|
The recommended way to connect to HTTPS hosts on the Internet is as
|
|
|
|
follows::
|
2010-10-13 07:36:15 -03:00
|
|
|
|
2014-03-22 14:19:11 -03:00
|
|
|
context = ssl.create_default_context()
|
|
|
|
h = client.HTTPSConnection('www.python.org', 443, context=context)
|
|
|
|
|
|
|
|
Please read :ref:`ssl-security` for more information on best practices.
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
If *context* is specified and has a :attr:`~ssl.SSLContext.verify_mode`
|
|
|
|
of either :data:`~ssl.CERT_OPTIONAL` or :data:`~ssl.CERT_REQUIRED`, then
|
|
|
|
by default *host* is matched against the host name(s) allowed by the
|
|
|
|
server's certificate. If you want to change that behaviour, you can
|
|
|
|
explicitly set *check_hostname* to False.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2010-01-02 23:28:29 -04:00
|
|
|
.. versionchanged:: 3.2
|
2010-10-13 07:36:15 -03:00
|
|
|
*source_address*, *context* and *check_hostname* were added.
|
2010-01-02 23:28:29 -04:00
|
|
|
|
2010-10-22 15:19:07 -03:00
|
|
|
.. versionchanged:: 3.2
|
|
|
|
This class now supports HTTPS virtual hosts if possible (that is,
|
|
|
|
if :data:`ssl.HAS_SNI` is true).
|
|
|
|
|
2013-03-19 05:22:56 -03:00
|
|
|
.. versionchanged:: 3.4
|
2014-01-03 14:04:25 -04:00
|
|
|
The *strict* parameter was removed. HTTP 0.9-style "Simple Responses" are
|
|
|
|
no longer supported.
|
2010-12-17 13:35:56 -04:00
|
|
|
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2013-03-18 18:11:41 -03:00
|
|
|
.. class:: HTTPResponse(sock, debuglevel=0, method=None, url=None)
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2009-03-31 11:40:19 -03:00
|
|
|
Class whose instances are returned upon successful connection. Not
|
|
|
|
instantiated directly by user.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2013-03-19 05:22:56 -03:00
|
|
|
.. versionchanged:: 3.4
|
2014-01-03 14:04:25 -04:00
|
|
|
The *strict* parameter was removed. HTTP 0.9 style "Simple Responses" are
|
|
|
|
no longer supported.
|
2010-12-17 13:35:56 -04:00
|
|
|
|
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.
|
|
|
|
|
|
|
|
|
2013-10-27 03:34:48 -03:00
|
|
|
The constants defined in this module are:
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
.. 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 |
|
|
|
|
+------------------------------------------+---------+-----------------------------------------------------------------------+
|
2012-05-16 04:51:07 -03:00
|
|
|
| :const:`PRECONDITION_REQUIRED` | ``428`` | Additional HTTP Status Codes, |
|
|
|
|
| | | :rfc:`6585`, Section 3 |
|
|
|
|
+------------------------------------------+---------+-----------------------------------------------------------------------+
|
|
|
|
| :const:`TOO_MANY_REQUESTS` | ``429`` | Additional HTTP Status Codes, |
|
|
|
|
| | | :rfc:`6585`, Section 4 |
|
|
|
|
+------------------------------------------+---------+-----------------------------------------------------------------------+
|
|
|
|
| :const:`REQUEST_HEADER_FIELDS_TOO_LARGE` | ``431`` | Additional HTTP Status Codes, |
|
|
|
|
| | | :rfc:`6585`, Section 5 |
|
|
|
|
+------------------------------------------+---------+-----------------------------------------------------------------------+
|
2007-08-15 11:28:22 -03:00
|
|
|
| :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 |
|
|
|
|
+------------------------------------------+---------+-----------------------------------------------------------------------+
|
2012-05-16 04:51:07 -03:00
|
|
|
| :const:`NETWORK_AUTHENTICATION_REQUIRED` | ``511`` | Additional HTTP Status Codes, |
|
|
|
|
| | | :rfc:`6585`, Section 6 |
|
|
|
|
+------------------------------------------+---------+-----------------------------------------------------------------------+
|
|
|
|
|
2012-05-21 15:28:58 -03:00
|
|
|
.. versionchanged:: 3.3
|
|
|
|
Added codes ``428``, ``429``, ``431`` and ``511`` from :rfc:`6585`.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
|
|
|
.. 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:
|
|
|
|
|
|
|
|
|
2009-05-17 10:00:36 -03:00
|
|
|
.. method:: HTTPConnection.request(method, url, body=None, headers={})
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2009-03-27 17:24:34 -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.
|
|
|
|
|
2010-09-15 08:11:28 -03:00
|
|
|
The *body* may also be an open :term:`file object`, in which case the
|
2010-12-19 06:49:52 -04:00
|
|
|
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 *body* argument may also be
|
2011-01-29 20:39:00 -04:00
|
|
|
an iterable and Content-Length header should be explicitly provided when the
|
2010-12-19 06:49:52 -04:00
|
|
|
body is an iterable.
|
2009-03-27 17:24:34 -03:00
|
|
|
|
|
|
|
The *headers* argument should be a mapping of extra HTTP
|
|
|
|
headers to send with the request.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2010-12-19 06:49:52 -04:00
|
|
|
.. versionadded:: 3.2
|
2010-12-19 08:33:52 -04:00
|
|
|
*body* can now be an iterable.
|
2010-12-19 06:49:52 -04:00
|
|
|
|
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)
|
|
|
|
|
2010-12-14 22:19:14 -04:00
|
|
|
Set the debugging level. The default debug level is ``0``, meaning no
|
|
|
|
debugging output is printed. Any value greater than ``0`` will cause all
|
|
|
|
currently defined debug output to be printed to stdout. The ``debuglevel``
|
|
|
|
is passed to any new :class:`HTTPResponse` objects that are created.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2009-10-01 17:20:09 -03:00
|
|
|
.. versionadded:: 3.1
|
Merged revisions 72506,72525-72526,72551,72558,72616,72654-72655,72689,72745,72750,72802,72812,72822,72824,72826-72827,72833,72876,72890,72923,72946,73026,73042,73045,73047,73065,73068-73069 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r72506 | vinay.sajip | 2009-05-09 07:07:17 -0500 (Sat, 09 May 2009) | 1 line
Issue #5971: StreamHandler.handleError now swallows IOErrors which occur when trying to print a traceback.
........
r72525 | benjamin.peterson | 2009-05-09 20:38:02 -0500 (Sat, 09 May 2009) | 1 line
close file explicitly
........
r72526 | benjamin.peterson | 2009-05-09 21:29:00 -0500 (Sat, 09 May 2009) | 1 line
make sure files are closed using the with statement
........
r72551 | benjamin.peterson | 2009-05-10 09:16:47 -0500 (Sun, 10 May 2009) | 1 line
use isinstance
........
r72558 | benjamin.peterson | 2009-05-10 18:52:09 -0500 (Sun, 10 May 2009) | 1 line
sys.setdefaultencoding() strikes me as a bad example
........
r72616 | benjamin.peterson | 2009-05-13 19:33:10 -0500 (Wed, 13 May 2009) | 1 line
importlib.import_module is better these days
........
r72654 | benjamin.peterson | 2009-05-14 17:37:49 -0500 (Thu, 14 May 2009) | 1 line
prevent refleaks from threads
........
r72655 | benjamin.peterson | 2009-05-14 17:40:34 -0500 (Thu, 14 May 2009) | 1 line
a useful decorator for cleaning up threads
........
r72689 | benjamin.peterson | 2009-05-16 13:44:34 -0500 (Sat, 16 May 2009) | 1 line
use skipTest()
........
r72745 | benjamin.peterson | 2009-05-17 09:16:29 -0500 (Sun, 17 May 2009) | 1 line
ignore .rst files in sphinx its self
........
r72750 | benjamin.peterson | 2009-05-17 11:59:27 -0500 (Sun, 17 May 2009) | 1 line
chop off slash
........
r72802 | georg.brandl | 2009-05-20 13:35:27 -0500 (Wed, 20 May 2009) | 1 line
#6051: refer to email examples for better way to construct email messages.
........
r72812 | michael.foord | 2009-05-21 17:57:02 -0500 (Thu, 21 May 2009) | 1 line
Rename TestCase._result to _resultForDoCleanups to avoid potential clashes in TestCase subclasses. Issue 6072.
........
r72822 | georg.brandl | 2009-05-22 04:33:25 -0500 (Fri, 22 May 2009) | 1 line
#6084: fix example.
........
r72824 | georg.brandl | 2009-05-22 04:43:17 -0500 (Fri, 22 May 2009) | 1 line
Fix references to file-related functions and methods (os.* vs file.*).
........
r72826 | georg.brandl | 2009-05-22 04:49:42 -0500 (Fri, 22 May 2009) | 1 line
Fix confusing wording.
........
r72827 | georg.brandl | 2009-05-22 04:50:30 -0500 (Fri, 22 May 2009) | 1 line
s/use/call/
........
r72833 | georg.brandl | 2009-05-22 12:00:17 -0500 (Fri, 22 May 2009) | 1 line
#6078: _warnings is a builtin module and has no standard init_warnings function.
........
r72876 | benjamin.peterson | 2009-05-23 15:59:09 -0500 (Sat, 23 May 2009) | 1 line
remove mention of old ctypes version
........
r72890 | gregory.p.smith | 2009-05-24 13:00:13 -0500 (Sun, 24 May 2009) | 2 lines
add a versionadded tag for set_tunnel
........
r72923 | michael.foord | 2009-05-25 15:36:56 -0500 (Mon, 25 May 2009) | 1 line
Make assertSequenceEqual error messages less cryptic, particularly for nested sequences.
........
r72946 | ronald.oussoren | 2009-05-26 13:44:48 -0500 (Tue, 26 May 2009) | 2 lines
Fixes issue 6110
........
r73026 | r.david.murray | 2009-05-29 14:30:27 -0500 (Fri, 29 May 2009) | 3 lines
Issue 6141: document that the first item of args is still the
command name even when executable is specified.
........
r73042 | benjamin.peterson | 2009-05-29 22:10:52 -0500 (Fri, 29 May 2009) | 1 line
no fdatasync on macos
........
r73045 | georg.brandl | 2009-05-30 02:26:04 -0500 (Sat, 30 May 2009) | 1 line
#6146: fix markup bug.
........
r73047 | georg.brandl | 2009-05-30 05:33:23 -0500 (Sat, 30 May 2009) | 1 line
Fix some more small markup problems.
........
r73065 | antoine.pitrou | 2009-05-30 16:39:25 -0500 (Sat, 30 May 2009) | 3 lines
The test for #5330 wasn't correct.
........
r73068 | antoine.pitrou | 2009-05-30 16:45:40 -0500 (Sat, 30 May 2009) | 3 lines
Update ACKS
........
r73069 | benjamin.peterson | 2009-05-30 19:42:42 -0500 (Sat, 30 May 2009) | 1 line
fix signature
........
2009-06-01 19:42:33 -03:00
|
|
|
|
2010-08-17 12:07:14 -03:00
|
|
|
|
2009-12-20 03:31:21 -04:00
|
|
|
.. method:: HTTPConnection.set_tunnel(host, port=None, headers=None)
|
2009-07-25 01:24:38 -03:00
|
|
|
|
2014-03-16 17:55:39 -03:00
|
|
|
Set the host and the port for HTTP Connect Tunnelling. This allows running
|
|
|
|
the connection through a proxy server.
|
2009-07-25 01:24:38 -03:00
|
|
|
|
2014-03-16 17:55:39 -03:00
|
|
|
The host and port arguments specify the endpoint of the tunneled connection
|
|
|
|
(i.e. the address included in the CONNECT request, *not* the address of the
|
|
|
|
proxy server).
|
|
|
|
|
|
|
|
The headers argument should be a mapping of extra HTTP headers to send with
|
|
|
|
the CONNECT request.
|
|
|
|
|
|
|
|
For example, to tunnel through a HTTPS proxy server running locally on port
|
|
|
|
8080, we would pass the address of the proxy to the :class:`HTTPSConnection`
|
|
|
|
constructor, and the address of the host that we eventually want to reach to
|
|
|
|
the :meth:`~HTTPConnection.set_tunnel` method::
|
|
|
|
|
|
|
|
>>> import http.client
|
|
|
|
>>> conn = http.client.HTTPSConnection("localhost", 8080)
|
|
|
|
>>> conn.set_tunnel("www.python.org")
|
|
|
|
>>> conn.request("HEAD","/index.html")
|
2009-12-20 03:31:21 -04:00
|
|
|
|
2009-07-25 01:27:38 -03:00
|
|
|
.. versionadded:: 3.2
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2010-08-17 12:07:14 -03:00
|
|
|
|
2007-08-15 11:28:22 -03:00
|
|
|
.. 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.
|
|
|
|
|
|
|
|
|
2009-05-17 10:00:36 -03:00
|
|
|
.. 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.
|
|
|
|
|
|
|
|
|
2011-10-02 20:27:06 -03:00
|
|
|
.. method:: HTTPConnection.endheaders(message_body=None)
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2011-10-02 20:27:06 -03:00
|
|
|
Send a blank line to the server, signalling the end of the headers. The
|
2011-10-05 12:26:49 -03:00
|
|
|
optional *message_body* argument can be used to pass a message body
|
|
|
|
associated with the request. The message body will be sent in the same
|
|
|
|
packet as the message headers if it is string, otherwise it is sent in a
|
|
|
|
separate packet.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
.. 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
|
|
|
|
--------------------
|
|
|
|
|
2009-03-31 11:40:19 -03:00
|
|
|
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.
|
|
|
|
|
2011-12-06 17:33:57 -04:00
|
|
|
.. method:: HTTPResponse.readinto(b)
|
|
|
|
|
|
|
|
Reads up to the next len(b) bytes of the response body into the buffer *b*.
|
|
|
|
Returns the number of bytes read.
|
|
|
|
|
|
|
|
.. versionadded:: 3.3
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2009-05-17 10:00:36 -03:00
|
|
|
.. method:: HTTPResponse.getheader(name, default=None)
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2010-08-02 14:09:02 -03:00
|
|
|
Return the value of the header *name*, or *default* if there is no header
|
|
|
|
matching *name*. If there is more than one header with the name *name*,
|
|
|
|
return all of the values joined by ', '. If 'default' is any iterable other
|
|
|
|
than a single string, its elements are similarly returned joined by commas.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
|
|
|
.. method:: HTTPResponse.getheaders()
|
|
|
|
|
|
|
|
Return a list of (header, value) tuples.
|
|
|
|
|
2010-09-20 22:57:43 -03:00
|
|
|
.. method:: HTTPResponse.fileno()
|
|
|
|
|
|
|
|
Return the ``fileno`` of the underlying socket.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
.. attribute:: HTTPResponse.msg
|
|
|
|
|
2009-03-31 11:40:19 -03:00
|
|
|
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.
|
|
|
|
|
|
|
|
|
2009-03-31 11:40:19 -03:00
|
|
|
.. attribute:: HTTPResponse.debuglevel
|
|
|
|
|
2010-03-12 06:06:40 -04:00
|
|
|
A debugging hook. If :attr:`debuglevel` is greater than zero, messages
|
2009-03-31 11:40:19 -03:00
|
|
|
will be printed to stdout as the response is read and parsed.
|
|
|
|
|
2011-06-19 20:56:49 -03:00
|
|
|
.. attribute:: HTTPResponse.closed
|
|
|
|
|
2013-11-29 06:17:13 -04:00
|
|
|
Is ``True`` if the stream is closed.
|
2009-03-31 11:40:19 -03:00
|
|
|
|
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()
|
2007-09-04 04:15:32 -03:00
|
|
|
>>> print(r1.status, r1.reason)
|
2007-08-15 11:28:22 -03:00
|
|
|
200 OK
|
2011-06-19 20:56:49 -03:00
|
|
|
>>> data1 = r1.read() # This will return entire content.
|
|
|
|
>>> # The following example demonstrates reading data in chunks.
|
|
|
|
>>> conn.request("GET", "/index.html")
|
|
|
|
>>> r1 = conn.getresponse()
|
|
|
|
>>> while not r1.closed:
|
|
|
|
... print(r1.read(200)) # 200 bytes
|
|
|
|
b'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"...
|
|
|
|
...
|
|
|
|
>>> # Example of an invalid request
|
2007-08-15 11:28:22 -03:00
|
|
|
>>> conn.request("GET", "/parrot.spam")
|
|
|
|
>>> r2 = conn.getresponse()
|
2007-09-04 04:15:32 -03:00
|
|
|
>>> print(r2.status, r2.reason)
|
2007-08-15 11:28:22 -03:00
|
|
|
404 Not Found
|
|
|
|
>>> data2 = r2.read()
|
|
|
|
>>> conn.close()
|
|
|
|
|
2010-05-11 22:36:11 -03:00
|
|
|
Here is an example session that uses the ``HEAD`` method. Note that the
|
|
|
|
``HEAD`` method never returns any data. ::
|
2010-04-28 14:39:48 -03:00
|
|
|
|
|
|
|
>>> import http.client
|
|
|
|
>>> conn = http.client.HTTPConnection("www.python.org")
|
|
|
|
>>> conn.request("HEAD","/index.html")
|
|
|
|
>>> res = conn.getresponse()
|
|
|
|
>>> print(res.status, res.reason)
|
|
|
|
200 OK
|
|
|
|
>>> data = res.read()
|
|
|
|
>>> print(len(data))
|
|
|
|
0
|
|
|
|
>>> data == b''
|
|
|
|
True
|
|
|
|
|
2007-08-15 11:28:22 -03:00
|
|
|
Here is an example session that shows how to ``POST`` requests::
|
|
|
|
|
2008-06-23 01:41:59 -03:00
|
|
|
>>> import http.client, urllib.parse
|
2011-07-20 10:56:24 -03:00
|
|
|
>>> params = urllib.parse.urlencode({'@number': 12524, '@type': 'issue', '@action': 'show'})
|
2007-08-15 11:28:22 -03:00
|
|
|
>>> headers = {"Content-type": "application/x-www-form-urlencoded",
|
|
|
|
... "Accept": "text/plain"}
|
2011-07-20 10:56:24 -03:00
|
|
|
>>> conn = http.client.HTTPConnection("bugs.python.org")
|
|
|
|
>>> conn.request("POST", "", params, headers)
|
2007-08-15 11:28:22 -03:00
|
|
|
>>> response = conn.getresponse()
|
2007-09-04 04:15:32 -03:00
|
|
|
>>> print(response.status, response.reason)
|
2011-07-20 10:56:24 -03:00
|
|
|
302 Found
|
2007-08-15 11:28:22 -03:00
|
|
|
>>> data = response.read()
|
2011-07-20 10:56:24 -03:00
|
|
|
>>> data
|
|
|
|
b'Redirecting to <a href="http://bugs.python.org/issue12524">http://bugs.python.org/issue12524</a>'
|
2007-08-15 11:28:22 -03:00
|
|
|
>>> conn.close()
|
|
|
|
|
2013-03-13 17:38:33 -03:00
|
|
|
Client side ``HTTP PUT`` requests are very similar to ``POST`` requests. The
|
|
|
|
difference lies only the server side where HTTP server will allow resources to
|
2013-03-13 17:42:47 -03:00
|
|
|
be created via ``PUT`` request. It should be noted that custom HTTP methods
|
|
|
|
+are also handled in :class:`urllib.request.Request` by sending the appropriate
|
|
|
|
+method attribute.Here is an example session that shows how to do ``PUT``
|
|
|
|
request using http.client::
|
2013-03-13 17:38:33 -03:00
|
|
|
|
|
|
|
>>> # This creates an HTTP message
|
|
|
|
>>> # with the content of BODY as the enclosed representation
|
2014-04-17 00:33:02 -03:00
|
|
|
>>> # for the resource http://localhost:8080/file
|
2013-03-13 17:38:33 -03:00
|
|
|
...
|
|
|
|
>>> import http.client
|
|
|
|
>>> BODY = "***filecontents***"
|
|
|
|
>>> conn = http.client.HTTPConnection("localhost", 8080)
|
|
|
|
>>> conn.request("PUT", "/file", BODY)
|
|
|
|
>>> response = conn.getresponse()
|
2013-10-06 07:42:18 -03:00
|
|
|
>>> print(response.status, response.reason)
|
2013-03-13 17:38:33 -03:00
|
|
|
200, OK
|
2009-03-31 11:40:19 -03:00
|
|
|
|
|
|
|
.. _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-03-31 11:40:19 -03:00
|
|
|
|
2009-04-01 21:24:00 -03:00
|
|
|
.. XXX Define the methods that clients can depend upon between versions.
|