blob: a29e3644a554a11b9952386a16aaa9f6ec2f36d9 [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
20 HTTPS support is only available if the :mod:`socket` module was compiled with
21 SSL support.
22
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
53.. class:: HTTPSConnection(host, port=None, key_file=None, cert_file=None, strict=None[, timeout[, source_address]])
Georg Brandl116aa622007-08-15 14:28:22 +000054
55 A subclass of :class:`HTTPConnection` that uses SSL for communication with
56 secure servers. Default port is ``443``. *key_file* is the name of a PEM
57 formatted file that contains your private key. *cert_file* is a PEM formatted
58 certificate chain file.
59
Georg Brandle720c0a2009-04-27 16:20:50 +000060 .. note::
Georg Brandl116aa622007-08-15 14:28:22 +000061
Georg Brandle720c0a2009-04-27 16:20:50 +000062 This does not do any certificate verification.
Georg Brandl116aa622007-08-15 14:28:22 +000063
Gregory P. Smithb4066372010-01-03 03:28:29 +000064 .. versionchanged:: 3.2
65 *source_address* was added.
66
Georg Brandl116aa622007-08-15 14:28:22 +000067
Georg Brandl036490d2009-05-17 13:00:36 +000068.. class:: HTTPResponse(sock, debuglevel=0, strict=0, method=None, url=None)
Georg Brandl116aa622007-08-15 14:28:22 +000069
Jeremy Hylton1052f892009-03-31 14:40:19 +000070 Class whose instances are returned upon successful connection. Not
71 instantiated directly by user.
Georg Brandl116aa622007-08-15 14:28:22 +000072
Georg Brandl116aa622007-08-15 14:28:22 +000073
74The following exceptions are raised as appropriate:
75
76
77.. exception:: HTTPException
78
79 The base class of the other exceptions in this module. It is a subclass of
80 :exc:`Exception`.
81
Georg Brandl116aa622007-08-15 14:28:22 +000082
83.. exception:: NotConnected
84
85 A subclass of :exc:`HTTPException`.
86
Georg Brandl116aa622007-08-15 14:28:22 +000087
88.. exception:: InvalidURL
89
90 A subclass of :exc:`HTTPException`, raised if a port is given and is either
91 non-numeric or empty.
92
Georg Brandl116aa622007-08-15 14:28:22 +000093
94.. exception:: UnknownProtocol
95
96 A subclass of :exc:`HTTPException`.
97
Georg Brandl116aa622007-08-15 14:28:22 +000098
99.. exception:: UnknownTransferEncoding
100
101 A subclass of :exc:`HTTPException`.
102
Georg Brandl116aa622007-08-15 14:28:22 +0000103
104.. exception:: UnimplementedFileMode
105
106 A subclass of :exc:`HTTPException`.
107
Georg Brandl116aa622007-08-15 14:28:22 +0000108
109.. exception:: IncompleteRead
110
111 A subclass of :exc:`HTTPException`.
112
Georg Brandl116aa622007-08-15 14:28:22 +0000113
114.. exception:: ImproperConnectionState
115
116 A subclass of :exc:`HTTPException`.
117
Georg Brandl116aa622007-08-15 14:28:22 +0000118
119.. exception:: CannotSendRequest
120
121 A subclass of :exc:`ImproperConnectionState`.
122
Georg Brandl116aa622007-08-15 14:28:22 +0000123
124.. exception:: CannotSendHeader
125
126 A subclass of :exc:`ImproperConnectionState`.
127
Georg Brandl116aa622007-08-15 14:28:22 +0000128
129.. exception:: ResponseNotReady
130
131 A subclass of :exc:`ImproperConnectionState`.
132
Georg Brandl116aa622007-08-15 14:28:22 +0000133
134.. exception:: BadStatusLine
135
136 A subclass of :exc:`HTTPException`. Raised if a server responds with a HTTP
137 status code that we don't understand.
138
Georg Brandl116aa622007-08-15 14:28:22 +0000139The constants defined in this module are:
140
141
142.. data:: HTTP_PORT
143
144 The default port for the HTTP protocol (always ``80``).
145
146
147.. data:: HTTPS_PORT
148
149 The default port for the HTTPS protocol (always ``443``).
150
151and also the following constants for integer status codes:
152
153+------------------------------------------+---------+-----------------------------------------------------------------------+
154| Constant | Value | Definition |
155+==========================================+=========+=======================================================================+
156| :const:`CONTINUE` | ``100`` | HTTP/1.1, `RFC 2616, Section |
157| | | 10.1.1 |
158| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.1.1>`_ |
159+------------------------------------------+---------+-----------------------------------------------------------------------+
160| :const:`SWITCHING_PROTOCOLS` | ``101`` | HTTP/1.1, `RFC 2616, Section |
161| | | 10.1.2 |
162| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.1.2>`_ |
163+------------------------------------------+---------+-----------------------------------------------------------------------+
164| :const:`PROCESSING` | ``102`` | WEBDAV, `RFC 2518, Section 10.1 |
165| | | <http://www.webdav.org/specs/rfc2518.html#STATUS_102>`_ |
166+------------------------------------------+---------+-----------------------------------------------------------------------+
167| :const:`OK` | ``200`` | HTTP/1.1, `RFC 2616, Section |
168| | | 10.2.1 |
169| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.1>`_ |
170+------------------------------------------+---------+-----------------------------------------------------------------------+
171| :const:`CREATED` | ``201`` | HTTP/1.1, `RFC 2616, Section |
172| | | 10.2.2 |
173| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.2>`_ |
174+------------------------------------------+---------+-----------------------------------------------------------------------+
175| :const:`ACCEPTED` | ``202`` | HTTP/1.1, `RFC 2616, Section |
176| | | 10.2.3 |
177| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.3>`_ |
178+------------------------------------------+---------+-----------------------------------------------------------------------+
179| :const:`NON_AUTHORITATIVE_INFORMATION` | ``203`` | HTTP/1.1, `RFC 2616, Section |
180| | | 10.2.4 |
181| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.4>`_ |
182+------------------------------------------+---------+-----------------------------------------------------------------------+
183| :const:`NO_CONTENT` | ``204`` | HTTP/1.1, `RFC 2616, Section |
184| | | 10.2.5 |
185| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.5>`_ |
186+------------------------------------------+---------+-----------------------------------------------------------------------+
187| :const:`RESET_CONTENT` | ``205`` | HTTP/1.1, `RFC 2616, Section |
188| | | 10.2.6 |
189| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.6>`_ |
190+------------------------------------------+---------+-----------------------------------------------------------------------+
191| :const:`PARTIAL_CONTENT` | ``206`` | HTTP/1.1, `RFC 2616, Section |
192| | | 10.2.7 |
193| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.7>`_ |
194+------------------------------------------+---------+-----------------------------------------------------------------------+
195| :const:`MULTI_STATUS` | ``207`` | WEBDAV `RFC 2518, Section 10.2 |
196| | | <http://www.webdav.org/specs/rfc2518.html#STATUS_207>`_ |
197+------------------------------------------+---------+-----------------------------------------------------------------------+
198| :const:`IM_USED` | ``226`` | Delta encoding in HTTP, |
199| | | :rfc:`3229`, Section 10.4.1 |
200+------------------------------------------+---------+-----------------------------------------------------------------------+
201| :const:`MULTIPLE_CHOICES` | ``300`` | HTTP/1.1, `RFC 2616, Section |
202| | | 10.3.1 |
203| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.1>`_ |
204+------------------------------------------+---------+-----------------------------------------------------------------------+
205| :const:`MOVED_PERMANENTLY` | ``301`` | HTTP/1.1, `RFC 2616, Section |
206| | | 10.3.2 |
207| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.2>`_ |
208+------------------------------------------+---------+-----------------------------------------------------------------------+
209| :const:`FOUND` | ``302`` | HTTP/1.1, `RFC 2616, Section |
210| | | 10.3.3 |
211| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.3>`_ |
212+------------------------------------------+---------+-----------------------------------------------------------------------+
213| :const:`SEE_OTHER` | ``303`` | HTTP/1.1, `RFC 2616, Section |
214| | | 10.3.4 |
215| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.4>`_ |
216+------------------------------------------+---------+-----------------------------------------------------------------------+
217| :const:`NOT_MODIFIED` | ``304`` | HTTP/1.1, `RFC 2616, Section |
218| | | 10.3.5 |
219| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.5>`_ |
220+------------------------------------------+---------+-----------------------------------------------------------------------+
221| :const:`USE_PROXY` | ``305`` | HTTP/1.1, `RFC 2616, Section |
222| | | 10.3.6 |
223| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.6>`_ |
224+------------------------------------------+---------+-----------------------------------------------------------------------+
225| :const:`TEMPORARY_REDIRECT` | ``307`` | HTTP/1.1, `RFC 2616, Section |
226| | | 10.3.8 |
227| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.8>`_ |
228+------------------------------------------+---------+-----------------------------------------------------------------------+
229| :const:`BAD_REQUEST` | ``400`` | HTTP/1.1, `RFC 2616, Section |
230| | | 10.4.1 |
231| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.1>`_ |
232+------------------------------------------+---------+-----------------------------------------------------------------------+
233| :const:`UNAUTHORIZED` | ``401`` | HTTP/1.1, `RFC 2616, Section |
234| | | 10.4.2 |
235| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.2>`_ |
236+------------------------------------------+---------+-----------------------------------------------------------------------+
237| :const:`PAYMENT_REQUIRED` | ``402`` | HTTP/1.1, `RFC 2616, Section |
238| | | 10.4.3 |
239| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.3>`_ |
240+------------------------------------------+---------+-----------------------------------------------------------------------+
241| :const:`FORBIDDEN` | ``403`` | HTTP/1.1, `RFC 2616, Section |
242| | | 10.4.4 |
243| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.4>`_ |
244+------------------------------------------+---------+-----------------------------------------------------------------------+
245| :const:`NOT_FOUND` | ``404`` | HTTP/1.1, `RFC 2616, Section |
246| | | 10.4.5 |
247| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.5>`_ |
248+------------------------------------------+---------+-----------------------------------------------------------------------+
249| :const:`METHOD_NOT_ALLOWED` | ``405`` | HTTP/1.1, `RFC 2616, Section |
250| | | 10.4.6 |
251| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.6>`_ |
252+------------------------------------------+---------+-----------------------------------------------------------------------+
253| :const:`NOT_ACCEPTABLE` | ``406`` | HTTP/1.1, `RFC 2616, Section |
254| | | 10.4.7 |
255| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.7>`_ |
256+------------------------------------------+---------+-----------------------------------------------------------------------+
257| :const:`PROXY_AUTHENTICATION_REQUIRED` | ``407`` | HTTP/1.1, `RFC 2616, Section |
258| | | 10.4.8 |
259| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.8>`_ |
260+------------------------------------------+---------+-----------------------------------------------------------------------+
261| :const:`REQUEST_TIMEOUT` | ``408`` | HTTP/1.1, `RFC 2616, Section |
262| | | 10.4.9 |
263| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.9>`_ |
264+------------------------------------------+---------+-----------------------------------------------------------------------+
265| :const:`CONFLICT` | ``409`` | HTTP/1.1, `RFC 2616, Section |
266| | | 10.4.10 |
267| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.10>`_ |
268+------------------------------------------+---------+-----------------------------------------------------------------------+
269| :const:`GONE` | ``410`` | HTTP/1.1, `RFC 2616, Section |
270| | | 10.4.11 |
271| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.11>`_ |
272+------------------------------------------+---------+-----------------------------------------------------------------------+
273| :const:`LENGTH_REQUIRED` | ``411`` | HTTP/1.1, `RFC 2616, Section |
274| | | 10.4.12 |
275| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.12>`_ |
276+------------------------------------------+---------+-----------------------------------------------------------------------+
277| :const:`PRECONDITION_FAILED` | ``412`` | HTTP/1.1, `RFC 2616, Section |
278| | | 10.4.13 |
279| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.13>`_ |
280+------------------------------------------+---------+-----------------------------------------------------------------------+
281| :const:`REQUEST_ENTITY_TOO_LARGE` | ``413`` | HTTP/1.1, `RFC 2616, Section |
282| | | 10.4.14 |
283| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.14>`_ |
284+------------------------------------------+---------+-----------------------------------------------------------------------+
285| :const:`REQUEST_URI_TOO_LONG` | ``414`` | HTTP/1.1, `RFC 2616, Section |
286| | | 10.4.15 |
287| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.15>`_ |
288+------------------------------------------+---------+-----------------------------------------------------------------------+
289| :const:`UNSUPPORTED_MEDIA_TYPE` | ``415`` | HTTP/1.1, `RFC 2616, Section |
290| | | 10.4.16 |
291| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.16>`_ |
292+------------------------------------------+---------+-----------------------------------------------------------------------+
293| :const:`REQUESTED_RANGE_NOT_SATISFIABLE` | ``416`` | HTTP/1.1, `RFC 2616, Section |
294| | | 10.4.17 |
295| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.17>`_ |
296+------------------------------------------+---------+-----------------------------------------------------------------------+
297| :const:`EXPECTATION_FAILED` | ``417`` | HTTP/1.1, `RFC 2616, Section |
298| | | 10.4.18 |
299| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.18>`_ |
300+------------------------------------------+---------+-----------------------------------------------------------------------+
301| :const:`UNPROCESSABLE_ENTITY` | ``422`` | WEBDAV, `RFC 2518, Section 10.3 |
302| | | <http://www.webdav.org/specs/rfc2518.html#STATUS_422>`_ |
303+------------------------------------------+---------+-----------------------------------------------------------------------+
304| :const:`LOCKED` | ``423`` | WEBDAV `RFC 2518, Section 10.4 |
305| | | <http://www.webdav.org/specs/rfc2518.html#STATUS_423>`_ |
306+------------------------------------------+---------+-----------------------------------------------------------------------+
307| :const:`FAILED_DEPENDENCY` | ``424`` | WEBDAV, `RFC 2518, Section 10.5 |
308| | | <http://www.webdav.org/specs/rfc2518.html#STATUS_424>`_ |
309+------------------------------------------+---------+-----------------------------------------------------------------------+
310| :const:`UPGRADE_REQUIRED` | ``426`` | HTTP Upgrade to TLS, |
311| | | :rfc:`2817`, Section 6 |
312+------------------------------------------+---------+-----------------------------------------------------------------------+
313| :const:`INTERNAL_SERVER_ERROR` | ``500`` | HTTP/1.1, `RFC 2616, Section |
314| | | 10.5.1 |
315| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.1>`_ |
316+------------------------------------------+---------+-----------------------------------------------------------------------+
317| :const:`NOT_IMPLEMENTED` | ``501`` | HTTP/1.1, `RFC 2616, Section |
318| | | 10.5.2 |
319| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.2>`_ |
320+------------------------------------------+---------+-----------------------------------------------------------------------+
321| :const:`BAD_GATEWAY` | ``502`` | HTTP/1.1 `RFC 2616, Section |
322| | | 10.5.3 |
323| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.3>`_ |
324+------------------------------------------+---------+-----------------------------------------------------------------------+
325| :const:`SERVICE_UNAVAILABLE` | ``503`` | HTTP/1.1, `RFC 2616, Section |
326| | | 10.5.4 |
327| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.4>`_ |
328+------------------------------------------+---------+-----------------------------------------------------------------------+
329| :const:`GATEWAY_TIMEOUT` | ``504`` | HTTP/1.1 `RFC 2616, Section |
330| | | 10.5.5 |
331| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.5>`_ |
332+------------------------------------------+---------+-----------------------------------------------------------------------+
333| :const:`HTTP_VERSION_NOT_SUPPORTED` | ``505`` | HTTP/1.1, `RFC 2616, Section |
334| | | 10.5.6 |
335| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.6>`_ |
336+------------------------------------------+---------+-----------------------------------------------------------------------+
337| :const:`INSUFFICIENT_STORAGE` | ``507`` | WEBDAV, `RFC 2518, Section 10.6 |
338| | | <http://www.webdav.org/specs/rfc2518.html#STATUS_507>`_ |
339+------------------------------------------+---------+-----------------------------------------------------------------------+
340| :const:`NOT_EXTENDED` | ``510`` | An HTTP Extension Framework, |
341| | | :rfc:`2774`, Section 7 |
342+------------------------------------------+---------+-----------------------------------------------------------------------+
343
344
345.. data:: responses
346
347 This dictionary maps the HTTP 1.1 status codes to the W3C names.
348
Georg Brandl24420152008-05-26 16:32:26 +0000349 Example: ``http.client.responses[http.client.NOT_FOUND]`` is ``'Not Found'``.
Georg Brandl116aa622007-08-15 14:28:22 +0000350
Georg Brandl116aa622007-08-15 14:28:22 +0000351
352.. _httpconnection-objects:
353
354HTTPConnection Objects
355----------------------
356
357:class:`HTTPConnection` instances have the following methods:
358
359
Georg Brandl036490d2009-05-17 13:00:36 +0000360.. method:: HTTPConnection.request(method, url, body=None, headers={})
Georg Brandl116aa622007-08-15 14:28:22 +0000361
Jeremy Hylton236654b2009-03-27 20:24:34 +0000362 This will send a request to the server using the HTTP request
363 method *method* and the selector *url*. If the *body* argument is
364 present, it should be string or bytes object of data to send after
365 the headers are finished. Strings are encoded as ISO-8859-1, the
366 default charset for HTTP. To use other encodings, pass a bytes
367 object. The Content-Length header is set to the length of the
368 string.
Georg Brandl116aa622007-08-15 14:28:22 +0000369
Jeremy Hylton236654b2009-03-27 20:24:34 +0000370 The *body* may also be an open file object, in which case the
371 contents of the file is sent; this file object should support
372 ``fileno()`` and ``read()`` methods. The header Content-Length is
373 automatically set to the length of the file as reported by
374 stat.
375
376 The *headers* argument should be a mapping of extra HTTP
377 headers to send with the request.
Georg Brandl116aa622007-08-15 14:28:22 +0000378
379.. method:: HTTPConnection.getresponse()
380
381 Should be called after a request is sent to get the response from the server.
382 Returns an :class:`HTTPResponse` instance.
383
384 .. note::
385
386 Note that you must have read the whole response before you can send a new
387 request to the server.
388
389
390.. method:: HTTPConnection.set_debuglevel(level)
391
392 Set the debugging level (the amount of debugging output printed). The default
393 debug level is ``0``, meaning no debugging output is printed.
394
Mark Dickinson574b1d62009-10-01 20:20:09 +0000395 .. versionadded:: 3.1
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000396
Senthil Kumaran7e5229c2009-12-20 07:31:21 +0000397.. method:: HTTPConnection.set_tunnel(host, port=None, headers=None)
Senthil Kumaran97f0c6b2009-07-25 04:24:38 +0000398
399 Set the host and the port for HTTP Connect Tunnelling. Normally used when it
400 is required to a HTTPS Connection through a proxy server.
401
Senthil Kumaran7e5229c2009-12-20 07:31:21 +0000402 The headers argument should be a mapping of extra HTTP headers to to sent
403 with the CONNECT request.
404
Senthil Kumaran2e910fd2009-07-25 04:27:38 +0000405 .. versionadded:: 3.2
Georg Brandl116aa622007-08-15 14:28:22 +0000406
407.. method:: HTTPConnection.connect()
408
409 Connect to the server specified when the object was created.
410
411
412.. method:: HTTPConnection.close()
413
414 Close the connection to the server.
415
416As an alternative to using the :meth:`request` method described above, you can
417also send your request step by step, by using the four functions below.
418
419
Georg Brandl036490d2009-05-17 13:00:36 +0000420.. method:: HTTPConnection.putrequest(request, selector, skip_host=False, skip_accept_encoding=False)
Georg Brandl116aa622007-08-15 14:28:22 +0000421
422 This should be the first call after the connection to the server has been made.
423 It sends a line to the server consisting of the *request* string, the *selector*
424 string, and the HTTP version (``HTTP/1.1``). To disable automatic sending of
425 ``Host:`` or ``Accept-Encoding:`` headers (for example to accept additional
426 content encodings), specify *skip_host* or *skip_accept_encoding* with non-False
427 values.
428
Georg Brandl116aa622007-08-15 14:28:22 +0000429
430.. method:: HTTPConnection.putheader(header, argument[, ...])
431
432 Send an :rfc:`822`\ -style header to the server. It sends a line to the server
433 consisting of the header, a colon and a space, and the first argument. If more
434 arguments are given, continuation lines are sent, each consisting of a tab and
435 an argument.
436
437
438.. method:: HTTPConnection.endheaders()
439
440 Send a blank line to the server, signalling the end of the headers.
441
442
443.. method:: HTTPConnection.send(data)
444
445 Send data to the server. This should be used directly only after the
446 :meth:`endheaders` method has been called and before :meth:`getresponse` is
447 called.
448
449
450.. _httpresponse-objects:
451
452HTTPResponse Objects
453--------------------
454
Jeremy Hylton1052f892009-03-31 14:40:19 +0000455An :class:`HTTPResponse` instance wraps the HTTP response from the
456server. It provides access to the request headers and the entity
457body. The response is an iterable object and can be used in a with
458statement.
Georg Brandl116aa622007-08-15 14:28:22 +0000459
460
461.. method:: HTTPResponse.read([amt])
462
463 Reads and returns the response body, or up to the next *amt* bytes.
464
465
Georg Brandl036490d2009-05-17 13:00:36 +0000466.. method:: HTTPResponse.getheader(name, default=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000467
468 Get the contents of the header *name*, or *default* if there is no matching
469 header.
470
471
472.. method:: HTTPResponse.getheaders()
473
474 Return a list of (header, value) tuples.
475
Georg Brandl116aa622007-08-15 14:28:22 +0000476
477.. attribute:: HTTPResponse.msg
478
Jeremy Hylton1052f892009-03-31 14:40:19 +0000479 A :class:`http.client.HTTPMessage` instance containing the response
480 headers. :class:`http.client.HTTPMessage` is a subclass of
481 :class:`email.message.Message`.
Georg Brandl116aa622007-08-15 14:28:22 +0000482
483
484.. attribute:: HTTPResponse.version
485
486 HTTP protocol version used by server. 10 for HTTP/1.0, 11 for HTTP/1.1.
487
488
489.. attribute:: HTTPResponse.status
490
491 Status code returned by server.
492
493
494.. attribute:: HTTPResponse.reason
495
496 Reason phrase returned by server.
497
498
Jeremy Hylton1052f892009-03-31 14:40:19 +0000499.. attribute:: HTTPResponse.debuglevel
500
Georg Brandlef871f62010-03-12 10:06:40 +0000501 A debugging hook. If :attr:`debuglevel` is greater than zero, messages
Jeremy Hylton1052f892009-03-31 14:40:19 +0000502 will be printed to stdout as the response is read and parsed.
503
504
Georg Brandl116aa622007-08-15 14:28:22 +0000505Examples
506--------
507
508Here is an example session that uses the ``GET`` method::
509
Georg Brandl24420152008-05-26 16:32:26 +0000510 >>> import http.client
511 >>> conn = http.client.HTTPConnection("www.python.org")
Georg Brandl116aa622007-08-15 14:28:22 +0000512 >>> conn.request("GET", "/index.html")
513 >>> r1 = conn.getresponse()
Georg Brandl6911e3c2007-09-04 07:15:32 +0000514 >>> print(r1.status, r1.reason)
Georg Brandl116aa622007-08-15 14:28:22 +0000515 200 OK
516 >>> data1 = r1.read()
517 >>> conn.request("GET", "/parrot.spam")
518 >>> r2 = conn.getresponse()
Georg Brandl6911e3c2007-09-04 07:15:32 +0000519 >>> print(r2.status, r2.reason)
Georg Brandl116aa622007-08-15 14:28:22 +0000520 404 Not Found
521 >>> data2 = r2.read()
522 >>> conn.close()
523
Fred Drake1587e3d2010-05-12 01:36:11 +0000524Here is an example session that uses the ``HEAD`` method. Note that the
525``HEAD`` method never returns any data. ::
Senthil Kumaran71fb6c82010-04-28 17:39:48 +0000526
527 >>> import http.client
528 >>> conn = http.client.HTTPConnection("www.python.org")
529 >>> conn.request("HEAD","/index.html")
530 >>> res = conn.getresponse()
531 >>> print(res.status, res.reason)
532 200 OK
533 >>> data = res.read()
534 >>> print(len(data))
535 0
536 >>> data == b''
537 True
538
Georg Brandl116aa622007-08-15 14:28:22 +0000539Here is an example session that shows how to ``POST`` requests::
540
Senthil Kumaranaca8fd72008-06-23 04:41:59 +0000541 >>> import http.client, urllib.parse
542 >>> params = urllib.parse.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})
Georg Brandl116aa622007-08-15 14:28:22 +0000543 >>> headers = {"Content-type": "application/x-www-form-urlencoded",
544 ... "Accept": "text/plain"}
Georg Brandl24420152008-05-26 16:32:26 +0000545 >>> conn = http.client.HTTPConnection("musi-cal.mojam.com:80")
Georg Brandl116aa622007-08-15 14:28:22 +0000546 >>> conn.request("POST", "/cgi-bin/query", params, headers)
547 >>> response = conn.getresponse()
Georg Brandl6911e3c2007-09-04 07:15:32 +0000548 >>> print(response.status, response.reason)
Georg Brandl116aa622007-08-15 14:28:22 +0000549 200 OK
550 >>> data = response.read()
551 >>> conn.close()
552
Jeremy Hylton1052f892009-03-31 14:40:19 +0000553
554.. _httpmessage-objects:
555
556HTTPMessage Objects
557-------------------
558
Benjamin Peterson605b9d92009-04-02 00:24:00 +0000559An :class:`http.client.HTTPMessage` instance holds the headers from an HTTP
560response. It is implemented using the :class:`email.message.Message` class.
Jeremy Hylton1052f892009-03-31 14:40:19 +0000561
Benjamin Peterson605b9d92009-04-02 00:24:00 +0000562.. XXX Define the methods that clients can depend upon between versions.