blob: 90e16a7325ec6ec6f6690ed5fa8c0c3d40a0c95b [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 Pitrou05188422010-10-12 16:44:39 +000020 HTTPS support is only available if the Python was compiled with SSL
21 support (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
Georg Brandl116aa622007-08-15 14:28:22 +000079
Georg Brandl036490d2009-05-17 13:00:36 +000080.. class:: HTTPResponse(sock, debuglevel=0, strict=0, method=None, url=None)
Georg Brandl116aa622007-08-15 14:28:22 +000081
Jeremy Hylton1052f892009-03-31 14:40:19 +000082 Class whose instances are returned upon successful connection. Not
83 instantiated directly by user.
Georg Brandl116aa622007-08-15 14:28:22 +000084
Georg Brandl116aa622007-08-15 14:28:22 +000085
86The following exceptions are raised as appropriate:
87
88
89.. exception:: HTTPException
90
91 The base class of the other exceptions in this module. It is a subclass of
92 :exc:`Exception`.
93
Georg Brandl116aa622007-08-15 14:28:22 +000094
95.. exception:: NotConnected
96
97 A subclass of :exc:`HTTPException`.
98
Georg Brandl116aa622007-08-15 14:28:22 +000099
100.. exception:: InvalidURL
101
102 A subclass of :exc:`HTTPException`, raised if a port is given and is either
103 non-numeric or empty.
104
Georg Brandl116aa622007-08-15 14:28:22 +0000105
106.. exception:: UnknownProtocol
107
108 A subclass of :exc:`HTTPException`.
109
Georg Brandl116aa622007-08-15 14:28:22 +0000110
111.. exception:: UnknownTransferEncoding
112
113 A subclass of :exc:`HTTPException`.
114
Georg Brandl116aa622007-08-15 14:28:22 +0000115
116.. exception:: UnimplementedFileMode
117
118 A subclass of :exc:`HTTPException`.
119
Georg Brandl116aa622007-08-15 14:28:22 +0000120
121.. exception:: IncompleteRead
122
123 A subclass of :exc:`HTTPException`.
124
Georg Brandl116aa622007-08-15 14:28:22 +0000125
126.. exception:: ImproperConnectionState
127
128 A subclass of :exc:`HTTPException`.
129
Georg Brandl116aa622007-08-15 14:28:22 +0000130
131.. exception:: CannotSendRequest
132
133 A subclass of :exc:`ImproperConnectionState`.
134
Georg Brandl116aa622007-08-15 14:28:22 +0000135
136.. exception:: CannotSendHeader
137
138 A subclass of :exc:`ImproperConnectionState`.
139
Georg Brandl116aa622007-08-15 14:28:22 +0000140
141.. exception:: ResponseNotReady
142
143 A subclass of :exc:`ImproperConnectionState`.
144
Georg Brandl116aa622007-08-15 14:28:22 +0000145
146.. exception:: BadStatusLine
147
148 A subclass of :exc:`HTTPException`. Raised if a server responds with a HTTP
149 status code that we don't understand.
150
Georg Brandl116aa622007-08-15 14:28:22 +0000151The constants defined in this module are:
152
153
154.. data:: HTTP_PORT
155
156 The default port for the HTTP protocol (always ``80``).
157
158
159.. data:: HTTPS_PORT
160
161 The default port for the HTTPS protocol (always ``443``).
162
163and also the following constants for integer status codes:
164
165+------------------------------------------+---------+-----------------------------------------------------------------------+
166| Constant | Value | Definition |
167+==========================================+=========+=======================================================================+
168| :const:`CONTINUE` | ``100`` | HTTP/1.1, `RFC 2616, Section |
169| | | 10.1.1 |
170| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.1.1>`_ |
171+------------------------------------------+---------+-----------------------------------------------------------------------+
172| :const:`SWITCHING_PROTOCOLS` | ``101`` | HTTP/1.1, `RFC 2616, Section |
173| | | 10.1.2 |
174| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.1.2>`_ |
175+------------------------------------------+---------+-----------------------------------------------------------------------+
176| :const:`PROCESSING` | ``102`` | WEBDAV, `RFC 2518, Section 10.1 |
177| | | <http://www.webdav.org/specs/rfc2518.html#STATUS_102>`_ |
178+------------------------------------------+---------+-----------------------------------------------------------------------+
179| :const:`OK` | ``200`` | HTTP/1.1, `RFC 2616, Section |
180| | | 10.2.1 |
181| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.1>`_ |
182+------------------------------------------+---------+-----------------------------------------------------------------------+
183| :const:`CREATED` | ``201`` | HTTP/1.1, `RFC 2616, Section |
184| | | 10.2.2 |
185| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.2>`_ |
186+------------------------------------------+---------+-----------------------------------------------------------------------+
187| :const:`ACCEPTED` | ``202`` | HTTP/1.1, `RFC 2616, Section |
188| | | 10.2.3 |
189| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.3>`_ |
190+------------------------------------------+---------+-----------------------------------------------------------------------+
191| :const:`NON_AUTHORITATIVE_INFORMATION` | ``203`` | HTTP/1.1, `RFC 2616, Section |
192| | | 10.2.4 |
193| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.4>`_ |
194+------------------------------------------+---------+-----------------------------------------------------------------------+
195| :const:`NO_CONTENT` | ``204`` | HTTP/1.1, `RFC 2616, Section |
196| | | 10.2.5 |
197| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.5>`_ |
198+------------------------------------------+---------+-----------------------------------------------------------------------+
199| :const:`RESET_CONTENT` | ``205`` | HTTP/1.1, `RFC 2616, Section |
200| | | 10.2.6 |
201| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.6>`_ |
202+------------------------------------------+---------+-----------------------------------------------------------------------+
203| :const:`PARTIAL_CONTENT` | ``206`` | HTTP/1.1, `RFC 2616, Section |
204| | | 10.2.7 |
205| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.7>`_ |
206+------------------------------------------+---------+-----------------------------------------------------------------------+
207| :const:`MULTI_STATUS` | ``207`` | WEBDAV `RFC 2518, Section 10.2 |
208| | | <http://www.webdav.org/specs/rfc2518.html#STATUS_207>`_ |
209+------------------------------------------+---------+-----------------------------------------------------------------------+
210| :const:`IM_USED` | ``226`` | Delta encoding in HTTP, |
211| | | :rfc:`3229`, Section 10.4.1 |
212+------------------------------------------+---------+-----------------------------------------------------------------------+
213| :const:`MULTIPLE_CHOICES` | ``300`` | HTTP/1.1, `RFC 2616, Section |
214| | | 10.3.1 |
215| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.1>`_ |
216+------------------------------------------+---------+-----------------------------------------------------------------------+
217| :const:`MOVED_PERMANENTLY` | ``301`` | HTTP/1.1, `RFC 2616, Section |
218| | | 10.3.2 |
219| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.2>`_ |
220+------------------------------------------+---------+-----------------------------------------------------------------------+
221| :const:`FOUND` | ``302`` | HTTP/1.1, `RFC 2616, Section |
222| | | 10.3.3 |
223| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.3>`_ |
224+------------------------------------------+---------+-----------------------------------------------------------------------+
225| :const:`SEE_OTHER` | ``303`` | HTTP/1.1, `RFC 2616, Section |
226| | | 10.3.4 |
227| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.4>`_ |
228+------------------------------------------+---------+-----------------------------------------------------------------------+
229| :const:`NOT_MODIFIED` | ``304`` | HTTP/1.1, `RFC 2616, Section |
230| | | 10.3.5 |
231| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.5>`_ |
232+------------------------------------------+---------+-----------------------------------------------------------------------+
233| :const:`USE_PROXY` | ``305`` | HTTP/1.1, `RFC 2616, Section |
234| | | 10.3.6 |
235| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.6>`_ |
236+------------------------------------------+---------+-----------------------------------------------------------------------+
237| :const:`TEMPORARY_REDIRECT` | ``307`` | HTTP/1.1, `RFC 2616, Section |
238| | | 10.3.8 |
239| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.8>`_ |
240+------------------------------------------+---------+-----------------------------------------------------------------------+
241| :const:`BAD_REQUEST` | ``400`` | HTTP/1.1, `RFC 2616, Section |
242| | | 10.4.1 |
243| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.1>`_ |
244+------------------------------------------+---------+-----------------------------------------------------------------------+
245| :const:`UNAUTHORIZED` | ``401`` | HTTP/1.1, `RFC 2616, Section |
246| | | 10.4.2 |
247| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.2>`_ |
248+------------------------------------------+---------+-----------------------------------------------------------------------+
249| :const:`PAYMENT_REQUIRED` | ``402`` | HTTP/1.1, `RFC 2616, Section |
250| | | 10.4.3 |
251| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.3>`_ |
252+------------------------------------------+---------+-----------------------------------------------------------------------+
253| :const:`FORBIDDEN` | ``403`` | HTTP/1.1, `RFC 2616, Section |
254| | | 10.4.4 |
255| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.4>`_ |
256+------------------------------------------+---------+-----------------------------------------------------------------------+
257| :const:`NOT_FOUND` | ``404`` | HTTP/1.1, `RFC 2616, Section |
258| | | 10.4.5 |
259| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.5>`_ |
260+------------------------------------------+---------+-----------------------------------------------------------------------+
261| :const:`METHOD_NOT_ALLOWED` | ``405`` | HTTP/1.1, `RFC 2616, Section |
262| | | 10.4.6 |
263| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.6>`_ |
264+------------------------------------------+---------+-----------------------------------------------------------------------+
265| :const:`NOT_ACCEPTABLE` | ``406`` | HTTP/1.1, `RFC 2616, Section |
266| | | 10.4.7 |
267| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.7>`_ |
268+------------------------------------------+---------+-----------------------------------------------------------------------+
269| :const:`PROXY_AUTHENTICATION_REQUIRED` | ``407`` | HTTP/1.1, `RFC 2616, Section |
270| | | 10.4.8 |
271| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.8>`_ |
272+------------------------------------------+---------+-----------------------------------------------------------------------+
273| :const:`REQUEST_TIMEOUT` | ``408`` | HTTP/1.1, `RFC 2616, Section |
274| | | 10.4.9 |
275| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.9>`_ |
276+------------------------------------------+---------+-----------------------------------------------------------------------+
277| :const:`CONFLICT` | ``409`` | HTTP/1.1, `RFC 2616, Section |
278| | | 10.4.10 |
279| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.10>`_ |
280+------------------------------------------+---------+-----------------------------------------------------------------------+
281| :const:`GONE` | ``410`` | HTTP/1.1, `RFC 2616, Section |
282| | | 10.4.11 |
283| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.11>`_ |
284+------------------------------------------+---------+-----------------------------------------------------------------------+
285| :const:`LENGTH_REQUIRED` | ``411`` | HTTP/1.1, `RFC 2616, Section |
286| | | 10.4.12 |
287| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.12>`_ |
288+------------------------------------------+---------+-----------------------------------------------------------------------+
289| :const:`PRECONDITION_FAILED` | ``412`` | HTTP/1.1, `RFC 2616, Section |
290| | | 10.4.13 |
291| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.13>`_ |
292+------------------------------------------+---------+-----------------------------------------------------------------------+
293| :const:`REQUEST_ENTITY_TOO_LARGE` | ``413`` | HTTP/1.1, `RFC 2616, Section |
294| | | 10.4.14 |
295| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.14>`_ |
296+------------------------------------------+---------+-----------------------------------------------------------------------+
297| :const:`REQUEST_URI_TOO_LONG` | ``414`` | HTTP/1.1, `RFC 2616, Section |
298| | | 10.4.15 |
299| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.15>`_ |
300+------------------------------------------+---------+-----------------------------------------------------------------------+
301| :const:`UNSUPPORTED_MEDIA_TYPE` | ``415`` | HTTP/1.1, `RFC 2616, Section |
302| | | 10.4.16 |
303| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.16>`_ |
304+------------------------------------------+---------+-----------------------------------------------------------------------+
305| :const:`REQUESTED_RANGE_NOT_SATISFIABLE` | ``416`` | HTTP/1.1, `RFC 2616, Section |
306| | | 10.4.17 |
307| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.17>`_ |
308+------------------------------------------+---------+-----------------------------------------------------------------------+
309| :const:`EXPECTATION_FAILED` | ``417`` | HTTP/1.1, `RFC 2616, Section |
310| | | 10.4.18 |
311| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.18>`_ |
312+------------------------------------------+---------+-----------------------------------------------------------------------+
313| :const:`UNPROCESSABLE_ENTITY` | ``422`` | WEBDAV, `RFC 2518, Section 10.3 |
314| | | <http://www.webdav.org/specs/rfc2518.html#STATUS_422>`_ |
315+------------------------------------------+---------+-----------------------------------------------------------------------+
316| :const:`LOCKED` | ``423`` | WEBDAV `RFC 2518, Section 10.4 |
317| | | <http://www.webdav.org/specs/rfc2518.html#STATUS_423>`_ |
318+------------------------------------------+---------+-----------------------------------------------------------------------+
319| :const:`FAILED_DEPENDENCY` | ``424`` | WEBDAV, `RFC 2518, Section 10.5 |
320| | | <http://www.webdav.org/specs/rfc2518.html#STATUS_424>`_ |
321+------------------------------------------+---------+-----------------------------------------------------------------------+
322| :const:`UPGRADE_REQUIRED` | ``426`` | HTTP Upgrade to TLS, |
323| | | :rfc:`2817`, Section 6 |
324+------------------------------------------+---------+-----------------------------------------------------------------------+
325| :const:`INTERNAL_SERVER_ERROR` | ``500`` | HTTP/1.1, `RFC 2616, Section |
326| | | 10.5.1 |
327| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.1>`_ |
328+------------------------------------------+---------+-----------------------------------------------------------------------+
329| :const:`NOT_IMPLEMENTED` | ``501`` | HTTP/1.1, `RFC 2616, Section |
330| | | 10.5.2 |
331| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.2>`_ |
332+------------------------------------------+---------+-----------------------------------------------------------------------+
333| :const:`BAD_GATEWAY` | ``502`` | HTTP/1.1 `RFC 2616, Section |
334| | | 10.5.3 |
335| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.3>`_ |
336+------------------------------------------+---------+-----------------------------------------------------------------------+
337| :const:`SERVICE_UNAVAILABLE` | ``503`` | HTTP/1.1, `RFC 2616, Section |
338| | | 10.5.4 |
339| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.4>`_ |
340+------------------------------------------+---------+-----------------------------------------------------------------------+
341| :const:`GATEWAY_TIMEOUT` | ``504`` | HTTP/1.1 `RFC 2616, Section |
342| | | 10.5.5 |
343| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.5>`_ |
344+------------------------------------------+---------+-----------------------------------------------------------------------+
345| :const:`HTTP_VERSION_NOT_SUPPORTED` | ``505`` | HTTP/1.1, `RFC 2616, Section |
346| | | 10.5.6 |
347| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.6>`_ |
348+------------------------------------------+---------+-----------------------------------------------------------------------+
349| :const:`INSUFFICIENT_STORAGE` | ``507`` | WEBDAV, `RFC 2518, Section 10.6 |
350| | | <http://www.webdav.org/specs/rfc2518.html#STATUS_507>`_ |
351+------------------------------------------+---------+-----------------------------------------------------------------------+
352| :const:`NOT_EXTENDED` | ``510`` | An HTTP Extension Framework, |
353| | | :rfc:`2774`, Section 7 |
354+------------------------------------------+---------+-----------------------------------------------------------------------+
355
356
357.. data:: responses
358
359 This dictionary maps the HTTP 1.1 status codes to the W3C names.
360
Georg Brandl24420152008-05-26 16:32:26 +0000361 Example: ``http.client.responses[http.client.NOT_FOUND]`` is ``'Not Found'``.
Georg Brandl116aa622007-08-15 14:28:22 +0000362
Georg Brandl116aa622007-08-15 14:28:22 +0000363
364.. _httpconnection-objects:
365
366HTTPConnection Objects
367----------------------
368
369:class:`HTTPConnection` instances have the following methods:
370
371
Georg Brandl036490d2009-05-17 13:00:36 +0000372.. method:: HTTPConnection.request(method, url, body=None, headers={})
Georg Brandl116aa622007-08-15 14:28:22 +0000373
Jeremy Hylton236654b2009-03-27 20:24:34 +0000374 This will send a request to the server using the HTTP request
375 method *method* and the selector *url*. If the *body* argument is
376 present, it should be string or bytes object of data to send after
377 the headers are finished. Strings are encoded as ISO-8859-1, the
378 default charset for HTTP. To use other encodings, pass a bytes
379 object. The Content-Length header is set to the length of the
380 string.
Georg Brandl116aa622007-08-15 14:28:22 +0000381
Antoine Pitrou11cb9612010-09-15 11:11:28 +0000382 The *body* may also be an open :term:`file object`, in which case the
Jeremy Hylton236654b2009-03-27 20:24:34 +0000383 contents of the file is sent; this file object should support
384 ``fileno()`` and ``read()`` methods. The header Content-Length is
385 automatically set to the length of the file as reported by
386 stat.
387
388 The *headers* argument should be a mapping of extra HTTP
389 headers to send with the request.
Georg Brandl116aa622007-08-15 14:28:22 +0000390
391.. method:: HTTPConnection.getresponse()
392
393 Should be called after a request is sent to get the response from the server.
394 Returns an :class:`HTTPResponse` instance.
395
396 .. note::
397
398 Note that you must have read the whole response before you can send a new
399 request to the server.
400
401
402.. method:: HTTPConnection.set_debuglevel(level)
403
404 Set the debugging level (the amount of debugging output printed). The default
405 debug level is ``0``, meaning no debugging output is printed.
406
Mark Dickinson574b1d62009-10-01 20:20:09 +0000407 .. versionadded:: 3.1
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000408
Georg Brandl67b21b72010-08-17 15:07:14 +0000409
Senthil Kumaran7e5229c2009-12-20 07:31:21 +0000410.. method:: HTTPConnection.set_tunnel(host, port=None, headers=None)
Senthil Kumaran97f0c6b2009-07-25 04:24:38 +0000411
412 Set the host and the port for HTTP Connect Tunnelling. Normally used when it
413 is required to a HTTPS Connection through a proxy server.
414
Senthil Kumaran7e5229c2009-12-20 07:31:21 +0000415 The headers argument should be a mapping of extra HTTP headers to to sent
416 with the CONNECT request.
417
Senthil Kumaran2e910fd2009-07-25 04:27:38 +0000418 .. versionadded:: 3.2
Georg Brandl116aa622007-08-15 14:28:22 +0000419
Georg Brandl67b21b72010-08-17 15:07:14 +0000420
Georg Brandl116aa622007-08-15 14:28:22 +0000421.. method:: HTTPConnection.connect()
422
423 Connect to the server specified when the object was created.
424
425
426.. method:: HTTPConnection.close()
427
428 Close the connection to the server.
429
430As an alternative to using the :meth:`request` method described above, you can
431also send your request step by step, by using the four functions below.
432
433
Georg Brandl036490d2009-05-17 13:00:36 +0000434.. method:: HTTPConnection.putrequest(request, selector, skip_host=False, skip_accept_encoding=False)
Georg Brandl116aa622007-08-15 14:28:22 +0000435
436 This should be the first call after the connection to the server has been made.
437 It sends a line to the server consisting of the *request* string, the *selector*
438 string, and the HTTP version (``HTTP/1.1``). To disable automatic sending of
439 ``Host:`` or ``Accept-Encoding:`` headers (for example to accept additional
440 content encodings), specify *skip_host* or *skip_accept_encoding* with non-False
441 values.
442
Georg Brandl116aa622007-08-15 14:28:22 +0000443
444.. method:: HTTPConnection.putheader(header, argument[, ...])
445
446 Send an :rfc:`822`\ -style header to the server. It sends a line to the server
447 consisting of the header, a colon and a space, and the first argument. If more
448 arguments are given, continuation lines are sent, each consisting of a tab and
449 an argument.
450
451
452.. method:: HTTPConnection.endheaders()
453
454 Send a blank line to the server, signalling the end of the headers.
455
456
457.. method:: HTTPConnection.send(data)
458
459 Send data to the server. This should be used directly only after the
460 :meth:`endheaders` method has been called and before :meth:`getresponse` is
461 called.
462
463
464.. _httpresponse-objects:
465
466HTTPResponse Objects
467--------------------
468
Jeremy Hylton1052f892009-03-31 14:40:19 +0000469An :class:`HTTPResponse` instance wraps the HTTP response from the
470server. It provides access to the request headers and the entity
471body. The response is an iterable object and can be used in a with
472statement.
Georg Brandl116aa622007-08-15 14:28:22 +0000473
474
475.. method:: HTTPResponse.read([amt])
476
477 Reads and returns the response body, or up to the next *amt* bytes.
478
479
Georg Brandl036490d2009-05-17 13:00:36 +0000480.. method:: HTTPResponse.getheader(name, default=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000481
Senthil Kumaran790f8312010-08-02 17:09:02 +0000482 Return the value of the header *name*, or *default* if there is no header
483 matching *name*. If there is more than one header with the name *name*,
484 return all of the values joined by ', '. If 'default' is any iterable other
485 than a single string, its elements are similarly returned joined by commas.
Georg Brandl116aa622007-08-15 14:28:22 +0000486
487
488.. method:: HTTPResponse.getheaders()
489
490 Return a list of (header, value) tuples.
491
Senthil Kumaranceff5662010-09-21 01:57:43 +0000492.. method:: HTTPResponse.fileno()
493
494 Return the ``fileno`` of the underlying socket.
Georg Brandl116aa622007-08-15 14:28:22 +0000495
496.. attribute:: HTTPResponse.msg
497
Jeremy Hylton1052f892009-03-31 14:40:19 +0000498 A :class:`http.client.HTTPMessage` instance containing the response
499 headers. :class:`http.client.HTTPMessage` is a subclass of
500 :class:`email.message.Message`.
Georg Brandl116aa622007-08-15 14:28:22 +0000501
502
503.. attribute:: HTTPResponse.version
504
505 HTTP protocol version used by server. 10 for HTTP/1.0, 11 for HTTP/1.1.
506
507
508.. attribute:: HTTPResponse.status
509
510 Status code returned by server.
511
512
513.. attribute:: HTTPResponse.reason
514
515 Reason phrase returned by server.
516
517
Jeremy Hylton1052f892009-03-31 14:40:19 +0000518.. attribute:: HTTPResponse.debuglevel
519
Georg Brandlef871f62010-03-12 10:06:40 +0000520 A debugging hook. If :attr:`debuglevel` is greater than zero, messages
Jeremy Hylton1052f892009-03-31 14:40:19 +0000521 will be printed to stdout as the response is read and parsed.
522
523
Georg Brandl116aa622007-08-15 14:28:22 +0000524Examples
525--------
526
527Here is an example session that uses the ``GET`` method::
528
Georg Brandl24420152008-05-26 16:32:26 +0000529 >>> import http.client
530 >>> conn = http.client.HTTPConnection("www.python.org")
Georg Brandl116aa622007-08-15 14:28:22 +0000531 >>> conn.request("GET", "/index.html")
532 >>> r1 = conn.getresponse()
Georg Brandl6911e3c2007-09-04 07:15:32 +0000533 >>> print(r1.status, r1.reason)
Georg Brandl116aa622007-08-15 14:28:22 +0000534 200 OK
535 >>> data1 = r1.read()
536 >>> conn.request("GET", "/parrot.spam")
537 >>> r2 = conn.getresponse()
Georg Brandl6911e3c2007-09-04 07:15:32 +0000538 >>> print(r2.status, r2.reason)
Georg Brandl116aa622007-08-15 14:28:22 +0000539 404 Not Found
540 >>> data2 = r2.read()
541 >>> conn.close()
542
Fred Drake1587e3d2010-05-12 01:36:11 +0000543Here is an example session that uses the ``HEAD`` method. Note that the
544``HEAD`` method never returns any data. ::
Senthil Kumaran71fb6c82010-04-28 17:39:48 +0000545
546 >>> import http.client
547 >>> conn = http.client.HTTPConnection("www.python.org")
548 >>> conn.request("HEAD","/index.html")
549 >>> res = conn.getresponse()
550 >>> print(res.status, res.reason)
551 200 OK
552 >>> data = res.read()
553 >>> print(len(data))
554 0
555 >>> data == b''
556 True
557
Georg Brandl116aa622007-08-15 14:28:22 +0000558Here is an example session that shows how to ``POST`` requests::
559
Senthil Kumaranaca8fd72008-06-23 04:41:59 +0000560 >>> import http.client, urllib.parse
561 >>> params = urllib.parse.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})
Georg Brandl116aa622007-08-15 14:28:22 +0000562 >>> headers = {"Content-type": "application/x-www-form-urlencoded",
563 ... "Accept": "text/plain"}
Georg Brandl24420152008-05-26 16:32:26 +0000564 >>> conn = http.client.HTTPConnection("musi-cal.mojam.com:80")
Georg Brandl116aa622007-08-15 14:28:22 +0000565 >>> conn.request("POST", "/cgi-bin/query", params, headers)
566 >>> response = conn.getresponse()
Georg Brandl6911e3c2007-09-04 07:15:32 +0000567 >>> print(response.status, response.reason)
Georg Brandl116aa622007-08-15 14:28:22 +0000568 200 OK
569 >>> data = response.read()
570 >>> conn.close()
571
Jeremy Hylton1052f892009-03-31 14:40:19 +0000572
573.. _httpmessage-objects:
574
575HTTPMessage Objects
576-------------------
577
Benjamin Peterson605b9d92009-04-02 00:24:00 +0000578An :class:`http.client.HTTPMessage` instance holds the headers from an HTTP
579response. It is implemented using the :class:`email.message.Message` class.
Jeremy Hylton1052f892009-03-31 14:40:19 +0000580
Benjamin Peterson605b9d92009-04-02 00:24:00 +0000581.. XXX Define the methods that clients can depend upon between versions.