blob: 714ebf3047e27d5228fce5a68f94d442a13e90b4 [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
Gregory P. Smithb4066372010-01-03 03:28:29 +000026.. class:: HTTPConnection(host, port=None, strict=None[, 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
Benjamin Petersonf608c612008-11-16 18:33:53 +000032 used. When True, the optional parameter *strict* (which defaults to a false
33 value) causes ``BadStatusLine`` to
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +000034 be raised if the status line can't be parsed as a valid HTTP/1.0 or 1.1
35 status line. If the optional *timeout* parameter is given, blocking
36 operations (like connection attempts) will timeout after that many seconds
Georg Brandlf78e02b2008-06-10 17:40:04 +000037 (if it is not given, the global default timeout setting is used).
Gregory P. Smithb4066372010-01-03 03:28:29 +000038 The optional *source_address* parameter may be a typle of a (host, port)
39 to use as the source address the HTTP connection is made from.
Georg Brandl116aa622007-08-15 14:28:22 +000040
41 For example, the following calls all create instances that connect to the server
42 at the same host and port::
43
Georg Brandl24420152008-05-26 16:32:26 +000044 >>> h1 = http.client.HTTPConnection('www.cwi.nl')
45 >>> h2 = http.client.HTTPConnection('www.cwi.nl:80')
46 >>> h3 = http.client.HTTPConnection('www.cwi.nl', 80)
47 >>> h3 = http.client.HTTPConnection('www.cwi.nl', 80, timeout=10)
Georg Brandl116aa622007-08-15 14:28:22 +000048
Gregory P. Smithb4066372010-01-03 03:28:29 +000049 .. versionchanged:: 3.2
50 *source_address* was added.
Georg Brandl116aa622007-08-15 14:28:22 +000051
Gregory P. Smithb4066372010-01-03 03:28:29 +000052
Antoine Pitrou803e6d62010-10-13 10:36:15 +000053.. class:: HTTPSConnection(host, port=None, key_file=None, cert_file=None, strict=None[, timeout[, source_address]], *, context=None, check_hostname=None)
Georg Brandl116aa622007-08-15 14:28:22 +000054
55 A subclass of :class:`HTTPConnection` that uses SSL for communication with
Antoine Pitrou803e6d62010-10-13 10:36:15 +000056 secure servers. Default port is ``443``. If *context* is specified, it
57 must be a :class:`ssl.SSLContext` instance describing the various SSL
58 options. If *context* is specified and has a :attr:`~ssl.SSLContext.verify_mode`
59 of either :data:`~ssl.CERT_OPTIONAL` or :data:`~ssl.CERT_REQUIRED`, then
60 by default *host* is matched against the host name(s) allowed by the
61 server's certificate. If you want to change that behaviour, you can
62 explicitly set *check_hostname* to False.
Georg Brandl116aa622007-08-15 14:28:22 +000063
Antoine Pitrou803e6d62010-10-13 10:36:15 +000064 *key_file* and *cert_file* are deprecated, please use
65 :meth:`ssl.SSLContext.load_cert_chain` instead.
66
67 If you access arbitrary hosts on the Internet, it is recommended to
68 require certificate checking and feed the *context* with a set of
69 trusted CA certificates::
70
71 context = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
72 context.verify_mode = ssl.CERT_REQUIRED
73 context.load_verify_locations('/etc/pki/tls/certs/ca-bundle.crt')
74 h = client.HTTPSConnection('svn.python.org', 443, context=context)
Georg Brandl116aa622007-08-15 14:28:22 +000075
Gregory P. Smithb4066372010-01-03 03:28:29 +000076 .. versionchanged:: 3.2
Antoine Pitrou803e6d62010-10-13 10:36:15 +000077 *source_address*, *context* and *check_hostname* were added.
Gregory P. Smithb4066372010-01-03 03:28:29 +000078
Antoine Pitroud5323212010-10-22 18:19:07 +000079 .. versionchanged:: 3.2
80 This class now supports HTTPS virtual hosts if possible (that is,
81 if :data:`ssl.HAS_SNI` is true).
82
Georg Brandl116aa622007-08-15 14:28:22 +000083
Georg Brandl036490d2009-05-17 13:00:36 +000084.. class:: HTTPResponse(sock, debuglevel=0, strict=0, method=None, url=None)
Georg Brandl116aa622007-08-15 14:28:22 +000085
Jeremy Hylton1052f892009-03-31 14:40:19 +000086 Class whose instances are returned upon successful connection. Not
87 instantiated directly by user.
Georg Brandl116aa622007-08-15 14:28:22 +000088
Georg Brandl116aa622007-08-15 14:28:22 +000089
90The following exceptions are raised as appropriate:
91
92
93.. exception:: HTTPException
94
95 The base class of the other exceptions in this module. It is a subclass of
96 :exc:`Exception`.
97
Georg Brandl116aa622007-08-15 14:28:22 +000098
99.. exception:: NotConnected
100
101 A subclass of :exc:`HTTPException`.
102
Georg Brandl116aa622007-08-15 14:28:22 +0000103
104.. exception:: InvalidURL
105
106 A subclass of :exc:`HTTPException`, raised if a port is given and is either
107 non-numeric or empty.
108
Georg Brandl116aa622007-08-15 14:28:22 +0000109
110.. exception:: UnknownProtocol
111
112 A subclass of :exc:`HTTPException`.
113
Georg Brandl116aa622007-08-15 14:28:22 +0000114
115.. exception:: UnknownTransferEncoding
116
117 A subclass of :exc:`HTTPException`.
118
Georg Brandl116aa622007-08-15 14:28:22 +0000119
120.. exception:: UnimplementedFileMode
121
122 A subclass of :exc:`HTTPException`.
123
Georg Brandl116aa622007-08-15 14:28:22 +0000124
125.. exception:: IncompleteRead
126
127 A subclass of :exc:`HTTPException`.
128
Georg Brandl116aa622007-08-15 14:28:22 +0000129
130.. exception:: ImproperConnectionState
131
132 A subclass of :exc:`HTTPException`.
133
Georg Brandl116aa622007-08-15 14:28:22 +0000134
135.. exception:: CannotSendRequest
136
137 A subclass of :exc:`ImproperConnectionState`.
138
Georg Brandl116aa622007-08-15 14:28:22 +0000139
140.. exception:: CannotSendHeader
141
142 A subclass of :exc:`ImproperConnectionState`.
143
Georg Brandl116aa622007-08-15 14:28:22 +0000144
145.. exception:: ResponseNotReady
146
147 A subclass of :exc:`ImproperConnectionState`.
148
Georg Brandl116aa622007-08-15 14:28:22 +0000149
150.. exception:: BadStatusLine
151
152 A subclass of :exc:`HTTPException`. Raised if a server responds with a HTTP
153 status code that we don't understand.
154
Georg Brandl116aa622007-08-15 14:28:22 +0000155The constants defined in this module are:
156
157
158.. data:: HTTP_PORT
159
160 The default port for the HTTP protocol (always ``80``).
161
162
163.. data:: HTTPS_PORT
164
165 The default port for the HTTPS protocol (always ``443``).
166
167and also the following constants for integer status codes:
168
169+------------------------------------------+---------+-----------------------------------------------------------------------+
170| Constant | Value | Definition |
171+==========================================+=========+=======================================================================+
172| :const:`CONTINUE` | ``100`` | HTTP/1.1, `RFC 2616, Section |
173| | | 10.1.1 |
174| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.1.1>`_ |
175+------------------------------------------+---------+-----------------------------------------------------------------------+
176| :const:`SWITCHING_PROTOCOLS` | ``101`` | HTTP/1.1, `RFC 2616, Section |
177| | | 10.1.2 |
178| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.1.2>`_ |
179+------------------------------------------+---------+-----------------------------------------------------------------------+
180| :const:`PROCESSING` | ``102`` | WEBDAV, `RFC 2518, Section 10.1 |
181| | | <http://www.webdav.org/specs/rfc2518.html#STATUS_102>`_ |
182+------------------------------------------+---------+-----------------------------------------------------------------------+
183| :const:`OK` | ``200`` | HTTP/1.1, `RFC 2616, Section |
184| | | 10.2.1 |
185| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.1>`_ |
186+------------------------------------------+---------+-----------------------------------------------------------------------+
187| :const:`CREATED` | ``201`` | HTTP/1.1, `RFC 2616, Section |
188| | | 10.2.2 |
189| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.2>`_ |
190+------------------------------------------+---------+-----------------------------------------------------------------------+
191| :const:`ACCEPTED` | ``202`` | HTTP/1.1, `RFC 2616, Section |
192| | | 10.2.3 |
193| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.3>`_ |
194+------------------------------------------+---------+-----------------------------------------------------------------------+
195| :const:`NON_AUTHORITATIVE_INFORMATION` | ``203`` | HTTP/1.1, `RFC 2616, Section |
196| | | 10.2.4 |
197| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.4>`_ |
198+------------------------------------------+---------+-----------------------------------------------------------------------+
199| :const:`NO_CONTENT` | ``204`` | HTTP/1.1, `RFC 2616, Section |
200| | | 10.2.5 |
201| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.5>`_ |
202+------------------------------------------+---------+-----------------------------------------------------------------------+
203| :const:`RESET_CONTENT` | ``205`` | HTTP/1.1, `RFC 2616, Section |
204| | | 10.2.6 |
205| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.6>`_ |
206+------------------------------------------+---------+-----------------------------------------------------------------------+
207| :const:`PARTIAL_CONTENT` | ``206`` | HTTP/1.1, `RFC 2616, Section |
208| | | 10.2.7 |
209| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.7>`_ |
210+------------------------------------------+---------+-----------------------------------------------------------------------+
211| :const:`MULTI_STATUS` | ``207`` | WEBDAV `RFC 2518, Section 10.2 |
212| | | <http://www.webdav.org/specs/rfc2518.html#STATUS_207>`_ |
213+------------------------------------------+---------+-----------------------------------------------------------------------+
214| :const:`IM_USED` | ``226`` | Delta encoding in HTTP, |
215| | | :rfc:`3229`, Section 10.4.1 |
216+------------------------------------------+---------+-----------------------------------------------------------------------+
217| :const:`MULTIPLE_CHOICES` | ``300`` | HTTP/1.1, `RFC 2616, Section |
218| | | 10.3.1 |
219| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.1>`_ |
220+------------------------------------------+---------+-----------------------------------------------------------------------+
221| :const:`MOVED_PERMANENTLY` | ``301`` | HTTP/1.1, `RFC 2616, Section |
222| | | 10.3.2 |
223| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.2>`_ |
224+------------------------------------------+---------+-----------------------------------------------------------------------+
225| :const:`FOUND` | ``302`` | HTTP/1.1, `RFC 2616, Section |
226| | | 10.3.3 |
227| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.3>`_ |
228+------------------------------------------+---------+-----------------------------------------------------------------------+
229| :const:`SEE_OTHER` | ``303`` | HTTP/1.1, `RFC 2616, Section |
230| | | 10.3.4 |
231| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.4>`_ |
232+------------------------------------------+---------+-----------------------------------------------------------------------+
233| :const:`NOT_MODIFIED` | ``304`` | HTTP/1.1, `RFC 2616, Section |
234| | | 10.3.5 |
235| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.5>`_ |
236+------------------------------------------+---------+-----------------------------------------------------------------------+
237| :const:`USE_PROXY` | ``305`` | HTTP/1.1, `RFC 2616, Section |
238| | | 10.3.6 |
239| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.6>`_ |
240+------------------------------------------+---------+-----------------------------------------------------------------------+
241| :const:`TEMPORARY_REDIRECT` | ``307`` | HTTP/1.1, `RFC 2616, Section |
242| | | 10.3.8 |
243| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.8>`_ |
244+------------------------------------------+---------+-----------------------------------------------------------------------+
245| :const:`BAD_REQUEST` | ``400`` | HTTP/1.1, `RFC 2616, Section |
246| | | 10.4.1 |
247| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.1>`_ |
248+------------------------------------------+---------+-----------------------------------------------------------------------+
249| :const:`UNAUTHORIZED` | ``401`` | HTTP/1.1, `RFC 2616, Section |
250| | | 10.4.2 |
251| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.2>`_ |
252+------------------------------------------+---------+-----------------------------------------------------------------------+
253| :const:`PAYMENT_REQUIRED` | ``402`` | HTTP/1.1, `RFC 2616, Section |
254| | | 10.4.3 |
255| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.3>`_ |
256+------------------------------------------+---------+-----------------------------------------------------------------------+
257| :const:`FORBIDDEN` | ``403`` | HTTP/1.1, `RFC 2616, Section |
258| | | 10.4.4 |
259| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.4>`_ |
260+------------------------------------------+---------+-----------------------------------------------------------------------+
261| :const:`NOT_FOUND` | ``404`` | HTTP/1.1, `RFC 2616, Section |
262| | | 10.4.5 |
263| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.5>`_ |
264+------------------------------------------+---------+-----------------------------------------------------------------------+
265| :const:`METHOD_NOT_ALLOWED` | ``405`` | HTTP/1.1, `RFC 2616, Section |
266| | | 10.4.6 |
267| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.6>`_ |
268+------------------------------------------+---------+-----------------------------------------------------------------------+
269| :const:`NOT_ACCEPTABLE` | ``406`` | HTTP/1.1, `RFC 2616, Section |
270| | | 10.4.7 |
271| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.7>`_ |
272+------------------------------------------+---------+-----------------------------------------------------------------------+
273| :const:`PROXY_AUTHENTICATION_REQUIRED` | ``407`` | HTTP/1.1, `RFC 2616, Section |
274| | | 10.4.8 |
275| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.8>`_ |
276+------------------------------------------+---------+-----------------------------------------------------------------------+
277| :const:`REQUEST_TIMEOUT` | ``408`` | HTTP/1.1, `RFC 2616, Section |
278| | | 10.4.9 |
279| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.9>`_ |
280+------------------------------------------+---------+-----------------------------------------------------------------------+
281| :const:`CONFLICT` | ``409`` | HTTP/1.1, `RFC 2616, Section |
282| | | 10.4.10 |
283| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.10>`_ |
284+------------------------------------------+---------+-----------------------------------------------------------------------+
285| :const:`GONE` | ``410`` | HTTP/1.1, `RFC 2616, Section |
286| | | 10.4.11 |
287| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.11>`_ |
288+------------------------------------------+---------+-----------------------------------------------------------------------+
289| :const:`LENGTH_REQUIRED` | ``411`` | HTTP/1.1, `RFC 2616, Section |
290| | | 10.4.12 |
291| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.12>`_ |
292+------------------------------------------+---------+-----------------------------------------------------------------------+
293| :const:`PRECONDITION_FAILED` | ``412`` | HTTP/1.1, `RFC 2616, Section |
294| | | 10.4.13 |
295| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.13>`_ |
296+------------------------------------------+---------+-----------------------------------------------------------------------+
297| :const:`REQUEST_ENTITY_TOO_LARGE` | ``413`` | HTTP/1.1, `RFC 2616, Section |
298| | | 10.4.14 |
299| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.14>`_ |
300+------------------------------------------+---------+-----------------------------------------------------------------------+
301| :const:`REQUEST_URI_TOO_LONG` | ``414`` | HTTP/1.1, `RFC 2616, Section |
302| | | 10.4.15 |
303| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.15>`_ |
304+------------------------------------------+---------+-----------------------------------------------------------------------+
305| :const:`UNSUPPORTED_MEDIA_TYPE` | ``415`` | HTTP/1.1, `RFC 2616, Section |
306| | | 10.4.16 |
307| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.16>`_ |
308+------------------------------------------+---------+-----------------------------------------------------------------------+
309| :const:`REQUESTED_RANGE_NOT_SATISFIABLE` | ``416`` | HTTP/1.1, `RFC 2616, Section |
310| | | 10.4.17 |
311| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.17>`_ |
312+------------------------------------------+---------+-----------------------------------------------------------------------+
313| :const:`EXPECTATION_FAILED` | ``417`` | HTTP/1.1, `RFC 2616, Section |
314| | | 10.4.18 |
315| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.18>`_ |
316+------------------------------------------+---------+-----------------------------------------------------------------------+
317| :const:`UNPROCESSABLE_ENTITY` | ``422`` | WEBDAV, `RFC 2518, Section 10.3 |
318| | | <http://www.webdav.org/specs/rfc2518.html#STATUS_422>`_ |
319+------------------------------------------+---------+-----------------------------------------------------------------------+
320| :const:`LOCKED` | ``423`` | WEBDAV `RFC 2518, Section 10.4 |
321| | | <http://www.webdav.org/specs/rfc2518.html#STATUS_423>`_ |
322+------------------------------------------+---------+-----------------------------------------------------------------------+
323| :const:`FAILED_DEPENDENCY` | ``424`` | WEBDAV, `RFC 2518, Section 10.5 |
324| | | <http://www.webdav.org/specs/rfc2518.html#STATUS_424>`_ |
325+------------------------------------------+---------+-----------------------------------------------------------------------+
326| :const:`UPGRADE_REQUIRED` | ``426`` | HTTP Upgrade to TLS, |
327| | | :rfc:`2817`, Section 6 |
328+------------------------------------------+---------+-----------------------------------------------------------------------+
329| :const:`INTERNAL_SERVER_ERROR` | ``500`` | HTTP/1.1, `RFC 2616, Section |
330| | | 10.5.1 |
331| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.1>`_ |
332+------------------------------------------+---------+-----------------------------------------------------------------------+
333| :const:`NOT_IMPLEMENTED` | ``501`` | HTTP/1.1, `RFC 2616, Section |
334| | | 10.5.2 |
335| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.2>`_ |
336+------------------------------------------+---------+-----------------------------------------------------------------------+
337| :const:`BAD_GATEWAY` | ``502`` | HTTP/1.1 `RFC 2616, Section |
338| | | 10.5.3 |
339| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.3>`_ |
340+------------------------------------------+---------+-----------------------------------------------------------------------+
341| :const:`SERVICE_UNAVAILABLE` | ``503`` | HTTP/1.1, `RFC 2616, Section |
342| | | 10.5.4 |
343| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.4>`_ |
344+------------------------------------------+---------+-----------------------------------------------------------------------+
345| :const:`GATEWAY_TIMEOUT` | ``504`` | HTTP/1.1 `RFC 2616, Section |
346| | | 10.5.5 |
347| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.5>`_ |
348+------------------------------------------+---------+-----------------------------------------------------------------------+
349| :const:`HTTP_VERSION_NOT_SUPPORTED` | ``505`` | HTTP/1.1, `RFC 2616, Section |
350| | | 10.5.6 |
351| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.6>`_ |
352+------------------------------------------+---------+-----------------------------------------------------------------------+
353| :const:`INSUFFICIENT_STORAGE` | ``507`` | WEBDAV, `RFC 2518, Section 10.6 |
354| | | <http://www.webdav.org/specs/rfc2518.html#STATUS_507>`_ |
355+------------------------------------------+---------+-----------------------------------------------------------------------+
356| :const:`NOT_EXTENDED` | ``510`` | An HTTP Extension Framework, |
357| | | :rfc:`2774`, Section 7 |
358+------------------------------------------+---------+-----------------------------------------------------------------------+
359
360
361.. data:: responses
362
363 This dictionary maps the HTTP 1.1 status codes to the W3C names.
364
Georg Brandl24420152008-05-26 16:32:26 +0000365 Example: ``http.client.responses[http.client.NOT_FOUND]`` is ``'Not Found'``.
Georg Brandl116aa622007-08-15 14:28:22 +0000366
Georg Brandl116aa622007-08-15 14:28:22 +0000367
368.. _httpconnection-objects:
369
370HTTPConnection Objects
371----------------------
372
373:class:`HTTPConnection` instances have the following methods:
374
375
Georg Brandl036490d2009-05-17 13:00:36 +0000376.. method:: HTTPConnection.request(method, url, body=None, headers={})
Georg Brandl116aa622007-08-15 14:28:22 +0000377
Jeremy Hylton236654b2009-03-27 20:24:34 +0000378 This will send a request to the server using the HTTP request
379 method *method* and the selector *url*. If the *body* argument is
380 present, it should be string or bytes object of data to send after
381 the headers are finished. Strings are encoded as ISO-8859-1, the
382 default charset for HTTP. To use other encodings, pass a bytes
383 object. The Content-Length header is set to the length of the
384 string.
Georg Brandl116aa622007-08-15 14:28:22 +0000385
Antoine Pitrou11cb9612010-09-15 11:11:28 +0000386 The *body* may also be an open :term:`file object`, in which case the
Jeremy Hylton236654b2009-03-27 20:24:34 +0000387 contents of the file is sent; this file object should support
388 ``fileno()`` and ``read()`` methods. The header Content-Length is
389 automatically set to the length of the file as reported by
390 stat.
391
392 The *headers* argument should be a mapping of extra HTTP
393 headers to send with the request.
Georg Brandl116aa622007-08-15 14:28:22 +0000394
395.. method:: HTTPConnection.getresponse()
396
397 Should be called after a request is sent to get the response from the server.
398 Returns an :class:`HTTPResponse` instance.
399
400 .. note::
401
402 Note that you must have read the whole response before you can send a new
403 request to the server.
404
405
406.. method:: HTTPConnection.set_debuglevel(level)
407
408 Set the debugging level (the amount of debugging output printed). The default
409 debug level is ``0``, meaning no debugging output is printed.
410
Mark Dickinson574b1d62009-10-01 20:20:09 +0000411 .. versionadded:: 3.1
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000412
Georg Brandl67b21b72010-08-17 15:07:14 +0000413
Senthil Kumaran7e5229c2009-12-20 07:31:21 +0000414.. method:: HTTPConnection.set_tunnel(host, port=None, headers=None)
Senthil Kumaran97f0c6b2009-07-25 04:24:38 +0000415
416 Set the host and the port for HTTP Connect Tunnelling. Normally used when it
417 is required to a HTTPS Connection through a proxy server.
418
Senthil Kumaran7e5229c2009-12-20 07:31:21 +0000419 The headers argument should be a mapping of extra HTTP headers to to sent
420 with the CONNECT request.
421
Senthil Kumaran2e910fd2009-07-25 04:27:38 +0000422 .. versionadded:: 3.2
Georg Brandl116aa622007-08-15 14:28:22 +0000423
Georg Brandl67b21b72010-08-17 15:07:14 +0000424
Georg Brandl116aa622007-08-15 14:28:22 +0000425.. method:: HTTPConnection.connect()
426
427 Connect to the server specified when the object was created.
428
429
430.. method:: HTTPConnection.close()
431
432 Close the connection to the server.
433
434As an alternative to using the :meth:`request` method described above, you can
435also send your request step by step, by using the four functions below.
436
437
Georg Brandl036490d2009-05-17 13:00:36 +0000438.. method:: HTTPConnection.putrequest(request, selector, skip_host=False, skip_accept_encoding=False)
Georg Brandl116aa622007-08-15 14:28:22 +0000439
440 This should be the first call after the connection to the server has been made.
441 It sends a line to the server consisting of the *request* string, the *selector*
442 string, and the HTTP version (``HTTP/1.1``). To disable automatic sending of
443 ``Host:`` or ``Accept-Encoding:`` headers (for example to accept additional
444 content encodings), specify *skip_host* or *skip_accept_encoding* with non-False
445 values.
446
Georg Brandl116aa622007-08-15 14:28:22 +0000447
448.. method:: HTTPConnection.putheader(header, argument[, ...])
449
450 Send an :rfc:`822`\ -style header to the server. It sends a line to the server
451 consisting of the header, a colon and a space, and the first argument. If more
452 arguments are given, continuation lines are sent, each consisting of a tab and
453 an argument.
454
455
456.. method:: HTTPConnection.endheaders()
457
458 Send a blank line to the server, signalling the end of the headers.
459
460
461.. method:: HTTPConnection.send(data)
462
463 Send data to the server. This should be used directly only after the
464 :meth:`endheaders` method has been called and before :meth:`getresponse` is
465 called.
466
467
468.. _httpresponse-objects:
469
470HTTPResponse Objects
471--------------------
472
Jeremy Hylton1052f892009-03-31 14:40:19 +0000473An :class:`HTTPResponse` instance wraps the HTTP response from the
474server. It provides access to the request headers and the entity
475body. The response is an iterable object and can be used in a with
476statement.
Georg Brandl116aa622007-08-15 14:28:22 +0000477
478
479.. method:: HTTPResponse.read([amt])
480
481 Reads and returns the response body, or up to the next *amt* bytes.
482
483
Georg Brandl036490d2009-05-17 13:00:36 +0000484.. method:: HTTPResponse.getheader(name, default=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000485
Senthil Kumaran790f8312010-08-02 17:09:02 +0000486 Return the value of the header *name*, or *default* if there is no header
487 matching *name*. If there is more than one header with the name *name*,
488 return all of the values joined by ', '. If 'default' is any iterable other
489 than a single string, its elements are similarly returned joined by commas.
Georg Brandl116aa622007-08-15 14:28:22 +0000490
491
492.. method:: HTTPResponse.getheaders()
493
494 Return a list of (header, value) tuples.
495
Senthil Kumaranceff5662010-09-21 01:57:43 +0000496.. method:: HTTPResponse.fileno()
497
498 Return the ``fileno`` of the underlying socket.
Georg Brandl116aa622007-08-15 14:28:22 +0000499
500.. attribute:: HTTPResponse.msg
501
Jeremy Hylton1052f892009-03-31 14:40:19 +0000502 A :class:`http.client.HTTPMessage` instance containing the response
503 headers. :class:`http.client.HTTPMessage` is a subclass of
504 :class:`email.message.Message`.
Georg Brandl116aa622007-08-15 14:28:22 +0000505
506
507.. attribute:: HTTPResponse.version
508
509 HTTP protocol version used by server. 10 for HTTP/1.0, 11 for HTTP/1.1.
510
511
512.. attribute:: HTTPResponse.status
513
514 Status code returned by server.
515
516
517.. attribute:: HTTPResponse.reason
518
519 Reason phrase returned by server.
520
521
Jeremy Hylton1052f892009-03-31 14:40:19 +0000522.. attribute:: HTTPResponse.debuglevel
523
Georg Brandlef871f62010-03-12 10:06:40 +0000524 A debugging hook. If :attr:`debuglevel` is greater than zero, messages
Jeremy Hylton1052f892009-03-31 14:40:19 +0000525 will be printed to stdout as the response is read and parsed.
526
527
Georg Brandl116aa622007-08-15 14:28:22 +0000528Examples
529--------
530
531Here is an example session that uses the ``GET`` method::
532
Georg Brandl24420152008-05-26 16:32:26 +0000533 >>> import http.client
534 >>> conn = http.client.HTTPConnection("www.python.org")
Georg Brandl116aa622007-08-15 14:28:22 +0000535 >>> conn.request("GET", "/index.html")
536 >>> r1 = conn.getresponse()
Georg Brandl6911e3c2007-09-04 07:15:32 +0000537 >>> print(r1.status, r1.reason)
Georg Brandl116aa622007-08-15 14:28:22 +0000538 200 OK
539 >>> data1 = r1.read()
540 >>> conn.request("GET", "/parrot.spam")
541 >>> r2 = conn.getresponse()
Georg Brandl6911e3c2007-09-04 07:15:32 +0000542 >>> print(r2.status, r2.reason)
Georg Brandl116aa622007-08-15 14:28:22 +0000543 404 Not Found
544 >>> data2 = r2.read()
545 >>> conn.close()
546
Fred Drake1587e3d2010-05-12 01:36:11 +0000547Here is an example session that uses the ``HEAD`` method. Note that the
548``HEAD`` method never returns any data. ::
Senthil Kumaran71fb6c82010-04-28 17:39:48 +0000549
550 >>> import http.client
551 >>> conn = http.client.HTTPConnection("www.python.org")
552 >>> conn.request("HEAD","/index.html")
553 >>> res = conn.getresponse()
554 >>> print(res.status, res.reason)
555 200 OK
556 >>> data = res.read()
557 >>> print(len(data))
558 0
559 >>> data == b''
560 True
561
Georg Brandl116aa622007-08-15 14:28:22 +0000562Here is an example session that shows how to ``POST`` requests::
563
Senthil Kumaranaca8fd72008-06-23 04:41:59 +0000564 >>> import http.client, urllib.parse
565 >>> params = urllib.parse.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})
Georg Brandl116aa622007-08-15 14:28:22 +0000566 >>> headers = {"Content-type": "application/x-www-form-urlencoded",
567 ... "Accept": "text/plain"}
Georg Brandl24420152008-05-26 16:32:26 +0000568 >>> conn = http.client.HTTPConnection("musi-cal.mojam.com:80")
Georg Brandl116aa622007-08-15 14:28:22 +0000569 >>> conn.request("POST", "/cgi-bin/query", params, headers)
570 >>> response = conn.getresponse()
Georg Brandl6911e3c2007-09-04 07:15:32 +0000571 >>> print(response.status, response.reason)
Georg Brandl116aa622007-08-15 14:28:22 +0000572 200 OK
573 >>> data = response.read()
574 >>> conn.close()
575
Jeremy Hylton1052f892009-03-31 14:40:19 +0000576
577.. _httpmessage-objects:
578
579HTTPMessage Objects
580-------------------
581
Benjamin Peterson605b9d92009-04-02 00:24:00 +0000582An :class:`http.client.HTTPMessage` instance holds the headers from an HTTP
583response. It is implemented using the :class:`email.message.Message` class.
Jeremy Hylton1052f892009-03-31 14:40:19 +0000584
Benjamin Peterson605b9d92009-04-02 00:24:00 +0000585.. XXX Define the methods that clients can depend upon between versions.