blob: 67d7271016f93323bae13c5e8e45eac316eff42c [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
Jeremy Hylton236654b2009-03-27 20:24:34 +0000396 contents of the file is sent; this file object should support
397 ``fileno()`` and ``read()`` methods. The header Content-Length is
398 automatically set to the length of the file as reported by
399 stat.
400
401 The *headers* argument should be a mapping of extra HTTP
402 headers to send with the request.
Georg Brandl116aa622007-08-15 14:28:22 +0000403
404.. method:: HTTPConnection.getresponse()
405
406 Should be called after a request is sent to get the response from the server.
407 Returns an :class:`HTTPResponse` instance.
408
409 .. note::
410
411 Note that you must have read the whole response before you can send a new
412 request to the server.
413
414
415.. method:: HTTPConnection.set_debuglevel(level)
416
R. David Murrayd89bc3f2010-12-15 02:19:14 +0000417 Set the debugging level. The default debug level is ``0``, meaning no
418 debugging output is printed. Any value greater than ``0`` will cause all
419 currently defined debug output to be printed to stdout. The ``debuglevel``
420 is passed to any new :class:`HTTPResponse` objects that are created.
Georg Brandl116aa622007-08-15 14:28:22 +0000421
Mark Dickinson574b1d62009-10-01 20:20:09 +0000422 .. versionadded:: 3.1
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000423
Georg Brandl67b21b72010-08-17 15:07:14 +0000424
Senthil Kumaran7e5229c2009-12-20 07:31:21 +0000425.. method:: HTTPConnection.set_tunnel(host, port=None, headers=None)
Senthil Kumaran97f0c6b2009-07-25 04:24:38 +0000426
427 Set the host and the port for HTTP Connect Tunnelling. Normally used when it
428 is required to a HTTPS Connection through a proxy server.
429
Senthil Kumaran7e5229c2009-12-20 07:31:21 +0000430 The headers argument should be a mapping of extra HTTP headers to to sent
431 with the CONNECT request.
432
Senthil Kumaran2e910fd2009-07-25 04:27:38 +0000433 .. versionadded:: 3.2
Georg Brandl116aa622007-08-15 14:28:22 +0000434
Georg Brandl67b21b72010-08-17 15:07:14 +0000435
Georg Brandl116aa622007-08-15 14:28:22 +0000436.. method:: HTTPConnection.connect()
437
438 Connect to the server specified when the object was created.
439
440
441.. method:: HTTPConnection.close()
442
443 Close the connection to the server.
444
445As an alternative to using the :meth:`request` method described above, you can
446also send your request step by step, by using the four functions below.
447
448
Georg Brandl036490d2009-05-17 13:00:36 +0000449.. method:: HTTPConnection.putrequest(request, selector, skip_host=False, skip_accept_encoding=False)
Georg Brandl116aa622007-08-15 14:28:22 +0000450
451 This should be the first call after the connection to the server has been made.
452 It sends a line to the server consisting of the *request* string, the *selector*
453 string, and the HTTP version (``HTTP/1.1``). To disable automatic sending of
454 ``Host:`` or ``Accept-Encoding:`` headers (for example to accept additional
455 content encodings), specify *skip_host* or *skip_accept_encoding* with non-False
456 values.
457
Georg Brandl116aa622007-08-15 14:28:22 +0000458
459.. method:: HTTPConnection.putheader(header, argument[, ...])
460
461 Send an :rfc:`822`\ -style header to the server. It sends a line to the server
462 consisting of the header, a colon and a space, and the first argument. If more
463 arguments are given, continuation lines are sent, each consisting of a tab and
464 an argument.
465
466
467.. method:: HTTPConnection.endheaders()
468
469 Send a blank line to the server, signalling the end of the headers.
470
471
472.. method:: HTTPConnection.send(data)
473
474 Send data to the server. This should be used directly only after the
475 :meth:`endheaders` method has been called and before :meth:`getresponse` is
476 called.
477
478
479.. _httpresponse-objects:
480
481HTTPResponse Objects
482--------------------
483
Jeremy Hylton1052f892009-03-31 14:40:19 +0000484An :class:`HTTPResponse` instance wraps the HTTP response from the
485server. It provides access to the request headers and the entity
486body. The response is an iterable object and can be used in a with
487statement.
Georg Brandl116aa622007-08-15 14:28:22 +0000488
489
490.. method:: HTTPResponse.read([amt])
491
492 Reads and returns the response body, or up to the next *amt* bytes.
493
494
Georg Brandl036490d2009-05-17 13:00:36 +0000495.. method:: HTTPResponse.getheader(name, default=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000496
Senthil Kumaran790f8312010-08-02 17:09:02 +0000497 Return the value of the header *name*, or *default* if there is no header
498 matching *name*. If there is more than one header with the name *name*,
499 return all of the values joined by ', '. If 'default' is any iterable other
500 than a single string, its elements are similarly returned joined by commas.
Georg Brandl116aa622007-08-15 14:28:22 +0000501
502
503.. method:: HTTPResponse.getheaders()
504
505 Return a list of (header, value) tuples.
506
Senthil Kumaranceff5662010-09-21 01:57:43 +0000507.. method:: HTTPResponse.fileno()
508
509 Return the ``fileno`` of the underlying socket.
Georg Brandl116aa622007-08-15 14:28:22 +0000510
511.. attribute:: HTTPResponse.msg
512
Jeremy Hylton1052f892009-03-31 14:40:19 +0000513 A :class:`http.client.HTTPMessage` instance containing the response
514 headers. :class:`http.client.HTTPMessage` is a subclass of
515 :class:`email.message.Message`.
Georg Brandl116aa622007-08-15 14:28:22 +0000516
517
518.. attribute:: HTTPResponse.version
519
520 HTTP protocol version used by server. 10 for HTTP/1.0, 11 for HTTP/1.1.
521
522
523.. attribute:: HTTPResponse.status
524
525 Status code returned by server.
526
527
528.. attribute:: HTTPResponse.reason
529
530 Reason phrase returned by server.
531
532
Jeremy Hylton1052f892009-03-31 14:40:19 +0000533.. attribute:: HTTPResponse.debuglevel
534
Georg Brandlef871f62010-03-12 10:06:40 +0000535 A debugging hook. If :attr:`debuglevel` is greater than zero, messages
Jeremy Hylton1052f892009-03-31 14:40:19 +0000536 will be printed to stdout as the response is read and parsed.
537
538
Georg Brandl116aa622007-08-15 14:28:22 +0000539Examples
540--------
541
542Here is an example session that uses the ``GET`` method::
543
Georg Brandl24420152008-05-26 16:32:26 +0000544 >>> import http.client
545 >>> conn = http.client.HTTPConnection("www.python.org")
Georg Brandl116aa622007-08-15 14:28:22 +0000546 >>> conn.request("GET", "/index.html")
547 >>> r1 = conn.getresponse()
Georg Brandl6911e3c2007-09-04 07:15:32 +0000548 >>> print(r1.status, r1.reason)
Georg Brandl116aa622007-08-15 14:28:22 +0000549 200 OK
550 >>> data1 = r1.read()
551 >>> conn.request("GET", "/parrot.spam")
552 >>> r2 = conn.getresponse()
Georg Brandl6911e3c2007-09-04 07:15:32 +0000553 >>> print(r2.status, r2.reason)
Georg Brandl116aa622007-08-15 14:28:22 +0000554 404 Not Found
555 >>> data2 = r2.read()
556 >>> conn.close()
557
Fred Drake1587e3d2010-05-12 01:36:11 +0000558Here is an example session that uses the ``HEAD`` method. Note that the
559``HEAD`` method never returns any data. ::
Senthil Kumaran71fb6c82010-04-28 17:39:48 +0000560
561 >>> import http.client
562 >>> conn = http.client.HTTPConnection("www.python.org")
563 >>> conn.request("HEAD","/index.html")
564 >>> res = conn.getresponse()
565 >>> print(res.status, res.reason)
566 200 OK
567 >>> data = res.read()
568 >>> print(len(data))
569 0
570 >>> data == b''
571 True
572
Georg Brandl116aa622007-08-15 14:28:22 +0000573Here is an example session that shows how to ``POST`` requests::
574
Senthil Kumaranaca8fd72008-06-23 04:41:59 +0000575 >>> import http.client, urllib.parse
576 >>> params = urllib.parse.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})
Georg Brandl116aa622007-08-15 14:28:22 +0000577 >>> headers = {"Content-type": "application/x-www-form-urlencoded",
578 ... "Accept": "text/plain"}
Georg Brandl24420152008-05-26 16:32:26 +0000579 >>> conn = http.client.HTTPConnection("musi-cal.mojam.com:80")
Georg Brandl116aa622007-08-15 14:28:22 +0000580 >>> conn.request("POST", "/cgi-bin/query", params, headers)
581 >>> response = conn.getresponse()
Georg Brandl6911e3c2007-09-04 07:15:32 +0000582 >>> print(response.status, response.reason)
Georg Brandl116aa622007-08-15 14:28:22 +0000583 200 OK
584 >>> data = response.read()
585 >>> conn.close()
586
Jeremy Hylton1052f892009-03-31 14:40:19 +0000587
588.. _httpmessage-objects:
589
590HTTPMessage Objects
591-------------------
592
Benjamin Peterson605b9d92009-04-02 00:24:00 +0000593An :class:`http.client.HTTPMessage` instance holds the headers from an HTTP
594response. It is implemented using the :class:`email.message.Message` class.
Jeremy Hylton1052f892009-03-31 14:40:19 +0000595
Benjamin Peterson605b9d92009-04-02 00:24:00 +0000596.. XXX Define the methods that clients can depend upon between versions.