blob: cba59077d670550968bc02acce218d3027d865d7 [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
14This module defines classes which implement the client side of the HTTP and
Senthil Kumaranaca8fd72008-06-23 04:41:59 +000015HTTPS protocols. It is normally not used directly --- the module
Georg Brandl0f7ede42008-06-23 11:23:31 +000016:mod:`urllib.request` uses it to handle URLs that use HTTP and HTTPS.
Georg Brandl116aa622007-08-15 14:28:22 +000017
18.. note::
19
Antoine Pitrou1ab19ca2010-10-13 10:39:21 +000020 HTTPS support is only available if Python was compiled with SSL support
21 (through the :mod:`ssl` module).
Georg Brandl116aa622007-08-15 14:28:22 +000022
Georg Brandl116aa622007-08-15 14:28:22 +000023The module provides the following classes:
24
25
Antoine Pitrou988dbd72010-12-17 17:35:56 +000026.. class:: HTTPConnection(host, port=None[, strict[, timeout[, source_address]]])
Georg Brandl116aa622007-08-15 14:28:22 +000027
28 An :class:`HTTPConnection` instance represents one transaction with an HTTP
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +000029 server. It should be instantiated passing it a host and optional port
30 number. If no port number is passed, the port is extracted from the host
31 string if it has the form ``host:port``, else the default HTTP port (80) is
Antoine Pitrou988dbd72010-12-17 17:35:56 +000032 used. If the optional *timeout* parameter is given, blocking
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +000033 operations (like connection attempts) will timeout after that many seconds
Georg Brandlf78e02b2008-06-10 17:40:04 +000034 (if it is not given, the global default timeout setting is used).
Gregory P. Smithb4066372010-01-03 03:28:29 +000035 The optional *source_address* parameter may be a typle of a (host, port)
36 to use as the source address the HTTP connection is made from.
Georg Brandl116aa622007-08-15 14:28:22 +000037
38 For example, the following calls all create instances that connect to the server
39 at the same host and port::
40
Georg Brandl24420152008-05-26 16:32:26 +000041 >>> h1 = http.client.HTTPConnection('www.cwi.nl')
42 >>> h2 = http.client.HTTPConnection('www.cwi.nl:80')
43 >>> h3 = http.client.HTTPConnection('www.cwi.nl', 80)
44 >>> h3 = http.client.HTTPConnection('www.cwi.nl', 80, timeout=10)
Georg Brandl116aa622007-08-15 14:28:22 +000045
Gregory P. Smithb4066372010-01-03 03:28:29 +000046 .. versionchanged:: 3.2
47 *source_address* was added.
Georg Brandl116aa622007-08-15 14:28:22 +000048
Antoine Pitrou988dbd72010-12-17 17:35:56 +000049 .. versionchanged:: 3.2
50 The *strict* parameter is deprecated. HTTP 0.9-style "Simple Responses"
51 are not supported anymore.
Gregory P. Smithb4066372010-01-03 03:28:29 +000052
Antoine Pitrou988dbd72010-12-17 17:35:56 +000053
54.. 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 +000055
56 A subclass of :class:`HTTPConnection` that uses SSL for communication with
Antoine Pitrou803e6d62010-10-13 10:36:15 +000057 secure servers. Default port is ``443``. If *context* is specified, it
58 must be a :class:`ssl.SSLContext` instance describing the various SSL
59 options. If *context* is specified and has a :attr:`~ssl.SSLContext.verify_mode`
60 of either :data:`~ssl.CERT_OPTIONAL` or :data:`~ssl.CERT_REQUIRED`, then
61 by default *host* is matched against the host name(s) allowed by the
62 server's certificate. If you want to change that behaviour, you can
63 explicitly set *check_hostname* to False.
Georg Brandl116aa622007-08-15 14:28:22 +000064
Antoine Pitrou803e6d62010-10-13 10:36:15 +000065 *key_file* and *cert_file* are deprecated, please use
66 :meth:`ssl.SSLContext.load_cert_chain` instead.
67
68 If you access arbitrary hosts on the Internet, it is recommended to
69 require certificate checking and feed the *context* with a set of
70 trusted CA certificates::
71
72 context = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
73 context.verify_mode = ssl.CERT_REQUIRED
74 context.load_verify_locations('/etc/pki/tls/certs/ca-bundle.crt')
75 h = client.HTTPSConnection('svn.python.org', 443, context=context)
Georg Brandl116aa622007-08-15 14:28:22 +000076
Gregory P. Smithb4066372010-01-03 03:28:29 +000077 .. versionchanged:: 3.2
Antoine Pitrou803e6d62010-10-13 10:36:15 +000078 *source_address*, *context* and *check_hostname* were added.
Gregory P. Smithb4066372010-01-03 03:28:29 +000079
Antoine Pitroud5323212010-10-22 18:19:07 +000080 .. versionchanged:: 3.2
81 This class now supports HTTPS virtual hosts if possible (that is,
82 if :data:`ssl.HAS_SNI` is true).
83
Antoine Pitrou988dbd72010-12-17 17:35:56 +000084 .. versionchanged:: 3.2
85 The *strict* parameter is deprecated. HTTP 0.9-style "Simple Responses"
86 are not supported anymore.
Georg Brandl116aa622007-08-15 14:28:22 +000087
Antoine Pitrou988dbd72010-12-17 17:35:56 +000088
89.. class:: HTTPResponse(sock, debuglevel=0[, strict], method=None, url=None)
Georg Brandl116aa622007-08-15 14:28:22 +000090
Jeremy Hylton1052f892009-03-31 14:40:19 +000091 Class whose instances are returned upon successful connection. Not
92 instantiated directly by user.
Georg Brandl116aa622007-08-15 14:28:22 +000093
Antoine Pitrou988dbd72010-12-17 17:35:56 +000094 .. versionchanged:: 3.2
95 The *strict* parameter is deprecated. HTTP 0.9-style "Simple Responses"
96 are not supported anymore.
97
Georg Brandl116aa622007-08-15 14:28:22 +000098
99The following exceptions are raised as appropriate:
100
101
102.. exception:: HTTPException
103
104 The base class of the other exceptions in this module. It is a subclass of
105 :exc:`Exception`.
106
Georg Brandl116aa622007-08-15 14:28:22 +0000107
108.. exception:: NotConnected
109
110 A subclass of :exc:`HTTPException`.
111
Georg Brandl116aa622007-08-15 14:28:22 +0000112
113.. exception:: InvalidURL
114
115 A subclass of :exc:`HTTPException`, raised if a port is given and is either
116 non-numeric or empty.
117
Georg Brandl116aa622007-08-15 14:28:22 +0000118
119.. exception:: UnknownProtocol
120
121 A subclass of :exc:`HTTPException`.
122
Georg Brandl116aa622007-08-15 14:28:22 +0000123
124.. exception:: UnknownTransferEncoding
125
126 A subclass of :exc:`HTTPException`.
127
Georg Brandl116aa622007-08-15 14:28:22 +0000128
129.. exception:: UnimplementedFileMode
130
131 A subclass of :exc:`HTTPException`.
132
Georg Brandl116aa622007-08-15 14:28:22 +0000133
134.. exception:: IncompleteRead
135
136 A subclass of :exc:`HTTPException`.
137
Georg Brandl116aa622007-08-15 14:28:22 +0000138
139.. exception:: ImproperConnectionState
140
141 A subclass of :exc:`HTTPException`.
142
Georg Brandl116aa622007-08-15 14:28:22 +0000143
144.. exception:: CannotSendRequest
145
146 A subclass of :exc:`ImproperConnectionState`.
147
Georg Brandl116aa622007-08-15 14:28:22 +0000148
149.. exception:: CannotSendHeader
150
151 A subclass of :exc:`ImproperConnectionState`.
152
Georg Brandl116aa622007-08-15 14:28:22 +0000153
154.. exception:: ResponseNotReady
155
156 A subclass of :exc:`ImproperConnectionState`.
157
Georg Brandl116aa622007-08-15 14:28:22 +0000158
159.. exception:: BadStatusLine
160
161 A subclass of :exc:`HTTPException`. Raised if a server responds with a HTTP
162 status code that we don't understand.
163
Georg Brandl116aa622007-08-15 14:28:22 +0000164The constants defined in this module are:
165
166
167.. data:: HTTP_PORT
168
169 The default port for the HTTP protocol (always ``80``).
170
171
172.. data:: HTTPS_PORT
173
174 The default port for the HTTPS protocol (always ``443``).
175
176and also the following constants for integer status codes:
177
178+------------------------------------------+---------+-----------------------------------------------------------------------+
179| Constant | Value | Definition |
180+==========================================+=========+=======================================================================+
181| :const:`CONTINUE` | ``100`` | HTTP/1.1, `RFC 2616, Section |
182| | | 10.1.1 |
183| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.1.1>`_ |
184+------------------------------------------+---------+-----------------------------------------------------------------------+
185| :const:`SWITCHING_PROTOCOLS` | ``101`` | HTTP/1.1, `RFC 2616, Section |
186| | | 10.1.2 |
187| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.1.2>`_ |
188+------------------------------------------+---------+-----------------------------------------------------------------------+
189| :const:`PROCESSING` | ``102`` | WEBDAV, `RFC 2518, Section 10.1 |
190| | | <http://www.webdav.org/specs/rfc2518.html#STATUS_102>`_ |
191+------------------------------------------+---------+-----------------------------------------------------------------------+
192| :const:`OK` | ``200`` | HTTP/1.1, `RFC 2616, Section |
193| | | 10.2.1 |
194| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.1>`_ |
195+------------------------------------------+---------+-----------------------------------------------------------------------+
196| :const:`CREATED` | ``201`` | HTTP/1.1, `RFC 2616, Section |
197| | | 10.2.2 |
198| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.2>`_ |
199+------------------------------------------+---------+-----------------------------------------------------------------------+
200| :const:`ACCEPTED` | ``202`` | HTTP/1.1, `RFC 2616, Section |
201| | | 10.2.3 |
202| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.3>`_ |
203+------------------------------------------+---------+-----------------------------------------------------------------------+
204| :const:`NON_AUTHORITATIVE_INFORMATION` | ``203`` | HTTP/1.1, `RFC 2616, Section |
205| | | 10.2.4 |
206| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.4>`_ |
207+------------------------------------------+---------+-----------------------------------------------------------------------+
208| :const:`NO_CONTENT` | ``204`` | HTTP/1.1, `RFC 2616, Section |
209| | | 10.2.5 |
210| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.5>`_ |
211+------------------------------------------+---------+-----------------------------------------------------------------------+
212| :const:`RESET_CONTENT` | ``205`` | HTTP/1.1, `RFC 2616, Section |
213| | | 10.2.6 |
214| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.6>`_ |
215+------------------------------------------+---------+-----------------------------------------------------------------------+
216| :const:`PARTIAL_CONTENT` | ``206`` | HTTP/1.1, `RFC 2616, Section |
217| | | 10.2.7 |
218| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.7>`_ |
219+------------------------------------------+---------+-----------------------------------------------------------------------+
220| :const:`MULTI_STATUS` | ``207`` | WEBDAV `RFC 2518, Section 10.2 |
221| | | <http://www.webdav.org/specs/rfc2518.html#STATUS_207>`_ |
222+------------------------------------------+---------+-----------------------------------------------------------------------+
223| :const:`IM_USED` | ``226`` | Delta encoding in HTTP, |
224| | | :rfc:`3229`, Section 10.4.1 |
225+------------------------------------------+---------+-----------------------------------------------------------------------+
226| :const:`MULTIPLE_CHOICES` | ``300`` | HTTP/1.1, `RFC 2616, Section |
227| | | 10.3.1 |
228| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.1>`_ |
229+------------------------------------------+---------+-----------------------------------------------------------------------+
230| :const:`MOVED_PERMANENTLY` | ``301`` | HTTP/1.1, `RFC 2616, Section |
231| | | 10.3.2 |
232| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.2>`_ |
233+------------------------------------------+---------+-----------------------------------------------------------------------+
234| :const:`FOUND` | ``302`` | HTTP/1.1, `RFC 2616, Section |
235| | | 10.3.3 |
236| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.3>`_ |
237+------------------------------------------+---------+-----------------------------------------------------------------------+
238| :const:`SEE_OTHER` | ``303`` | HTTP/1.1, `RFC 2616, Section |
239| | | 10.3.4 |
240| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.4>`_ |
241+------------------------------------------+---------+-----------------------------------------------------------------------+
242| :const:`NOT_MODIFIED` | ``304`` | HTTP/1.1, `RFC 2616, Section |
243| | | 10.3.5 |
244| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.5>`_ |
245+------------------------------------------+---------+-----------------------------------------------------------------------+
246| :const:`USE_PROXY` | ``305`` | HTTP/1.1, `RFC 2616, Section |
247| | | 10.3.6 |
248| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.6>`_ |
249+------------------------------------------+---------+-----------------------------------------------------------------------+
250| :const:`TEMPORARY_REDIRECT` | ``307`` | HTTP/1.1, `RFC 2616, Section |
251| | | 10.3.8 |
252| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.8>`_ |
253+------------------------------------------+---------+-----------------------------------------------------------------------+
254| :const:`BAD_REQUEST` | ``400`` | HTTP/1.1, `RFC 2616, Section |
255| | | 10.4.1 |
256| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.1>`_ |
257+------------------------------------------+---------+-----------------------------------------------------------------------+
258| :const:`UNAUTHORIZED` | ``401`` | HTTP/1.1, `RFC 2616, Section |
259| | | 10.4.2 |
260| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.2>`_ |
261+------------------------------------------+---------+-----------------------------------------------------------------------+
262| :const:`PAYMENT_REQUIRED` | ``402`` | HTTP/1.1, `RFC 2616, Section |
263| | | 10.4.3 |
264| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.3>`_ |
265+------------------------------------------+---------+-----------------------------------------------------------------------+
266| :const:`FORBIDDEN` | ``403`` | HTTP/1.1, `RFC 2616, Section |
267| | | 10.4.4 |
268| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.4>`_ |
269+------------------------------------------+---------+-----------------------------------------------------------------------+
270| :const:`NOT_FOUND` | ``404`` | HTTP/1.1, `RFC 2616, Section |
271| | | 10.4.5 |
272| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.5>`_ |
273+------------------------------------------+---------+-----------------------------------------------------------------------+
274| :const:`METHOD_NOT_ALLOWED` | ``405`` | HTTP/1.1, `RFC 2616, Section |
275| | | 10.4.6 |
276| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.6>`_ |
277+------------------------------------------+---------+-----------------------------------------------------------------------+
278| :const:`NOT_ACCEPTABLE` | ``406`` | HTTP/1.1, `RFC 2616, Section |
279| | | 10.4.7 |
280| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.7>`_ |
281+------------------------------------------+---------+-----------------------------------------------------------------------+
282| :const:`PROXY_AUTHENTICATION_REQUIRED` | ``407`` | HTTP/1.1, `RFC 2616, Section |
283| | | 10.4.8 |
284| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.8>`_ |
285+------------------------------------------+---------+-----------------------------------------------------------------------+
286| :const:`REQUEST_TIMEOUT` | ``408`` | HTTP/1.1, `RFC 2616, Section |
287| | | 10.4.9 |
288| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.9>`_ |
289+------------------------------------------+---------+-----------------------------------------------------------------------+
290| :const:`CONFLICT` | ``409`` | HTTP/1.1, `RFC 2616, Section |
291| | | 10.4.10 |
292| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.10>`_ |
293+------------------------------------------+---------+-----------------------------------------------------------------------+
294| :const:`GONE` | ``410`` | HTTP/1.1, `RFC 2616, Section |
295| | | 10.4.11 |
296| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.11>`_ |
297+------------------------------------------+---------+-----------------------------------------------------------------------+
298| :const:`LENGTH_REQUIRED` | ``411`` | HTTP/1.1, `RFC 2616, Section |
299| | | 10.4.12 |
300| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.12>`_ |
301+------------------------------------------+---------+-----------------------------------------------------------------------+
302| :const:`PRECONDITION_FAILED` | ``412`` | HTTP/1.1, `RFC 2616, Section |
303| | | 10.4.13 |
304| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.13>`_ |
305+------------------------------------------+---------+-----------------------------------------------------------------------+
306| :const:`REQUEST_ENTITY_TOO_LARGE` | ``413`` | HTTP/1.1, `RFC 2616, Section |
307| | | 10.4.14 |
308| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.14>`_ |
309+------------------------------------------+---------+-----------------------------------------------------------------------+
310| :const:`REQUEST_URI_TOO_LONG` | ``414`` | HTTP/1.1, `RFC 2616, Section |
311| | | 10.4.15 |
312| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.15>`_ |
313+------------------------------------------+---------+-----------------------------------------------------------------------+
314| :const:`UNSUPPORTED_MEDIA_TYPE` | ``415`` | HTTP/1.1, `RFC 2616, Section |
315| | | 10.4.16 |
316| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.16>`_ |
317+------------------------------------------+---------+-----------------------------------------------------------------------+
318| :const:`REQUESTED_RANGE_NOT_SATISFIABLE` | ``416`` | HTTP/1.1, `RFC 2616, Section |
319| | | 10.4.17 |
320| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.17>`_ |
321+------------------------------------------+---------+-----------------------------------------------------------------------+
322| :const:`EXPECTATION_FAILED` | ``417`` | HTTP/1.1, `RFC 2616, Section |
323| | | 10.4.18 |
324| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.18>`_ |
325+------------------------------------------+---------+-----------------------------------------------------------------------+
326| :const:`UNPROCESSABLE_ENTITY` | ``422`` | WEBDAV, `RFC 2518, Section 10.3 |
327| | | <http://www.webdav.org/specs/rfc2518.html#STATUS_422>`_ |
328+------------------------------------------+---------+-----------------------------------------------------------------------+
329| :const:`LOCKED` | ``423`` | WEBDAV `RFC 2518, Section 10.4 |
330| | | <http://www.webdav.org/specs/rfc2518.html#STATUS_423>`_ |
331+------------------------------------------+---------+-----------------------------------------------------------------------+
332| :const:`FAILED_DEPENDENCY` | ``424`` | WEBDAV, `RFC 2518, Section 10.5 |
333| | | <http://www.webdav.org/specs/rfc2518.html#STATUS_424>`_ |
334+------------------------------------------+---------+-----------------------------------------------------------------------+
335| :const:`UPGRADE_REQUIRED` | ``426`` | HTTP Upgrade to TLS, |
336| | | :rfc:`2817`, Section 6 |
337+------------------------------------------+---------+-----------------------------------------------------------------------+
338| :const:`INTERNAL_SERVER_ERROR` | ``500`` | HTTP/1.1, `RFC 2616, Section |
339| | | 10.5.1 |
340| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.1>`_ |
341+------------------------------------------+---------+-----------------------------------------------------------------------+
342| :const:`NOT_IMPLEMENTED` | ``501`` | HTTP/1.1, `RFC 2616, Section |
343| | | 10.5.2 |
344| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.2>`_ |
345+------------------------------------------+---------+-----------------------------------------------------------------------+
346| :const:`BAD_GATEWAY` | ``502`` | HTTP/1.1 `RFC 2616, Section |
347| | | 10.5.3 |
348| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.3>`_ |
349+------------------------------------------+---------+-----------------------------------------------------------------------+
350| :const:`SERVICE_UNAVAILABLE` | ``503`` | HTTP/1.1, `RFC 2616, Section |
351| | | 10.5.4 |
352| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.4>`_ |
353+------------------------------------------+---------+-----------------------------------------------------------------------+
354| :const:`GATEWAY_TIMEOUT` | ``504`` | HTTP/1.1 `RFC 2616, Section |
355| | | 10.5.5 |
356| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.5>`_ |
357+------------------------------------------+---------+-----------------------------------------------------------------------+
358| :const:`HTTP_VERSION_NOT_SUPPORTED` | ``505`` | HTTP/1.1, `RFC 2616, Section |
359| | | 10.5.6 |
360| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.6>`_ |
361+------------------------------------------+---------+-----------------------------------------------------------------------+
362| :const:`INSUFFICIENT_STORAGE` | ``507`` | WEBDAV, `RFC 2518, Section 10.6 |
363| | | <http://www.webdav.org/specs/rfc2518.html#STATUS_507>`_ |
364+------------------------------------------+---------+-----------------------------------------------------------------------+
365| :const:`NOT_EXTENDED` | ``510`` | An HTTP Extension Framework, |
366| | | :rfc:`2774`, Section 7 |
367+------------------------------------------+---------+-----------------------------------------------------------------------+
368
369
370.. data:: responses
371
372 This dictionary maps the HTTP 1.1 status codes to the W3C names.
373
Georg Brandl24420152008-05-26 16:32:26 +0000374 Example: ``http.client.responses[http.client.NOT_FOUND]`` is ``'Not Found'``.
Georg Brandl116aa622007-08-15 14:28:22 +0000375
Georg Brandl116aa622007-08-15 14:28:22 +0000376
377.. _httpconnection-objects:
378
379HTTPConnection Objects
380----------------------
381
382:class:`HTTPConnection` instances have the following methods:
383
384
Georg Brandl036490d2009-05-17 13:00:36 +0000385.. method:: HTTPConnection.request(method, url, body=None, headers={})
Georg Brandl116aa622007-08-15 14:28:22 +0000386
Jeremy Hylton236654b2009-03-27 20:24:34 +0000387 This will send a request to the server using the HTTP request
388 method *method* and the selector *url*. If the *body* argument is
389 present, it should be string or bytes object of data to send after
390 the headers are finished. Strings are encoded as ISO-8859-1, the
391 default charset for HTTP. To use other encodings, pass a bytes
392 object. The Content-Length header is set to the length of the
393 string.
Georg Brandl116aa622007-08-15 14:28:22 +0000394
Antoine Pitrou11cb9612010-09-15 11:11:28 +0000395 The *body* may also be an open :term:`file object`, in which case the
Senthil Kumaran7bc0d872010-12-19 10:49:52 +0000396 contents of the file is sent; this file object should support ``fileno()``
397 and ``read()`` methods. The header Content-Length is automatically set to
398 the length of the file as reported by stat. The *body* argument may also be
399 an iterable and Contet-Length header should be explicitly provided when the
400 body is an iterable.
Jeremy Hylton236654b2009-03-27 20:24:34 +0000401
402 The *headers* argument should be a mapping of extra HTTP
403 headers to send with the request.
Georg Brandl116aa622007-08-15 14:28:22 +0000404
Senthil Kumaran7bc0d872010-12-19 10:49:52 +0000405 .. versionadded:: 3.2
406 *body* can be an iterable
407
Georg Brandl116aa622007-08-15 14:28:22 +0000408.. method:: HTTPConnection.getresponse()
409
410 Should be called after a request is sent to get the response from the server.
411 Returns an :class:`HTTPResponse` instance.
412
413 .. note::
414
415 Note that you must have read the whole response before you can send a new
416 request to the server.
417
418
419.. method:: HTTPConnection.set_debuglevel(level)
420
R. David Murrayd89bc3f2010-12-15 02:19:14 +0000421 Set the debugging level. The default debug level is ``0``, meaning no
422 debugging output is printed. Any value greater than ``0`` will cause all
423 currently defined debug output to be printed to stdout. The ``debuglevel``
424 is passed to any new :class:`HTTPResponse` objects that are created.
Georg Brandl116aa622007-08-15 14:28:22 +0000425
Mark Dickinson574b1d62009-10-01 20:20:09 +0000426 .. versionadded:: 3.1
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000427
Georg Brandl67b21b72010-08-17 15:07:14 +0000428
Senthil Kumaran7e5229c2009-12-20 07:31:21 +0000429.. method:: HTTPConnection.set_tunnel(host, port=None, headers=None)
Senthil Kumaran97f0c6b2009-07-25 04:24:38 +0000430
431 Set the host and the port for HTTP Connect Tunnelling. Normally used when it
432 is required to a HTTPS Connection through a proxy server.
433
Senthil Kumaran7e5229c2009-12-20 07:31:21 +0000434 The headers argument should be a mapping of extra HTTP headers to to sent
435 with the CONNECT request.
436
Senthil Kumaran2e910fd2009-07-25 04:27:38 +0000437 .. versionadded:: 3.2
Georg Brandl116aa622007-08-15 14:28:22 +0000438
Georg Brandl67b21b72010-08-17 15:07:14 +0000439
Georg Brandl116aa622007-08-15 14:28:22 +0000440.. method:: HTTPConnection.connect()
441
442 Connect to the server specified when the object was created.
443
444
445.. method:: HTTPConnection.close()
446
447 Close the connection to the server.
448
449As an alternative to using the :meth:`request` method described above, you can
450also send your request step by step, by using the four functions below.
451
452
Georg Brandl036490d2009-05-17 13:00:36 +0000453.. method:: HTTPConnection.putrequest(request, selector, skip_host=False, skip_accept_encoding=False)
Georg Brandl116aa622007-08-15 14:28:22 +0000454
455 This should be the first call after the connection to the server has been made.
456 It sends a line to the server consisting of the *request* string, the *selector*
457 string, and the HTTP version (``HTTP/1.1``). To disable automatic sending of
458 ``Host:`` or ``Accept-Encoding:`` headers (for example to accept additional
459 content encodings), specify *skip_host* or *skip_accept_encoding* with non-False
460 values.
461
Georg Brandl116aa622007-08-15 14:28:22 +0000462
463.. method:: HTTPConnection.putheader(header, argument[, ...])
464
465 Send an :rfc:`822`\ -style header to the server. It sends a line to the server
466 consisting of the header, a colon and a space, and the first argument. If more
467 arguments are given, continuation lines are sent, each consisting of a tab and
468 an argument.
469
470
471.. method:: HTTPConnection.endheaders()
472
473 Send a blank line to the server, signalling the end of the headers.
474
475
476.. method:: HTTPConnection.send(data)
477
478 Send data to the server. This should be used directly only after the
479 :meth:`endheaders` method has been called and before :meth:`getresponse` is
480 called.
481
482
483.. _httpresponse-objects:
484
485HTTPResponse Objects
486--------------------
487
Jeremy Hylton1052f892009-03-31 14:40:19 +0000488An :class:`HTTPResponse` instance wraps the HTTP response from the
489server. It provides access to the request headers and the entity
490body. The response is an iterable object and can be used in a with
491statement.
Georg Brandl116aa622007-08-15 14:28:22 +0000492
493
494.. method:: HTTPResponse.read([amt])
495
496 Reads and returns the response body, or up to the next *amt* bytes.
497
498
Georg Brandl036490d2009-05-17 13:00:36 +0000499.. method:: HTTPResponse.getheader(name, default=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000500
Senthil Kumaran790f8312010-08-02 17:09:02 +0000501 Return the value of the header *name*, or *default* if there is no header
502 matching *name*. If there is more than one header with the name *name*,
503 return all of the values joined by ', '. If 'default' is any iterable other
504 than a single string, its elements are similarly returned joined by commas.
Georg Brandl116aa622007-08-15 14:28:22 +0000505
506
507.. method:: HTTPResponse.getheaders()
508
509 Return a list of (header, value) tuples.
510
Senthil Kumaranceff5662010-09-21 01:57:43 +0000511.. method:: HTTPResponse.fileno()
512
513 Return the ``fileno`` of the underlying socket.
Georg Brandl116aa622007-08-15 14:28:22 +0000514
515.. attribute:: HTTPResponse.msg
516
Jeremy Hylton1052f892009-03-31 14:40:19 +0000517 A :class:`http.client.HTTPMessage` instance containing the response
518 headers. :class:`http.client.HTTPMessage` is a subclass of
519 :class:`email.message.Message`.
Georg Brandl116aa622007-08-15 14:28:22 +0000520
521
522.. attribute:: HTTPResponse.version
523
524 HTTP protocol version used by server. 10 for HTTP/1.0, 11 for HTTP/1.1.
525
526
527.. attribute:: HTTPResponse.status
528
529 Status code returned by server.
530
531
532.. attribute:: HTTPResponse.reason
533
534 Reason phrase returned by server.
535
536
Jeremy Hylton1052f892009-03-31 14:40:19 +0000537.. attribute:: HTTPResponse.debuglevel
538
Georg Brandlef871f62010-03-12 10:06:40 +0000539 A debugging hook. If :attr:`debuglevel` is greater than zero, messages
Jeremy Hylton1052f892009-03-31 14:40:19 +0000540 will be printed to stdout as the response is read and parsed.
541
542
Georg Brandl116aa622007-08-15 14:28:22 +0000543Examples
544--------
545
546Here is an example session that uses the ``GET`` method::
547
Georg Brandl24420152008-05-26 16:32:26 +0000548 >>> import http.client
549 >>> conn = http.client.HTTPConnection("www.python.org")
Georg Brandl116aa622007-08-15 14:28:22 +0000550 >>> conn.request("GET", "/index.html")
551 >>> r1 = conn.getresponse()
Georg Brandl6911e3c2007-09-04 07:15:32 +0000552 >>> print(r1.status, r1.reason)
Georg Brandl116aa622007-08-15 14:28:22 +0000553 200 OK
554 >>> data1 = r1.read()
555 >>> conn.request("GET", "/parrot.spam")
556 >>> r2 = conn.getresponse()
Georg Brandl6911e3c2007-09-04 07:15:32 +0000557 >>> print(r2.status, r2.reason)
Georg Brandl116aa622007-08-15 14:28:22 +0000558 404 Not Found
559 >>> data2 = r2.read()
560 >>> conn.close()
561
Fred Drake1587e3d2010-05-12 01:36:11 +0000562Here is an example session that uses the ``HEAD`` method. Note that the
563``HEAD`` method never returns any data. ::
Senthil Kumaran71fb6c82010-04-28 17:39:48 +0000564
565 >>> import http.client
566 >>> conn = http.client.HTTPConnection("www.python.org")
567 >>> conn.request("HEAD","/index.html")
568 >>> res = conn.getresponse()
569 >>> print(res.status, res.reason)
570 200 OK
571 >>> data = res.read()
572 >>> print(len(data))
573 0
574 >>> data == b''
575 True
576
Georg Brandl116aa622007-08-15 14:28:22 +0000577Here is an example session that shows how to ``POST`` requests::
578
Senthil Kumaranaca8fd72008-06-23 04:41:59 +0000579 >>> import http.client, urllib.parse
580 >>> params = urllib.parse.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})
Georg Brandl116aa622007-08-15 14:28:22 +0000581 >>> headers = {"Content-type": "application/x-www-form-urlencoded",
582 ... "Accept": "text/plain"}
Georg Brandl24420152008-05-26 16:32:26 +0000583 >>> conn = http.client.HTTPConnection("musi-cal.mojam.com:80")
Georg Brandl116aa622007-08-15 14:28:22 +0000584 >>> conn.request("POST", "/cgi-bin/query", params, headers)
585 >>> response = conn.getresponse()
Georg Brandl6911e3c2007-09-04 07:15:32 +0000586 >>> print(response.status, response.reason)
Georg Brandl116aa622007-08-15 14:28:22 +0000587 200 OK
588 >>> data = response.read()
589 >>> conn.close()
590
Jeremy Hylton1052f892009-03-31 14:40:19 +0000591
592.. _httpmessage-objects:
593
594HTTPMessage Objects
595-------------------
596
Benjamin Peterson605b9d92009-04-02 00:24:00 +0000597An :class:`http.client.HTTPMessage` instance holds the headers from an HTTP
598response. It is implemented using the :class:`email.message.Message` class.
Jeremy Hylton1052f892009-03-31 14:40:19 +0000599
Benjamin Peterson605b9d92009-04-02 00:24:00 +0000600.. XXX Define the methods that clients can depend upon between versions.