blob: 807f6852486c989fa81271cbced137f40dae7b0b [file] [log] [blame]
Georg Brandl24420152008-05-26 16:32:26 +00001:mod:`http.client` --- HTTP protocol client
2===========================================
Georg Brandl116aa622007-08-15 14:28:22 +00003
Georg Brandl24420152008-05-26 16:32:26 +00004.. module:: http.client
Georg Brandl116aa622007-08-15 14:28:22 +00005 :synopsis: HTTP and HTTPS protocol client (requires sockets).
6
7
8.. index::
9 pair: HTTP; protocol
Georg Brandl24420152008-05-26 16:32:26 +000010 single: HTTP; http.client (standard module)
Georg Brandl116aa622007-08-15 14:28:22 +000011
Senthil Kumaranaca8fd72008-06-23 04:41:59 +000012.. index:: module: urllib.request
Georg Brandl116aa622007-08-15 14:28:22 +000013
Raymond Hettinger469271d2011-01-27 20:38:46 +000014**Source code:** :source:`Lib/http/client.py`
15
16--------------
17
Georg Brandl116aa622007-08-15 14:28:22 +000018This module defines classes which implement the client side of the HTTP and
Senthil Kumaranaca8fd72008-06-23 04:41:59 +000019HTTPS protocols. It is normally not used directly --- the module
Georg Brandl0f7ede42008-06-23 11:23:31 +000020:mod:`urllib.request` uses it to handle URLs that use HTTP and HTTPS.
Georg Brandl116aa622007-08-15 14:28:22 +000021
Benjamin Peterson6de708f2015-04-20 18:18:14 -040022.. seealso::
23
24 The `Requests package <http://requests.readthedocs.org/>`_
25 is recommended for a higher-level http client interface.
26
Georg Brandl116aa622007-08-15 14:28:22 +000027.. note::
28
Antoine Pitrou1ab19ca2010-10-13 10:39:21 +000029 HTTPS support is only available if Python was compiled with SSL support
30 (through the :mod:`ssl` module).
Georg Brandl116aa622007-08-15 14:28:22 +000031
Georg Brandl116aa622007-08-15 14:28:22 +000032The module provides the following classes:
33
34
Senthil Kumaran052ddb02013-03-18 14:11:41 -070035.. class:: HTTPConnection(host, port=None[, timeout], \
Ezio Melottie0add762012-09-14 06:32:35 +030036 source_address=None)
Georg Brandl116aa622007-08-15 14:28:22 +000037
38 An :class:`HTTPConnection` instance represents one transaction with an HTTP
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +000039 server. It should be instantiated passing it a host and optional port
40 number. If no port number is passed, the port is extracted from the host
41 string if it has the form ``host:port``, else the default HTTP port (80) is
Antoine Pitrou988dbd72010-12-17 17:35:56 +000042 used. If the optional *timeout* parameter is given, blocking
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +000043 operations (like connection attempts) will timeout after that many seconds
Georg Brandlf78e02b2008-06-10 17:40:04 +000044 (if it is not given, the global default timeout setting is used).
Raymond Hettinger519c3082011-01-30 00:39:00 +000045 The optional *source_address* parameter may be a tuple of a (host, port)
Gregory P. Smithb4066372010-01-03 03:28:29 +000046 to use as the source address the HTTP connection is made from.
Georg Brandl116aa622007-08-15 14:28:22 +000047
48 For example, the following calls all create instances that connect to the server
49 at the same host and port::
50
Antoine Pitrouc5e075f2014-03-22 18:19:11 +010051 >>> h1 = http.client.HTTPConnection('www.python.org')
52 >>> h2 = http.client.HTTPConnection('www.python.org:80')
53 >>> h3 = http.client.HTTPConnection('www.python.org', 80)
54 >>> h4 = http.client.HTTPConnection('www.python.org', 80, timeout=10)
Georg Brandl116aa622007-08-15 14:28:22 +000055
Gregory P. Smithb4066372010-01-03 03:28:29 +000056 .. versionchanged:: 3.2
57 *source_address* was added.
Georg Brandl116aa622007-08-15 14:28:22 +000058
Senthil Kumaranaced69f2013-03-19 01:22:56 -070059 .. versionchanged:: 3.4
R David Murray00569362014-01-03 13:04:25 -050060 The *strict* parameter was removed. HTTP 0.9-style "Simple Responses" are
61 not longer supported.
Gregory P. Smithb4066372010-01-03 03:28:29 +000062
Antoine Pitrou988dbd72010-12-17 17:35:56 +000063
Ezio Melottie0add762012-09-14 06:32:35 +030064.. class:: HTTPSConnection(host, port=None, key_file=None, \
Senthil Kumaran052ddb02013-03-18 14:11:41 -070065 cert_file=None[, timeout], \
Ezio Melottie0add762012-09-14 06:32:35 +030066 source_address=None, *, context=None, \
67 check_hostname=None)
Georg Brandl116aa622007-08-15 14:28:22 +000068
69 A subclass of :class:`HTTPConnection` that uses SSL for communication with
Antoine Pitrou803e6d62010-10-13 10:36:15 +000070 secure servers. Default port is ``443``. If *context* is specified, it
71 must be a :class:`ssl.SSLContext` instance describing the various SSL
Antoine Pitrouc5e075f2014-03-22 18:19:11 +010072 options.
Georg Brandl116aa622007-08-15 14:28:22 +000073
Antoine Pitrou803e6d62010-10-13 10:36:15 +000074 *key_file* and *cert_file* are deprecated, please use
Antoine Pitrouc5e075f2014-03-22 18:19:11 +010075 :meth:`ssl.SSLContext.load_cert_chain` instead, or let
76 :func:`ssl.create_default_context` select the system's trusted CA
Benjamin Petersona090f012014-12-07 13:18:25 -050077 certificates for you. The *check_hostname* parameter is also deprecated; the
Benjamin Petersone3b743c2014-12-07 17:26:38 -050078 :attr:`ssl.SSLContext.check_hostname` attribute of *context* should be used
Benjamin Petersona090f012014-12-07 13:18:25 -050079 instead.
Antoine Pitrou803e6d62010-10-13 10:36:15 +000080
Antoine Pitrouc5e075f2014-03-22 18:19:11 +010081 Please read :ref:`ssl-security` for more information on best practices.
82
Gregory P. Smithb4066372010-01-03 03:28:29 +000083 .. versionchanged:: 3.2
Antoine Pitrou803e6d62010-10-13 10:36:15 +000084 *source_address*, *context* and *check_hostname* were added.
Gregory P. Smithb4066372010-01-03 03:28:29 +000085
Antoine Pitroud5323212010-10-22 18:19:07 +000086 .. versionchanged:: 3.2
87 This class now supports HTTPS virtual hosts if possible (that is,
88 if :data:`ssl.HAS_SNI` is true).
89
Senthil Kumaranaced69f2013-03-19 01:22:56 -070090 .. versionchanged:: 3.4
R David Murray00569362014-01-03 13:04:25 -050091 The *strict* parameter was removed. HTTP 0.9-style "Simple Responses" are
92 no longer supported.
Georg Brandl116aa622007-08-15 14:28:22 +000093
Benjamin Peterson4ffb0752014-11-03 14:29:33 -050094 .. versionchanged:: 3.4.3
95 This class now performs all the necessary certificate and hostname checks
96 by default. To revert to the previous, unverified, behavior
97 :func:`ssl._create_unverified_context` can be passed to the *context*
98 parameter.
99
Georg Brandl116aa622007-08-15 14:28:22 +0000100
Senthil Kumaran052ddb02013-03-18 14:11:41 -0700101.. class:: HTTPResponse(sock, debuglevel=0, method=None, url=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000102
Jeremy Hylton1052f892009-03-31 14:40:19 +0000103 Class whose instances are returned upon successful connection. Not
104 instantiated directly by user.
Georg Brandl116aa622007-08-15 14:28:22 +0000105
Senthil Kumaranaced69f2013-03-19 01:22:56 -0700106 .. versionchanged:: 3.4
R David Murray00569362014-01-03 13:04:25 -0500107 The *strict* parameter was removed. HTTP 0.9 style "Simple Responses" are
108 no longer supported.
Antoine Pitrou988dbd72010-12-17 17:35:56 +0000109
Georg Brandl116aa622007-08-15 14:28:22 +0000110
111The following exceptions are raised as appropriate:
112
113
114.. exception:: HTTPException
115
116 The base class of the other exceptions in this module. It is a subclass of
117 :exc:`Exception`.
118
Georg Brandl116aa622007-08-15 14:28:22 +0000119
120.. exception:: NotConnected
121
122 A subclass of :exc:`HTTPException`.
123
Georg Brandl116aa622007-08-15 14:28:22 +0000124
125.. exception:: InvalidURL
126
127 A subclass of :exc:`HTTPException`, raised if a port is given and is either
128 non-numeric or empty.
129
Georg Brandl116aa622007-08-15 14:28:22 +0000130
131.. exception:: UnknownProtocol
132
133 A subclass of :exc:`HTTPException`.
134
Georg Brandl116aa622007-08-15 14:28:22 +0000135
136.. exception:: UnknownTransferEncoding
137
138 A subclass of :exc:`HTTPException`.
139
Georg Brandl116aa622007-08-15 14:28:22 +0000140
141.. exception:: UnimplementedFileMode
142
143 A subclass of :exc:`HTTPException`.
144
Georg Brandl116aa622007-08-15 14:28:22 +0000145
146.. exception:: IncompleteRead
147
148 A subclass of :exc:`HTTPException`.
149
Georg Brandl116aa622007-08-15 14:28:22 +0000150
151.. exception:: ImproperConnectionState
152
153 A subclass of :exc:`HTTPException`.
154
Georg Brandl116aa622007-08-15 14:28:22 +0000155
156.. exception:: CannotSendRequest
157
158 A subclass of :exc:`ImproperConnectionState`.
159
Georg Brandl116aa622007-08-15 14:28:22 +0000160
161.. exception:: CannotSendHeader
162
163 A subclass of :exc:`ImproperConnectionState`.
164
Georg Brandl116aa622007-08-15 14:28:22 +0000165
166.. exception:: ResponseNotReady
167
168 A subclass of :exc:`ImproperConnectionState`.
169
Georg Brandl116aa622007-08-15 14:28:22 +0000170
171.. exception:: BadStatusLine
172
173 A subclass of :exc:`HTTPException`. Raised if a server responds with a HTTP
174 status code that we don't understand.
175
Georg Brandl116aa622007-08-15 14:28:22 +0000176
Berker Peksagbabc6882015-02-20 09:39:38 +0200177.. exception:: LineTooLong
178
179 A subclass of :exc:`HTTPException`. Raised if an excessively long line
180 is received in the HTTP protocol from the server.
181
182
Georg Brandlbf3f8eb2013-10-27 07:34:48 +0100183The constants defined in this module are:
Georg Brandl116aa622007-08-15 14:28:22 +0000184
185.. data:: HTTP_PORT
186
187 The default port for the HTTP protocol (always ``80``).
188
189
190.. data:: HTTPS_PORT
191
192 The default port for the HTTPS protocol (always ``443``).
193
194and also the following constants for integer status codes:
195
196+------------------------------------------+---------+-----------------------------------------------------------------------+
197| Constant | Value | Definition |
198+==========================================+=========+=======================================================================+
199| :const:`CONTINUE` | ``100`` | HTTP/1.1, `RFC 2616, Section |
200| | | 10.1.1 |
201| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.1.1>`_ |
202+------------------------------------------+---------+-----------------------------------------------------------------------+
203| :const:`SWITCHING_PROTOCOLS` | ``101`` | HTTP/1.1, `RFC 2616, Section |
204| | | 10.1.2 |
205| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.1.2>`_ |
206+------------------------------------------+---------+-----------------------------------------------------------------------+
207| :const:`PROCESSING` | ``102`` | WEBDAV, `RFC 2518, Section 10.1 |
208| | | <http://www.webdav.org/specs/rfc2518.html#STATUS_102>`_ |
209+------------------------------------------+---------+-----------------------------------------------------------------------+
210| :const:`OK` | ``200`` | HTTP/1.1, `RFC 2616, Section |
211| | | 10.2.1 |
212| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.1>`_ |
213+------------------------------------------+---------+-----------------------------------------------------------------------+
214| :const:`CREATED` | ``201`` | HTTP/1.1, `RFC 2616, Section |
215| | | 10.2.2 |
216| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.2>`_ |
217+------------------------------------------+---------+-----------------------------------------------------------------------+
218| :const:`ACCEPTED` | ``202`` | HTTP/1.1, `RFC 2616, Section |
219| | | 10.2.3 |
220| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.3>`_ |
221+------------------------------------------+---------+-----------------------------------------------------------------------+
222| :const:`NON_AUTHORITATIVE_INFORMATION` | ``203`` | HTTP/1.1, `RFC 2616, Section |
223| | | 10.2.4 |
224| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.4>`_ |
225+------------------------------------------+---------+-----------------------------------------------------------------------+
226| :const:`NO_CONTENT` | ``204`` | HTTP/1.1, `RFC 2616, Section |
227| | | 10.2.5 |
228| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.5>`_ |
229+------------------------------------------+---------+-----------------------------------------------------------------------+
230| :const:`RESET_CONTENT` | ``205`` | HTTP/1.1, `RFC 2616, Section |
231| | | 10.2.6 |
232| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.6>`_ |
233+------------------------------------------+---------+-----------------------------------------------------------------------+
234| :const:`PARTIAL_CONTENT` | ``206`` | HTTP/1.1, `RFC 2616, Section |
235| | | 10.2.7 |
236| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.7>`_ |
237+------------------------------------------+---------+-----------------------------------------------------------------------+
238| :const:`MULTI_STATUS` | ``207`` | WEBDAV `RFC 2518, Section 10.2 |
239| | | <http://www.webdav.org/specs/rfc2518.html#STATUS_207>`_ |
240+------------------------------------------+---------+-----------------------------------------------------------------------+
241| :const:`IM_USED` | ``226`` | Delta encoding in HTTP, |
242| | | :rfc:`3229`, Section 10.4.1 |
243+------------------------------------------+---------+-----------------------------------------------------------------------+
244| :const:`MULTIPLE_CHOICES` | ``300`` | HTTP/1.1, `RFC 2616, Section |
245| | | 10.3.1 |
246| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.1>`_ |
247+------------------------------------------+---------+-----------------------------------------------------------------------+
248| :const:`MOVED_PERMANENTLY` | ``301`` | HTTP/1.1, `RFC 2616, Section |
249| | | 10.3.2 |
250| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.2>`_ |
251+------------------------------------------+---------+-----------------------------------------------------------------------+
252| :const:`FOUND` | ``302`` | HTTP/1.1, `RFC 2616, Section |
253| | | 10.3.3 |
254| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.3>`_ |
255+------------------------------------------+---------+-----------------------------------------------------------------------+
256| :const:`SEE_OTHER` | ``303`` | HTTP/1.1, `RFC 2616, Section |
257| | | 10.3.4 |
258| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.4>`_ |
259+------------------------------------------+---------+-----------------------------------------------------------------------+
260| :const:`NOT_MODIFIED` | ``304`` | HTTP/1.1, `RFC 2616, Section |
261| | | 10.3.5 |
262| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.5>`_ |
263+------------------------------------------+---------+-----------------------------------------------------------------------+
264| :const:`USE_PROXY` | ``305`` | HTTP/1.1, `RFC 2616, Section |
265| | | 10.3.6 |
266| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.6>`_ |
267+------------------------------------------+---------+-----------------------------------------------------------------------+
268| :const:`TEMPORARY_REDIRECT` | ``307`` | HTTP/1.1, `RFC 2616, Section |
269| | | 10.3.8 |
270| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.8>`_ |
271+------------------------------------------+---------+-----------------------------------------------------------------------+
272| :const:`BAD_REQUEST` | ``400`` | HTTP/1.1, `RFC 2616, Section |
273| | | 10.4.1 |
274| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.1>`_ |
275+------------------------------------------+---------+-----------------------------------------------------------------------+
276| :const:`UNAUTHORIZED` | ``401`` | HTTP/1.1, `RFC 2616, Section |
277| | | 10.4.2 |
278| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.2>`_ |
279+------------------------------------------+---------+-----------------------------------------------------------------------+
280| :const:`PAYMENT_REQUIRED` | ``402`` | HTTP/1.1, `RFC 2616, Section |
281| | | 10.4.3 |
282| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.3>`_ |
283+------------------------------------------+---------+-----------------------------------------------------------------------+
284| :const:`FORBIDDEN` | ``403`` | HTTP/1.1, `RFC 2616, Section |
285| | | 10.4.4 |
286| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.4>`_ |
287+------------------------------------------+---------+-----------------------------------------------------------------------+
288| :const:`NOT_FOUND` | ``404`` | HTTP/1.1, `RFC 2616, Section |
289| | | 10.4.5 |
290| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.5>`_ |
291+------------------------------------------+---------+-----------------------------------------------------------------------+
292| :const:`METHOD_NOT_ALLOWED` | ``405`` | HTTP/1.1, `RFC 2616, Section |
293| | | 10.4.6 |
294| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.6>`_ |
295+------------------------------------------+---------+-----------------------------------------------------------------------+
296| :const:`NOT_ACCEPTABLE` | ``406`` | HTTP/1.1, `RFC 2616, Section |
297| | | 10.4.7 |
298| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.7>`_ |
299+------------------------------------------+---------+-----------------------------------------------------------------------+
300| :const:`PROXY_AUTHENTICATION_REQUIRED` | ``407`` | HTTP/1.1, `RFC 2616, Section |
301| | | 10.4.8 |
302| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.8>`_ |
303+------------------------------------------+---------+-----------------------------------------------------------------------+
304| :const:`REQUEST_TIMEOUT` | ``408`` | HTTP/1.1, `RFC 2616, Section |
305| | | 10.4.9 |
306| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.9>`_ |
307+------------------------------------------+---------+-----------------------------------------------------------------------+
308| :const:`CONFLICT` | ``409`` | HTTP/1.1, `RFC 2616, Section |
309| | | 10.4.10 |
310| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.10>`_ |
311+------------------------------------------+---------+-----------------------------------------------------------------------+
312| :const:`GONE` | ``410`` | HTTP/1.1, `RFC 2616, Section |
313| | | 10.4.11 |
314| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.11>`_ |
315+------------------------------------------+---------+-----------------------------------------------------------------------+
316| :const:`LENGTH_REQUIRED` | ``411`` | HTTP/1.1, `RFC 2616, Section |
317| | | 10.4.12 |
318| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.12>`_ |
319+------------------------------------------+---------+-----------------------------------------------------------------------+
320| :const:`PRECONDITION_FAILED` | ``412`` | HTTP/1.1, `RFC 2616, Section |
321| | | 10.4.13 |
322| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.13>`_ |
323+------------------------------------------+---------+-----------------------------------------------------------------------+
324| :const:`REQUEST_ENTITY_TOO_LARGE` | ``413`` | HTTP/1.1, `RFC 2616, Section |
325| | | 10.4.14 |
326| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.14>`_ |
327+------------------------------------------+---------+-----------------------------------------------------------------------+
328| :const:`REQUEST_URI_TOO_LONG` | ``414`` | HTTP/1.1, `RFC 2616, Section |
329| | | 10.4.15 |
330| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.15>`_ |
331+------------------------------------------+---------+-----------------------------------------------------------------------+
332| :const:`UNSUPPORTED_MEDIA_TYPE` | ``415`` | HTTP/1.1, `RFC 2616, Section |
333| | | 10.4.16 |
334| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.16>`_ |
335+------------------------------------------+---------+-----------------------------------------------------------------------+
336| :const:`REQUESTED_RANGE_NOT_SATISFIABLE` | ``416`` | HTTP/1.1, `RFC 2616, Section |
337| | | 10.4.17 |
338| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.17>`_ |
339+------------------------------------------+---------+-----------------------------------------------------------------------+
340| :const:`EXPECTATION_FAILED` | ``417`` | HTTP/1.1, `RFC 2616, Section |
341| | | 10.4.18 |
342| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.18>`_ |
343+------------------------------------------+---------+-----------------------------------------------------------------------+
344| :const:`UNPROCESSABLE_ENTITY` | ``422`` | WEBDAV, `RFC 2518, Section 10.3 |
345| | | <http://www.webdav.org/specs/rfc2518.html#STATUS_422>`_ |
346+------------------------------------------+---------+-----------------------------------------------------------------------+
347| :const:`LOCKED` | ``423`` | WEBDAV `RFC 2518, Section 10.4 |
348| | | <http://www.webdav.org/specs/rfc2518.html#STATUS_423>`_ |
349+------------------------------------------+---------+-----------------------------------------------------------------------+
350| :const:`FAILED_DEPENDENCY` | ``424`` | WEBDAV, `RFC 2518, Section 10.5 |
351| | | <http://www.webdav.org/specs/rfc2518.html#STATUS_424>`_ |
352+------------------------------------------+---------+-----------------------------------------------------------------------+
353| :const:`UPGRADE_REQUIRED` | ``426`` | HTTP Upgrade to TLS, |
354| | | :rfc:`2817`, Section 6 |
355+------------------------------------------+---------+-----------------------------------------------------------------------+
Hynek Schlawack51b2ed52012-05-16 09:51:07 +0200356| :const:`PRECONDITION_REQUIRED` | ``428`` | Additional HTTP Status Codes, |
357| | | :rfc:`6585`, Section 3 |
358+------------------------------------------+---------+-----------------------------------------------------------------------+
359| :const:`TOO_MANY_REQUESTS` | ``429`` | Additional HTTP Status Codes, |
360| | | :rfc:`6585`, Section 4 |
361+------------------------------------------+---------+-----------------------------------------------------------------------+
362| :const:`REQUEST_HEADER_FIELDS_TOO_LARGE` | ``431`` | Additional HTTP Status Codes, |
363| | | :rfc:`6585`, Section 5 |
364+------------------------------------------+---------+-----------------------------------------------------------------------+
Georg Brandl116aa622007-08-15 14:28:22 +0000365| :const:`INTERNAL_SERVER_ERROR` | ``500`` | HTTP/1.1, `RFC 2616, Section |
366| | | 10.5.1 |
367| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.1>`_ |
368+------------------------------------------+---------+-----------------------------------------------------------------------+
369| :const:`NOT_IMPLEMENTED` | ``501`` | HTTP/1.1, `RFC 2616, Section |
370| | | 10.5.2 |
371| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.2>`_ |
372+------------------------------------------+---------+-----------------------------------------------------------------------+
373| :const:`BAD_GATEWAY` | ``502`` | HTTP/1.1 `RFC 2616, Section |
374| | | 10.5.3 |
375| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.3>`_ |
376+------------------------------------------+---------+-----------------------------------------------------------------------+
377| :const:`SERVICE_UNAVAILABLE` | ``503`` | HTTP/1.1, `RFC 2616, Section |
378| | | 10.5.4 |
379| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.4>`_ |
380+------------------------------------------+---------+-----------------------------------------------------------------------+
381| :const:`GATEWAY_TIMEOUT` | ``504`` | HTTP/1.1 `RFC 2616, Section |
382| | | 10.5.5 |
383| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.5>`_ |
384+------------------------------------------+---------+-----------------------------------------------------------------------+
385| :const:`HTTP_VERSION_NOT_SUPPORTED` | ``505`` | HTTP/1.1, `RFC 2616, Section |
386| | | 10.5.6 |
387| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.6>`_ |
388+------------------------------------------+---------+-----------------------------------------------------------------------+
389| :const:`INSUFFICIENT_STORAGE` | ``507`` | WEBDAV, `RFC 2518, Section 10.6 |
390| | | <http://www.webdav.org/specs/rfc2518.html#STATUS_507>`_ |
391+------------------------------------------+---------+-----------------------------------------------------------------------+
392| :const:`NOT_EXTENDED` | ``510`` | An HTTP Extension Framework, |
393| | | :rfc:`2774`, Section 7 |
394+------------------------------------------+---------+-----------------------------------------------------------------------+
Hynek Schlawack51b2ed52012-05-16 09:51:07 +0200395| :const:`NETWORK_AUTHENTICATION_REQUIRED` | ``511`` | Additional HTTP Status Codes, |
396| | | :rfc:`6585`, Section 6 |
397+------------------------------------------+---------+-----------------------------------------------------------------------+
398
Georg Brandl945a3ad2012-05-21 20:28:58 +0200399.. versionchanged:: 3.3
400 Added codes ``428``, ``429``, ``431`` and ``511`` from :rfc:`6585`.
Georg Brandl116aa622007-08-15 14:28:22 +0000401
402
403.. data:: responses
404
405 This dictionary maps the HTTP 1.1 status codes to the W3C names.
406
Georg Brandl24420152008-05-26 16:32:26 +0000407 Example: ``http.client.responses[http.client.NOT_FOUND]`` is ``'Not Found'``.
Georg Brandl116aa622007-08-15 14:28:22 +0000408
Georg Brandl116aa622007-08-15 14:28:22 +0000409
410.. _httpconnection-objects:
411
412HTTPConnection Objects
413----------------------
414
415:class:`HTTPConnection` instances have the following methods:
416
417
Georg Brandl036490d2009-05-17 13:00:36 +0000418.. method:: HTTPConnection.request(method, url, body=None, headers={})
Georg Brandl116aa622007-08-15 14:28:22 +0000419
Jeremy Hylton236654b2009-03-27 20:24:34 +0000420 This will send a request to the server using the HTTP request
R David Murraybeed8402015-03-22 15:18:23 -0400421 method *method* and the selector *url*.
Georg Brandl116aa622007-08-15 14:28:22 +0000422
R David Murraybeed8402015-03-22 15:18:23 -0400423 If *body* is specified, the specified data is sent after the headers are
424 finished. It may be a string, a :term:`bytes-like object`, an open
425 :term:`file object`, or an iterable of :term:`bytes-like object`\s. If
426 *body* is a string, it is encoded as ISO-8851-1, the default for HTTP. If
427 it is a bytes-like object the bytes are sent as is. If it is a :term:`file
428 object`, the contents of the file is sent; this file object should support
429 at least the ``read()`` method. If the file object has a ``mode``
430 attribute, the data returned by the ``read()`` method will be encoded as
431 ISO-8851-1 unless the ``mode`` attribute contains the substring ``b``,
432 otherwise the data returned by ``read()`` is sent as is. If *body* is an
433 iterable, the elements of the iterable are sent as is until the iterable is
434 exhausted.
Jeremy Hylton236654b2009-03-27 20:24:34 +0000435
436 The *headers* argument should be a mapping of extra HTTP
437 headers to send with the request.
Georg Brandl116aa622007-08-15 14:28:22 +0000438
R David Murraybeed8402015-03-22 15:18:23 -0400439 If *headers* does not contain a Content-Length item, one is added
440 automatically if possible. If *body* is ``None``, the Content-Length header
441 is set to ``0`` for methods that expect a body (``PUT``, ``POST``, and
442 ``PATCH``). If *body* is a string or bytes object, the Content-Length
443 header is set to its length. If *body* is a :term:`file object` and it
444 works to call :func:`~os.fstat` on the result of its ``fileno()`` method,
445 then the Content-Length header is set to the ``st_size`` reported by the
446 ``fstat`` call. Otherwise no Content-Length header is added.
447
Senthil Kumaran7bc0d872010-12-19 10:49:52 +0000448 .. versionadded:: 3.2
Georg Brandl09a7df82010-12-19 12:33:52 +0000449 *body* can now be an iterable.
Senthil Kumaran7bc0d872010-12-19 10:49:52 +0000450
Georg Brandl116aa622007-08-15 14:28:22 +0000451.. method:: HTTPConnection.getresponse()
452
453 Should be called after a request is sent to get the response from the server.
454 Returns an :class:`HTTPResponse` instance.
455
456 .. note::
457
458 Note that you must have read the whole response before you can send a new
459 request to the server.
460
461
462.. method:: HTTPConnection.set_debuglevel(level)
463
R. David Murrayd89bc3f2010-12-15 02:19:14 +0000464 Set the debugging level. The default debug level is ``0``, meaning no
465 debugging output is printed. Any value greater than ``0`` will cause all
466 currently defined debug output to be printed to stdout. The ``debuglevel``
467 is passed to any new :class:`HTTPResponse` objects that are created.
Georg Brandl116aa622007-08-15 14:28:22 +0000468
Mark Dickinson574b1d62009-10-01 20:20:09 +0000469 .. versionadded:: 3.1
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000470
Georg Brandl67b21b72010-08-17 15:07:14 +0000471
Senthil Kumaran7e5229c2009-12-20 07:31:21 +0000472.. method:: HTTPConnection.set_tunnel(host, port=None, headers=None)
Senthil Kumaran97f0c6b2009-07-25 04:24:38 +0000473
Benjamin Petersona48d9ea2014-03-16 15:55:39 -0500474 Set the host and the port for HTTP Connect Tunnelling. This allows running
475 the connection through a proxy server.
Senthil Kumaran97f0c6b2009-07-25 04:24:38 +0000476
Benjamin Petersona48d9ea2014-03-16 15:55:39 -0500477 The host and port arguments specify the endpoint of the tunneled connection
478 (i.e. the address included in the CONNECT request, *not* the address of the
479 proxy server).
480
481 The headers argument should be a mapping of extra HTTP headers to send with
482 the CONNECT request.
483
484 For example, to tunnel through a HTTPS proxy server running locally on port
485 8080, we would pass the address of the proxy to the :class:`HTTPSConnection`
486 constructor, and the address of the host that we eventually want to reach to
487 the :meth:`~HTTPConnection.set_tunnel` method::
488
489 >>> import http.client
490 >>> conn = http.client.HTTPSConnection("localhost", 8080)
491 >>> conn.set_tunnel("www.python.org")
492 >>> conn.request("HEAD","/index.html")
Senthil Kumaran7e5229c2009-12-20 07:31:21 +0000493
Senthil Kumaran2e910fd2009-07-25 04:27:38 +0000494 .. versionadded:: 3.2
Georg Brandl116aa622007-08-15 14:28:22 +0000495
Georg Brandl67b21b72010-08-17 15:07:14 +0000496
Georg Brandl116aa622007-08-15 14:28:22 +0000497.. method:: HTTPConnection.connect()
498
499 Connect to the server specified when the object was created.
500
501
502.. method:: HTTPConnection.close()
503
504 Close the connection to the server.
505
506As an alternative to using the :meth:`request` method described above, you can
507also send your request step by step, by using the four functions below.
508
509
Georg Brandl036490d2009-05-17 13:00:36 +0000510.. method:: HTTPConnection.putrequest(request, selector, skip_host=False, skip_accept_encoding=False)
Georg Brandl116aa622007-08-15 14:28:22 +0000511
512 This should be the first call after the connection to the server has been made.
513 It sends a line to the server consisting of the *request* string, the *selector*
514 string, and the HTTP version (``HTTP/1.1``). To disable automatic sending of
515 ``Host:`` or ``Accept-Encoding:`` headers (for example to accept additional
516 content encodings), specify *skip_host* or *skip_accept_encoding* with non-False
517 values.
518
Georg Brandl116aa622007-08-15 14:28:22 +0000519
520.. method:: HTTPConnection.putheader(header, argument[, ...])
521
522 Send an :rfc:`822`\ -style header to the server. It sends a line to the server
523 consisting of the header, a colon and a space, and the first argument. If more
524 arguments are given, continuation lines are sent, each consisting of a tab and
525 an argument.
526
527
Senthil Kumaran5d0de3f2011-10-03 07:27:06 +0800528.. method:: HTTPConnection.endheaders(message_body=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000529
Senthil Kumaran5d0de3f2011-10-03 07:27:06 +0800530 Send a blank line to the server, signalling the end of the headers. The
Senthil Kumaranad87fa62011-10-05 23:26:49 +0800531 optional *message_body* argument can be used to pass a message body
532 associated with the request. The message body will be sent in the same
533 packet as the message headers if it is string, otherwise it is sent in a
534 separate packet.
Georg Brandl116aa622007-08-15 14:28:22 +0000535
536.. method:: HTTPConnection.send(data)
537
538 Send data to the server. This should be used directly only after the
539 :meth:`endheaders` method has been called and before :meth:`getresponse` is
540 called.
541
542
543.. _httpresponse-objects:
544
545HTTPResponse Objects
546--------------------
547
Jeremy Hylton1052f892009-03-31 14:40:19 +0000548An :class:`HTTPResponse` instance wraps the HTTP response from the
549server. It provides access to the request headers and the entity
550body. The response is an iterable object and can be used in a with
551statement.
Georg Brandl116aa622007-08-15 14:28:22 +0000552
553
554.. method:: HTTPResponse.read([amt])
555
556 Reads and returns the response body, or up to the next *amt* bytes.
557
Antoine Pitrou38d96432011-12-06 22:33:57 +0100558.. method:: HTTPResponse.readinto(b)
559
560 Reads up to the next len(b) bytes of the response body into the buffer *b*.
561 Returns the number of bytes read.
562
563 .. versionadded:: 3.3
Georg Brandl116aa622007-08-15 14:28:22 +0000564
Georg Brandl036490d2009-05-17 13:00:36 +0000565.. method:: HTTPResponse.getheader(name, default=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000566
Senthil Kumaran790f8312010-08-02 17:09:02 +0000567 Return the value of the header *name*, or *default* if there is no header
568 matching *name*. If there is more than one header with the name *name*,
569 return all of the values joined by ', '. If 'default' is any iterable other
570 than a single string, its elements are similarly returned joined by commas.
Georg Brandl116aa622007-08-15 14:28:22 +0000571
572
573.. method:: HTTPResponse.getheaders()
574
575 Return a list of (header, value) tuples.
576
Senthil Kumaranceff5662010-09-21 01:57:43 +0000577.. method:: HTTPResponse.fileno()
578
579 Return the ``fileno`` of the underlying socket.
Georg Brandl116aa622007-08-15 14:28:22 +0000580
581.. attribute:: HTTPResponse.msg
582
Jeremy Hylton1052f892009-03-31 14:40:19 +0000583 A :class:`http.client.HTTPMessage` instance containing the response
584 headers. :class:`http.client.HTTPMessage` is a subclass of
585 :class:`email.message.Message`.
Georg Brandl116aa622007-08-15 14:28:22 +0000586
587
588.. attribute:: HTTPResponse.version
589
590 HTTP protocol version used by server. 10 for HTTP/1.0, 11 for HTTP/1.1.
591
592
593.. attribute:: HTTPResponse.status
594
595 Status code returned by server.
596
597
598.. attribute:: HTTPResponse.reason
599
600 Reason phrase returned by server.
601
602
Jeremy Hylton1052f892009-03-31 14:40:19 +0000603.. attribute:: HTTPResponse.debuglevel
604
Georg Brandlef871f62010-03-12 10:06:40 +0000605 A debugging hook. If :attr:`debuglevel` is greater than zero, messages
Jeremy Hylton1052f892009-03-31 14:40:19 +0000606 will be printed to stdout as the response is read and parsed.
607
Senthil Kumarance9b5962011-06-19 16:56:49 -0700608.. attribute:: HTTPResponse.closed
609
Serhiy Storchakafbc1c262013-11-29 12:17:13 +0200610 Is ``True`` if the stream is closed.
Jeremy Hylton1052f892009-03-31 14:40:19 +0000611
Georg Brandl116aa622007-08-15 14:28:22 +0000612Examples
613--------
614
615Here is an example session that uses the ``GET`` method::
616
Georg Brandl24420152008-05-26 16:32:26 +0000617 >>> import http.client
Benjamin Petersonac87ed72015-05-03 12:59:09 -0400618 >>> conn = http.client.HTTPSConnection("www.python.org")
619 >>> conn.request("GET", "/")
Georg Brandl116aa622007-08-15 14:28:22 +0000620 >>> r1 = conn.getresponse()
Georg Brandl6911e3c2007-09-04 07:15:32 +0000621 >>> print(r1.status, r1.reason)
Georg Brandl116aa622007-08-15 14:28:22 +0000622 200 OK
Senthil Kumarance9b5962011-06-19 16:56:49 -0700623 >>> data1 = r1.read() # This will return entire content.
624 >>> # The following example demonstrates reading data in chunks.
Benjamin Petersonac87ed72015-05-03 12:59:09 -0400625 >>> conn.request("GET", "/")
Senthil Kumarance9b5962011-06-19 16:56:49 -0700626 >>> r1 = conn.getresponse()
627 >>> while not r1.closed:
628 ... print(r1.read(200)) # 200 bytes
Benjamin Petersonac87ed72015-05-03 12:59:09 -0400629 b'<!doctype html>\n<!--[if"...
Senthil Kumarance9b5962011-06-19 16:56:49 -0700630 ...
631 >>> # Example of an invalid request
Georg Brandl116aa622007-08-15 14:28:22 +0000632 >>> conn.request("GET", "/parrot.spam")
633 >>> r2 = conn.getresponse()
Georg Brandl6911e3c2007-09-04 07:15:32 +0000634 >>> print(r2.status, r2.reason)
Georg Brandl116aa622007-08-15 14:28:22 +0000635 404 Not Found
636 >>> data2 = r2.read()
637 >>> conn.close()
638
Fred Drake1587e3d2010-05-12 01:36:11 +0000639Here is an example session that uses the ``HEAD`` method. Note that the
640``HEAD`` method never returns any data. ::
Senthil Kumaran71fb6c82010-04-28 17:39:48 +0000641
642 >>> import http.client
Benjamin Petersonac87ed72015-05-03 12:59:09 -0400643 >>> conn = http.client.HTTPSConnection("www.python.org")
644 >>> conn.request("HEAD", "/")
Senthil Kumaran71fb6c82010-04-28 17:39:48 +0000645 >>> res = conn.getresponse()
646 >>> print(res.status, res.reason)
647 200 OK
648 >>> data = res.read()
649 >>> print(len(data))
650 0
651 >>> data == b''
652 True
653
Georg Brandl116aa622007-08-15 14:28:22 +0000654Here is an example session that shows how to ``POST`` requests::
655
Senthil Kumaranaca8fd72008-06-23 04:41:59 +0000656 >>> import http.client, urllib.parse
Senthil Kumaran96c84a42011-07-20 21:56:24 +0800657 >>> params = urllib.parse.urlencode({'@number': 12524, '@type': 'issue', '@action': 'show'})
Georg Brandl116aa622007-08-15 14:28:22 +0000658 >>> headers = {"Content-type": "application/x-www-form-urlencoded",
659 ... "Accept": "text/plain"}
Senthil Kumaran96c84a42011-07-20 21:56:24 +0800660 >>> conn = http.client.HTTPConnection("bugs.python.org")
661 >>> conn.request("POST", "", params, headers)
Georg Brandl116aa622007-08-15 14:28:22 +0000662 >>> response = conn.getresponse()
Georg Brandl6911e3c2007-09-04 07:15:32 +0000663 >>> print(response.status, response.reason)
Senthil Kumaran96c84a42011-07-20 21:56:24 +0800664 302 Found
Georg Brandl116aa622007-08-15 14:28:22 +0000665 >>> data = response.read()
Senthil Kumaran96c84a42011-07-20 21:56:24 +0800666 >>> data
667 b'Redirecting to <a href="http://bugs.python.org/issue12524">http://bugs.python.org/issue12524</a>'
Georg Brandl116aa622007-08-15 14:28:22 +0000668 >>> conn.close()
669
Senthil Kumaranb5fe2472013-03-13 13:38:33 -0700670Client side ``HTTP PUT`` requests are very similar to ``POST`` requests. The
671difference lies only the server side where HTTP server will allow resources to
Senthil Kumarane66cc812013-03-13 13:42:47 -0700672be created via ``PUT`` request. It should be noted that custom HTTP methods
673+are also handled in :class:`urllib.request.Request` by sending the appropriate
674+method attribute.Here is an example session that shows how to do ``PUT``
675request using http.client::
Senthil Kumaranb5fe2472013-03-13 13:38:33 -0700676
677 >>> # This creates an HTTP message
678 >>> # with the content of BODY as the enclosed representation
Senthil Kumaran8b4a2722014-04-16 23:33:02 -0400679 >>> # for the resource http://localhost:8080/file
Senthil Kumaranb5fe2472013-03-13 13:38:33 -0700680 ...
681 >>> import http.client
682 >>> BODY = "***filecontents***"
683 >>> conn = http.client.HTTPConnection("localhost", 8080)
684 >>> conn.request("PUT", "/file", BODY)
685 >>> response = conn.getresponse()
Georg Brandld277a562013-10-06 12:42:18 +0200686 >>> print(response.status, response.reason)
Senthil Kumaranb5fe2472013-03-13 13:38:33 -0700687 200, OK
Jeremy Hylton1052f892009-03-31 14:40:19 +0000688
689.. _httpmessage-objects:
690
691HTTPMessage Objects
692-------------------
693
Benjamin Peterson605b9d92009-04-02 00:24:00 +0000694An :class:`http.client.HTTPMessage` instance holds the headers from an HTTP
695response. It is implemented using the :class:`email.message.Message` class.
Jeremy Hylton1052f892009-03-31 14:40:19 +0000696
Benjamin Peterson605b9d92009-04-02 00:24:00 +0000697.. XXX Define the methods that clients can depend upon between versions.