blob: 9ef795651ef1f8d9387efacd65d4709de330c4d2 [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
22.. note::
23
Antoine Pitrou1ab19ca2010-10-13 10:39:21 +000024 HTTPS support is only available if Python was compiled with SSL support
25 (through the :mod:`ssl` module).
Georg Brandl116aa622007-08-15 14:28:22 +000026
Georg Brandl116aa622007-08-15 14:28:22 +000027The module provides the following classes:
28
29
Antoine Pitrou988dbd72010-12-17 17:35:56 +000030.. class:: HTTPConnection(host, port=None[, strict[, timeout[, source_address]]])
Georg Brandl116aa622007-08-15 14:28:22 +000031
32 An :class:`HTTPConnection` instance represents one transaction with an HTTP
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +000033 server. It should be instantiated passing it a host and optional port
34 number. If no port number is passed, the port is extracted from the host
35 string if it has the form ``host:port``, else the default HTTP port (80) is
Antoine Pitrou988dbd72010-12-17 17:35:56 +000036 used. If the optional *timeout* parameter is given, blocking
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +000037 operations (like connection attempts) will timeout after that many seconds
Georg Brandlf78e02b2008-06-10 17:40:04 +000038 (if it is not given, the global default timeout setting is used).
Raymond Hettinger519c3082011-01-30 00:39:00 +000039 The optional *source_address* parameter may be a tuple of a (host, port)
Gregory P. Smithb4066372010-01-03 03:28:29 +000040 to use as the source address the HTTP connection is made from.
Georg Brandl116aa622007-08-15 14:28:22 +000041
42 For example, the following calls all create instances that connect to the server
43 at the same host and port::
44
Georg Brandl24420152008-05-26 16:32:26 +000045 >>> h1 = http.client.HTTPConnection('www.cwi.nl')
46 >>> h2 = http.client.HTTPConnection('www.cwi.nl:80')
47 >>> h3 = http.client.HTTPConnection('www.cwi.nl', 80)
48 >>> h3 = http.client.HTTPConnection('www.cwi.nl', 80, timeout=10)
Georg Brandl116aa622007-08-15 14:28:22 +000049
Gregory P. Smithb4066372010-01-03 03:28:29 +000050 .. versionchanged:: 3.2
51 *source_address* was added.
Georg Brandl116aa622007-08-15 14:28:22 +000052
Antoine Pitrou988dbd72010-12-17 17:35:56 +000053 .. versionchanged:: 3.2
54 The *strict* parameter is deprecated. HTTP 0.9-style "Simple Responses"
55 are not supported anymore.
Gregory P. Smithb4066372010-01-03 03:28:29 +000056
Antoine Pitrou988dbd72010-12-17 17:35:56 +000057
58.. class:: HTTPSConnection(host, port=None, key_file=None, cert_file=None[, strict[, timeout[, source_address]]], *, context=None, check_hostname=None)
Georg Brandl116aa622007-08-15 14:28:22 +000059
60 A subclass of :class:`HTTPConnection` that uses SSL for communication with
Antoine Pitrou803e6d62010-10-13 10:36:15 +000061 secure servers. Default port is ``443``. If *context* is specified, it
62 must be a :class:`ssl.SSLContext` instance describing the various SSL
63 options. If *context* is specified and has a :attr:`~ssl.SSLContext.verify_mode`
64 of either :data:`~ssl.CERT_OPTIONAL` or :data:`~ssl.CERT_REQUIRED`, then
65 by default *host* is matched against the host name(s) allowed by the
66 server's certificate. If you want to change that behaviour, you can
67 explicitly set *check_hostname* to False.
Georg Brandl116aa622007-08-15 14:28:22 +000068
Antoine Pitrou803e6d62010-10-13 10:36:15 +000069 *key_file* and *cert_file* are deprecated, please use
70 :meth:`ssl.SSLContext.load_cert_chain` instead.
71
72 If you access arbitrary hosts on the Internet, it is recommended to
73 require certificate checking and feed the *context* with a set of
74 trusted CA certificates::
75
76 context = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
77 context.verify_mode = ssl.CERT_REQUIRED
78 context.load_verify_locations('/etc/pki/tls/certs/ca-bundle.crt')
79 h = client.HTTPSConnection('svn.python.org', 443, context=context)
Georg Brandl116aa622007-08-15 14:28:22 +000080
Gregory P. Smithb4066372010-01-03 03:28:29 +000081 .. versionchanged:: 3.2
Antoine Pitrou803e6d62010-10-13 10:36:15 +000082 *source_address*, *context* and *check_hostname* were added.
Gregory P. Smithb4066372010-01-03 03:28:29 +000083
Antoine Pitroud5323212010-10-22 18:19:07 +000084 .. versionchanged:: 3.2
85 This class now supports HTTPS virtual hosts if possible (that is,
86 if :data:`ssl.HAS_SNI` is true).
87
Antoine Pitrou988dbd72010-12-17 17:35:56 +000088 .. versionchanged:: 3.2
89 The *strict* parameter is deprecated. HTTP 0.9-style "Simple Responses"
90 are not supported anymore.
Georg Brandl116aa622007-08-15 14:28:22 +000091
Antoine Pitrou988dbd72010-12-17 17:35:56 +000092
93.. class:: HTTPResponse(sock, debuglevel=0[, strict], method=None, url=None)
Georg Brandl116aa622007-08-15 14:28:22 +000094
Jeremy Hylton1052f892009-03-31 14:40:19 +000095 Class whose instances are returned upon successful connection. Not
96 instantiated directly by user.
Georg Brandl116aa622007-08-15 14:28:22 +000097
Antoine Pitrou988dbd72010-12-17 17:35:56 +000098 .. versionchanged:: 3.2
99 The *strict* parameter is deprecated. HTTP 0.9-style "Simple Responses"
100 are not supported anymore.
101
Georg Brandl116aa622007-08-15 14:28:22 +0000102
103The following exceptions are raised as appropriate:
104
105
106.. exception:: HTTPException
107
108 The base class of the other exceptions in this module. It is a subclass of
109 :exc:`Exception`.
110
Georg Brandl116aa622007-08-15 14:28:22 +0000111
112.. exception:: NotConnected
113
114 A subclass of :exc:`HTTPException`.
115
Georg Brandl116aa622007-08-15 14:28:22 +0000116
117.. exception:: InvalidURL
118
119 A subclass of :exc:`HTTPException`, raised if a port is given and is either
120 non-numeric or empty.
121
Georg Brandl116aa622007-08-15 14:28:22 +0000122
123.. exception:: UnknownProtocol
124
125 A subclass of :exc:`HTTPException`.
126
Georg Brandl116aa622007-08-15 14:28:22 +0000127
128.. exception:: UnknownTransferEncoding
129
130 A subclass of :exc:`HTTPException`.
131
Georg Brandl116aa622007-08-15 14:28:22 +0000132
133.. exception:: UnimplementedFileMode
134
135 A subclass of :exc:`HTTPException`.
136
Georg Brandl116aa622007-08-15 14:28:22 +0000137
138.. exception:: IncompleteRead
139
140 A subclass of :exc:`HTTPException`.
141
Georg Brandl116aa622007-08-15 14:28:22 +0000142
143.. exception:: ImproperConnectionState
144
145 A subclass of :exc:`HTTPException`.
146
Georg Brandl116aa622007-08-15 14:28:22 +0000147
148.. exception:: CannotSendRequest
149
150 A subclass of :exc:`ImproperConnectionState`.
151
Georg Brandl116aa622007-08-15 14:28:22 +0000152
153.. exception:: CannotSendHeader
154
155 A subclass of :exc:`ImproperConnectionState`.
156
Georg Brandl116aa622007-08-15 14:28:22 +0000157
158.. exception:: ResponseNotReady
159
160 A subclass of :exc:`ImproperConnectionState`.
161
Georg Brandl116aa622007-08-15 14:28:22 +0000162
163.. exception:: BadStatusLine
164
165 A subclass of :exc:`HTTPException`. Raised if a server responds with a HTTP
166 status code that we don't understand.
167
Georg Brandl116aa622007-08-15 14:28:22 +0000168The constants defined in this module are:
169
170
171.. data:: HTTP_PORT
172
173 The default port for the HTTP protocol (always ``80``).
174
175
176.. data:: HTTPS_PORT
177
178 The default port for the HTTPS protocol (always ``443``).
179
180and also the following constants for integer status codes:
181
182+------------------------------------------+---------+-----------------------------------------------------------------------+
183| Constant | Value | Definition |
184+==========================================+=========+=======================================================================+
185| :const:`CONTINUE` | ``100`` | HTTP/1.1, `RFC 2616, Section |
186| | | 10.1.1 |
187| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.1.1>`_ |
188+------------------------------------------+---------+-----------------------------------------------------------------------+
189| :const:`SWITCHING_PROTOCOLS` | ``101`` | HTTP/1.1, `RFC 2616, Section |
190| | | 10.1.2 |
191| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.1.2>`_ |
192+------------------------------------------+---------+-----------------------------------------------------------------------+
193| :const:`PROCESSING` | ``102`` | WEBDAV, `RFC 2518, Section 10.1 |
194| | | <http://www.webdav.org/specs/rfc2518.html#STATUS_102>`_ |
195+------------------------------------------+---------+-----------------------------------------------------------------------+
196| :const:`OK` | ``200`` | HTTP/1.1, `RFC 2616, Section |
197| | | 10.2.1 |
198| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.1>`_ |
199+------------------------------------------+---------+-----------------------------------------------------------------------+
200| :const:`CREATED` | ``201`` | HTTP/1.1, `RFC 2616, Section |
201| | | 10.2.2 |
202| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.2>`_ |
203+------------------------------------------+---------+-----------------------------------------------------------------------+
204| :const:`ACCEPTED` | ``202`` | HTTP/1.1, `RFC 2616, Section |
205| | | 10.2.3 |
206| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.3>`_ |
207+------------------------------------------+---------+-----------------------------------------------------------------------+
208| :const:`NON_AUTHORITATIVE_INFORMATION` | ``203`` | HTTP/1.1, `RFC 2616, Section |
209| | | 10.2.4 |
210| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.4>`_ |
211+------------------------------------------+---------+-----------------------------------------------------------------------+
212| :const:`NO_CONTENT` | ``204`` | HTTP/1.1, `RFC 2616, Section |
213| | | 10.2.5 |
214| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.5>`_ |
215+------------------------------------------+---------+-----------------------------------------------------------------------+
216| :const:`RESET_CONTENT` | ``205`` | HTTP/1.1, `RFC 2616, Section |
217| | | 10.2.6 |
218| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.6>`_ |
219+------------------------------------------+---------+-----------------------------------------------------------------------+
220| :const:`PARTIAL_CONTENT` | ``206`` | HTTP/1.1, `RFC 2616, Section |
221| | | 10.2.7 |
222| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.7>`_ |
223+------------------------------------------+---------+-----------------------------------------------------------------------+
224| :const:`MULTI_STATUS` | ``207`` | WEBDAV `RFC 2518, Section 10.2 |
225| | | <http://www.webdav.org/specs/rfc2518.html#STATUS_207>`_ |
226+------------------------------------------+---------+-----------------------------------------------------------------------+
227| :const:`IM_USED` | ``226`` | Delta encoding in HTTP, |
228| | | :rfc:`3229`, Section 10.4.1 |
229+------------------------------------------+---------+-----------------------------------------------------------------------+
230| :const:`MULTIPLE_CHOICES` | ``300`` | HTTP/1.1, `RFC 2616, Section |
231| | | 10.3.1 |
232| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.1>`_ |
233+------------------------------------------+---------+-----------------------------------------------------------------------+
234| :const:`MOVED_PERMANENTLY` | ``301`` | HTTP/1.1, `RFC 2616, Section |
235| | | 10.3.2 |
236| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.2>`_ |
237+------------------------------------------+---------+-----------------------------------------------------------------------+
238| :const:`FOUND` | ``302`` | HTTP/1.1, `RFC 2616, Section |
239| | | 10.3.3 |
240| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.3>`_ |
241+------------------------------------------+---------+-----------------------------------------------------------------------+
242| :const:`SEE_OTHER` | ``303`` | HTTP/1.1, `RFC 2616, Section |
243| | | 10.3.4 |
244| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.4>`_ |
245+------------------------------------------+---------+-----------------------------------------------------------------------+
246| :const:`NOT_MODIFIED` | ``304`` | HTTP/1.1, `RFC 2616, Section |
247| | | 10.3.5 |
248| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.5>`_ |
249+------------------------------------------+---------+-----------------------------------------------------------------------+
250| :const:`USE_PROXY` | ``305`` | HTTP/1.1, `RFC 2616, Section |
251| | | 10.3.6 |
252| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.6>`_ |
253+------------------------------------------+---------+-----------------------------------------------------------------------+
254| :const:`TEMPORARY_REDIRECT` | ``307`` | HTTP/1.1, `RFC 2616, Section |
255| | | 10.3.8 |
256| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.8>`_ |
257+------------------------------------------+---------+-----------------------------------------------------------------------+
258| :const:`BAD_REQUEST` | ``400`` | HTTP/1.1, `RFC 2616, Section |
259| | | 10.4.1 |
260| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.1>`_ |
261+------------------------------------------+---------+-----------------------------------------------------------------------+
262| :const:`UNAUTHORIZED` | ``401`` | HTTP/1.1, `RFC 2616, Section |
263| | | 10.4.2 |
264| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.2>`_ |
265+------------------------------------------+---------+-----------------------------------------------------------------------+
266| :const:`PAYMENT_REQUIRED` | ``402`` | HTTP/1.1, `RFC 2616, Section |
267| | | 10.4.3 |
268| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.3>`_ |
269+------------------------------------------+---------+-----------------------------------------------------------------------+
270| :const:`FORBIDDEN` | ``403`` | HTTP/1.1, `RFC 2616, Section |
271| | | 10.4.4 |
272| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.4>`_ |
273+------------------------------------------+---------+-----------------------------------------------------------------------+
274| :const:`NOT_FOUND` | ``404`` | HTTP/1.1, `RFC 2616, Section |
275| | | 10.4.5 |
276| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.5>`_ |
277+------------------------------------------+---------+-----------------------------------------------------------------------+
278| :const:`METHOD_NOT_ALLOWED` | ``405`` | HTTP/1.1, `RFC 2616, Section |
279| | | 10.4.6 |
280| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.6>`_ |
281+------------------------------------------+---------+-----------------------------------------------------------------------+
282| :const:`NOT_ACCEPTABLE` | ``406`` | HTTP/1.1, `RFC 2616, Section |
283| | | 10.4.7 |
284| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.7>`_ |
285+------------------------------------------+---------+-----------------------------------------------------------------------+
286| :const:`PROXY_AUTHENTICATION_REQUIRED` | ``407`` | HTTP/1.1, `RFC 2616, Section |
287| | | 10.4.8 |
288| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.8>`_ |
289+------------------------------------------+---------+-----------------------------------------------------------------------+
290| :const:`REQUEST_TIMEOUT` | ``408`` | HTTP/1.1, `RFC 2616, Section |
291| | | 10.4.9 |
292| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.9>`_ |
293+------------------------------------------+---------+-----------------------------------------------------------------------+
294| :const:`CONFLICT` | ``409`` | HTTP/1.1, `RFC 2616, Section |
295| | | 10.4.10 |
296| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.10>`_ |
297+------------------------------------------+---------+-----------------------------------------------------------------------+
298| :const:`GONE` | ``410`` | HTTP/1.1, `RFC 2616, Section |
299| | | 10.4.11 |
300| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.11>`_ |
301+------------------------------------------+---------+-----------------------------------------------------------------------+
302| :const:`LENGTH_REQUIRED` | ``411`` | HTTP/1.1, `RFC 2616, Section |
303| | | 10.4.12 |
304| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.12>`_ |
305+------------------------------------------+---------+-----------------------------------------------------------------------+
306| :const:`PRECONDITION_FAILED` | ``412`` | HTTP/1.1, `RFC 2616, Section |
307| | | 10.4.13 |
308| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.13>`_ |
309+------------------------------------------+---------+-----------------------------------------------------------------------+
310| :const:`REQUEST_ENTITY_TOO_LARGE` | ``413`` | HTTP/1.1, `RFC 2616, Section |
311| | | 10.4.14 |
312| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.14>`_ |
313+------------------------------------------+---------+-----------------------------------------------------------------------+
314| :const:`REQUEST_URI_TOO_LONG` | ``414`` | HTTP/1.1, `RFC 2616, Section |
315| | | 10.4.15 |
316| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.15>`_ |
317+------------------------------------------+---------+-----------------------------------------------------------------------+
318| :const:`UNSUPPORTED_MEDIA_TYPE` | ``415`` | HTTP/1.1, `RFC 2616, Section |
319| | | 10.4.16 |
320| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.16>`_ |
321+------------------------------------------+---------+-----------------------------------------------------------------------+
322| :const:`REQUESTED_RANGE_NOT_SATISFIABLE` | ``416`` | HTTP/1.1, `RFC 2616, Section |
323| | | 10.4.17 |
324| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.17>`_ |
325+------------------------------------------+---------+-----------------------------------------------------------------------+
326| :const:`EXPECTATION_FAILED` | ``417`` | HTTP/1.1, `RFC 2616, Section |
327| | | 10.4.18 |
328| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.18>`_ |
329+------------------------------------------+---------+-----------------------------------------------------------------------+
330| :const:`UNPROCESSABLE_ENTITY` | ``422`` | WEBDAV, `RFC 2518, Section 10.3 |
331| | | <http://www.webdav.org/specs/rfc2518.html#STATUS_422>`_ |
332+------------------------------------------+---------+-----------------------------------------------------------------------+
333| :const:`LOCKED` | ``423`` | WEBDAV `RFC 2518, Section 10.4 |
334| | | <http://www.webdav.org/specs/rfc2518.html#STATUS_423>`_ |
335+------------------------------------------+---------+-----------------------------------------------------------------------+
336| :const:`FAILED_DEPENDENCY` | ``424`` | WEBDAV, `RFC 2518, Section 10.5 |
337| | | <http://www.webdav.org/specs/rfc2518.html#STATUS_424>`_ |
338+------------------------------------------+---------+-----------------------------------------------------------------------+
339| :const:`UPGRADE_REQUIRED` | ``426`` | HTTP Upgrade to TLS, |
340| | | :rfc:`2817`, Section 6 |
341+------------------------------------------+---------+-----------------------------------------------------------------------+
342| :const:`INTERNAL_SERVER_ERROR` | ``500`` | HTTP/1.1, `RFC 2616, Section |
343| | | 10.5.1 |
344| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.1>`_ |
345+------------------------------------------+---------+-----------------------------------------------------------------------+
346| :const:`NOT_IMPLEMENTED` | ``501`` | HTTP/1.1, `RFC 2616, Section |
347| | | 10.5.2 |
348| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.2>`_ |
349+------------------------------------------+---------+-----------------------------------------------------------------------+
350| :const:`BAD_GATEWAY` | ``502`` | HTTP/1.1 `RFC 2616, Section |
351| | | 10.5.3 |
352| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.3>`_ |
353+------------------------------------------+---------+-----------------------------------------------------------------------+
354| :const:`SERVICE_UNAVAILABLE` | ``503`` | HTTP/1.1, `RFC 2616, Section |
355| | | 10.5.4 |
356| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.4>`_ |
357+------------------------------------------+---------+-----------------------------------------------------------------------+
358| :const:`GATEWAY_TIMEOUT` | ``504`` | HTTP/1.1 `RFC 2616, Section |
359| | | 10.5.5 |
360| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.5>`_ |
361+------------------------------------------+---------+-----------------------------------------------------------------------+
362| :const:`HTTP_VERSION_NOT_SUPPORTED` | ``505`` | HTTP/1.1, `RFC 2616, Section |
363| | | 10.5.6 |
364| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.6>`_ |
365+------------------------------------------+---------+-----------------------------------------------------------------------+
366| :const:`INSUFFICIENT_STORAGE` | ``507`` | WEBDAV, `RFC 2518, Section 10.6 |
367| | | <http://www.webdav.org/specs/rfc2518.html#STATUS_507>`_ |
368+------------------------------------------+---------+-----------------------------------------------------------------------+
369| :const:`NOT_EXTENDED` | ``510`` | An HTTP Extension Framework, |
370| | | :rfc:`2774`, Section 7 |
371+------------------------------------------+---------+-----------------------------------------------------------------------+
372
373
374.. data:: responses
375
376 This dictionary maps the HTTP 1.1 status codes to the W3C names.
377
Georg Brandl24420152008-05-26 16:32:26 +0000378 Example: ``http.client.responses[http.client.NOT_FOUND]`` is ``'Not Found'``.
Georg Brandl116aa622007-08-15 14:28:22 +0000379
Georg Brandl116aa622007-08-15 14:28:22 +0000380
381.. _httpconnection-objects:
382
383HTTPConnection Objects
384----------------------
385
386:class:`HTTPConnection` instances have the following methods:
387
388
Georg Brandl036490d2009-05-17 13:00:36 +0000389.. method:: HTTPConnection.request(method, url, body=None, headers={})
Georg Brandl116aa622007-08-15 14:28:22 +0000390
Jeremy Hylton236654b2009-03-27 20:24:34 +0000391 This will send a request to the server using the HTTP request
392 method *method* and the selector *url*. If the *body* argument is
393 present, it should be string or bytes object of data to send after
394 the headers are finished. Strings are encoded as ISO-8859-1, the
395 default charset for HTTP. To use other encodings, pass a bytes
396 object. The Content-Length header is set to the length of the
397 string.
Georg Brandl116aa622007-08-15 14:28:22 +0000398
Antoine Pitrou11cb9612010-09-15 11:11:28 +0000399 The *body* may also be an open :term:`file object`, in which case the
Senthil Kumaran7bc0d872010-12-19 10:49:52 +0000400 contents of the file is sent; this file object should support ``fileno()``
401 and ``read()`` methods. The header Content-Length is automatically set to
402 the length of the file as reported by stat. The *body* argument may also be
Raymond Hettinger519c3082011-01-30 00:39:00 +0000403 an iterable and Content-Length header should be explicitly provided when the
Senthil Kumaran7bc0d872010-12-19 10:49:52 +0000404 body is an iterable.
Jeremy Hylton236654b2009-03-27 20:24:34 +0000405
406 The *headers* argument should be a mapping of extra HTTP
407 headers to send with the request.
Georg Brandl116aa622007-08-15 14:28:22 +0000408
Senthil Kumaran7bc0d872010-12-19 10:49:52 +0000409 .. versionadded:: 3.2
Georg Brandl09a7df82010-12-19 12:33:52 +0000410 *body* can now be an iterable.
Senthil Kumaran7bc0d872010-12-19 10:49:52 +0000411
Georg Brandl116aa622007-08-15 14:28:22 +0000412.. method:: HTTPConnection.getresponse()
413
414 Should be called after a request is sent to get the response from the server.
415 Returns an :class:`HTTPResponse` instance.
416
417 .. note::
418
419 Note that you must have read the whole response before you can send a new
420 request to the server.
421
422
423.. method:: HTTPConnection.set_debuglevel(level)
424
R. David Murrayd89bc3f2010-12-15 02:19:14 +0000425 Set the debugging level. The default debug level is ``0``, meaning no
426 debugging output is printed. Any value greater than ``0`` will cause all
427 currently defined debug output to be printed to stdout. The ``debuglevel``
428 is passed to any new :class:`HTTPResponse` objects that are created.
Georg Brandl116aa622007-08-15 14:28:22 +0000429
Mark Dickinson574b1d62009-10-01 20:20:09 +0000430 .. versionadded:: 3.1
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000431
Georg Brandl67b21b72010-08-17 15:07:14 +0000432
Senthil Kumaran7e5229c2009-12-20 07:31:21 +0000433.. method:: HTTPConnection.set_tunnel(host, port=None, headers=None)
Senthil Kumaran97f0c6b2009-07-25 04:24:38 +0000434
435 Set the host and the port for HTTP Connect Tunnelling. Normally used when it
436 is required to a HTTPS Connection through a proxy server.
437
Senthil Kumaran7e5229c2009-12-20 07:31:21 +0000438 The headers argument should be a mapping of extra HTTP headers to to sent
439 with the CONNECT request.
440
Senthil Kumaran2e910fd2009-07-25 04:27:38 +0000441 .. versionadded:: 3.2
Georg Brandl116aa622007-08-15 14:28:22 +0000442
Georg Brandl67b21b72010-08-17 15:07:14 +0000443
Georg Brandl116aa622007-08-15 14:28:22 +0000444.. method:: HTTPConnection.connect()
445
446 Connect to the server specified when the object was created.
447
448
449.. method:: HTTPConnection.close()
450
451 Close the connection to the server.
452
453As an alternative to using the :meth:`request` method described above, you can
454also send your request step by step, by using the four functions below.
455
456
Georg Brandl036490d2009-05-17 13:00:36 +0000457.. method:: HTTPConnection.putrequest(request, selector, skip_host=False, skip_accept_encoding=False)
Georg Brandl116aa622007-08-15 14:28:22 +0000458
459 This should be the first call after the connection to the server has been made.
460 It sends a line to the server consisting of the *request* string, the *selector*
461 string, and the HTTP version (``HTTP/1.1``). To disable automatic sending of
462 ``Host:`` or ``Accept-Encoding:`` headers (for example to accept additional
463 content encodings), specify *skip_host* or *skip_accept_encoding* with non-False
464 values.
465
Georg Brandl116aa622007-08-15 14:28:22 +0000466
467.. method:: HTTPConnection.putheader(header, argument[, ...])
468
469 Send an :rfc:`822`\ -style header to the server. It sends a line to the server
470 consisting of the header, a colon and a space, and the first argument. If more
471 arguments are given, continuation lines are sent, each consisting of a tab and
472 an argument.
473
474
Senthil Kumaran5d0de3f2011-10-03 07:27:06 +0800475.. method:: HTTPConnection.endheaders(message_body=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000476
Senthil Kumaran5d0de3f2011-10-03 07:27:06 +0800477 Send a blank line to the server, signalling the end of the headers. The
478 optional message_body argument can be used to pass message body
479 associated with the request. The message body will be sent in
480 the same packet as the message headers if possible. The
481 message_body should be a string.
Georg Brandl116aa622007-08-15 14:28:22 +0000482
483
484.. method:: HTTPConnection.send(data)
485
486 Send data to the server. This should be used directly only after the
487 :meth:`endheaders` method has been called and before :meth:`getresponse` is
488 called.
489
490
491.. _httpresponse-objects:
492
493HTTPResponse Objects
494--------------------
495
Jeremy Hylton1052f892009-03-31 14:40:19 +0000496An :class:`HTTPResponse` instance wraps the HTTP response from the
497server. It provides access to the request headers and the entity
498body. The response is an iterable object and can be used in a with
499statement.
Georg Brandl116aa622007-08-15 14:28:22 +0000500
501
502.. method:: HTTPResponse.read([amt])
503
504 Reads and returns the response body, or up to the next *amt* bytes.
505
506
Georg Brandl036490d2009-05-17 13:00:36 +0000507.. method:: HTTPResponse.getheader(name, default=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000508
Senthil Kumaran790f8312010-08-02 17:09:02 +0000509 Return the value of the header *name*, or *default* if there is no header
510 matching *name*. If there is more than one header with the name *name*,
511 return all of the values joined by ', '. If 'default' is any iterable other
512 than a single string, its elements are similarly returned joined by commas.
Georg Brandl116aa622007-08-15 14:28:22 +0000513
514
515.. method:: HTTPResponse.getheaders()
516
517 Return a list of (header, value) tuples.
518
Senthil Kumaranceff5662010-09-21 01:57:43 +0000519.. method:: HTTPResponse.fileno()
520
521 Return the ``fileno`` of the underlying socket.
Georg Brandl116aa622007-08-15 14:28:22 +0000522
523.. attribute:: HTTPResponse.msg
524
Jeremy Hylton1052f892009-03-31 14:40:19 +0000525 A :class:`http.client.HTTPMessage` instance containing the response
526 headers. :class:`http.client.HTTPMessage` is a subclass of
527 :class:`email.message.Message`.
Georg Brandl116aa622007-08-15 14:28:22 +0000528
529
530.. attribute:: HTTPResponse.version
531
532 HTTP protocol version used by server. 10 for HTTP/1.0, 11 for HTTP/1.1.
533
534
535.. attribute:: HTTPResponse.status
536
537 Status code returned by server.
538
539
540.. attribute:: HTTPResponse.reason
541
542 Reason phrase returned by server.
543
544
Jeremy Hylton1052f892009-03-31 14:40:19 +0000545.. attribute:: HTTPResponse.debuglevel
546
Georg Brandlef871f62010-03-12 10:06:40 +0000547 A debugging hook. If :attr:`debuglevel` is greater than zero, messages
Jeremy Hylton1052f892009-03-31 14:40:19 +0000548 will be printed to stdout as the response is read and parsed.
549
Senthil Kumarance9b5962011-06-19 16:56:49 -0700550.. attribute:: HTTPResponse.closed
551
Senthil Kumaranfd8d7e92011-06-19 16:59:41 -0700552 Is True if the stream is closed.
Jeremy Hylton1052f892009-03-31 14:40:19 +0000553
Georg Brandl116aa622007-08-15 14:28:22 +0000554Examples
555--------
556
557Here is an example session that uses the ``GET`` method::
558
Georg Brandl24420152008-05-26 16:32:26 +0000559 >>> import http.client
560 >>> conn = http.client.HTTPConnection("www.python.org")
Georg Brandl116aa622007-08-15 14:28:22 +0000561 >>> conn.request("GET", "/index.html")
562 >>> r1 = conn.getresponse()
Georg Brandl6911e3c2007-09-04 07:15:32 +0000563 >>> print(r1.status, r1.reason)
Georg Brandl116aa622007-08-15 14:28:22 +0000564 200 OK
Senthil Kumarance9b5962011-06-19 16:56:49 -0700565 >>> data1 = r1.read() # This will return entire content.
566 >>> # The following example demonstrates reading data in chunks.
567 >>> conn.request("GET", "/index.html")
568 >>> r1 = conn.getresponse()
569 >>> while not r1.closed:
570 ... print(r1.read(200)) # 200 bytes
571 b'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"...
572 ...
573 >>> # Example of an invalid request
Georg Brandl116aa622007-08-15 14:28:22 +0000574 >>> conn.request("GET", "/parrot.spam")
575 >>> r2 = conn.getresponse()
Georg Brandl6911e3c2007-09-04 07:15:32 +0000576 >>> print(r2.status, r2.reason)
Georg Brandl116aa622007-08-15 14:28:22 +0000577 404 Not Found
578 >>> data2 = r2.read()
579 >>> conn.close()
580
Fred Drake1587e3d2010-05-12 01:36:11 +0000581Here is an example session that uses the ``HEAD`` method. Note that the
582``HEAD`` method never returns any data. ::
Senthil Kumaran71fb6c82010-04-28 17:39:48 +0000583
584 >>> import http.client
585 >>> conn = http.client.HTTPConnection("www.python.org")
586 >>> conn.request("HEAD","/index.html")
587 >>> res = conn.getresponse()
588 >>> print(res.status, res.reason)
589 200 OK
590 >>> data = res.read()
591 >>> print(len(data))
592 0
593 >>> data == b''
594 True
595
Georg Brandl116aa622007-08-15 14:28:22 +0000596Here is an example session that shows how to ``POST`` requests::
597
Senthil Kumaranaca8fd72008-06-23 04:41:59 +0000598 >>> import http.client, urllib.parse
Senthil Kumaran96c84a42011-07-20 21:56:24 +0800599 >>> params = urllib.parse.urlencode({'@number': 12524, '@type': 'issue', '@action': 'show'})
Georg Brandl116aa622007-08-15 14:28:22 +0000600 >>> headers = {"Content-type": "application/x-www-form-urlencoded",
601 ... "Accept": "text/plain"}
Senthil Kumaran96c84a42011-07-20 21:56:24 +0800602 >>> conn = http.client.HTTPConnection("bugs.python.org")
603 >>> conn.request("POST", "", params, headers)
Georg Brandl116aa622007-08-15 14:28:22 +0000604 >>> response = conn.getresponse()
Georg Brandl6911e3c2007-09-04 07:15:32 +0000605 >>> print(response.status, response.reason)
Senthil Kumaran96c84a42011-07-20 21:56:24 +0800606 302 Found
Georg Brandl116aa622007-08-15 14:28:22 +0000607 >>> data = response.read()
Senthil Kumaran96c84a42011-07-20 21:56:24 +0800608 >>> data
609 b'Redirecting to <a href="http://bugs.python.org/issue12524">http://bugs.python.org/issue12524</a>'
Georg Brandl116aa622007-08-15 14:28:22 +0000610 >>> conn.close()
611
Jeremy Hylton1052f892009-03-31 14:40:19 +0000612
613.. _httpmessage-objects:
614
615HTTPMessage Objects
616-------------------
617
Benjamin Peterson605b9d92009-04-02 00:24:00 +0000618An :class:`http.client.HTTPMessage` instance holds the headers from an HTTP
619response. It is implemented using the :class:`email.message.Message` class.
Jeremy Hylton1052f892009-03-31 14:40:19 +0000620
Benjamin Peterson605b9d92009-04-02 00:24:00 +0000621.. XXX Define the methods that clients can depend upon between versions.