blob: 02ab9dc552f7bd3d96ca19117e71a8ca0448e53a [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
Georg Brandl036490d2009-05-17 13:00:36 +000026.. class:: HTTPConnection(host, port=None, strict=None[, timeout])
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).
Georg Brandl116aa622007-08-15 14:28:22 +000038
39 For example, the following calls all create instances that connect to the server
40 at the same host and port::
41
Georg Brandl24420152008-05-26 16:32:26 +000042 >>> h1 = http.client.HTTPConnection('www.cwi.nl')
43 >>> h2 = http.client.HTTPConnection('www.cwi.nl:80')
44 >>> h3 = http.client.HTTPConnection('www.cwi.nl', 80)
45 >>> h3 = http.client.HTTPConnection('www.cwi.nl', 80, timeout=10)
Georg Brandl116aa622007-08-15 14:28:22 +000046
Georg Brandl116aa622007-08-15 14:28:22 +000047
Georg Brandl036490d2009-05-17 13:00:36 +000048.. class:: HTTPSConnection(host, port=None, key_file=None, cert_file=None, strict=None[, timeout])
Georg Brandl116aa622007-08-15 14:28:22 +000049
50 A subclass of :class:`HTTPConnection` that uses SSL for communication with
51 secure servers. Default port is ``443``. *key_file* is the name of a PEM
52 formatted file that contains your private key. *cert_file* is a PEM formatted
53 certificate chain file.
54
Georg Brandle720c0a2009-04-27 16:20:50 +000055 .. note::
Georg Brandl116aa622007-08-15 14:28:22 +000056
Georg Brandle720c0a2009-04-27 16:20:50 +000057 This does not do any certificate verification.
Georg Brandl116aa622007-08-15 14:28:22 +000058
Georg Brandl116aa622007-08-15 14:28:22 +000059
Georg Brandl036490d2009-05-17 13:00:36 +000060.. class:: HTTPResponse(sock, debuglevel=0, strict=0, method=None, url=None)
Georg Brandl116aa622007-08-15 14:28:22 +000061
Jeremy Hylton1052f892009-03-31 14:40:19 +000062 Class whose instances are returned upon successful connection. Not
63 instantiated directly by user.
Georg Brandl116aa622007-08-15 14:28:22 +000064
Georg Brandl116aa622007-08-15 14:28:22 +000065
66The following exceptions are raised as appropriate:
67
68
69.. exception:: HTTPException
70
71 The base class of the other exceptions in this module. It is a subclass of
72 :exc:`Exception`.
73
Georg Brandl116aa622007-08-15 14:28:22 +000074
75.. exception:: NotConnected
76
77 A subclass of :exc:`HTTPException`.
78
Georg Brandl116aa622007-08-15 14:28:22 +000079
80.. exception:: InvalidURL
81
82 A subclass of :exc:`HTTPException`, raised if a port is given and is either
83 non-numeric or empty.
84
Georg Brandl116aa622007-08-15 14:28:22 +000085
86.. exception:: UnknownProtocol
87
88 A subclass of :exc:`HTTPException`.
89
Georg Brandl116aa622007-08-15 14:28:22 +000090
91.. exception:: UnknownTransferEncoding
92
93 A subclass of :exc:`HTTPException`.
94
Georg Brandl116aa622007-08-15 14:28:22 +000095
96.. exception:: UnimplementedFileMode
97
98 A subclass of :exc:`HTTPException`.
99
Georg Brandl116aa622007-08-15 14:28:22 +0000100
101.. exception:: IncompleteRead
102
103 A subclass of :exc:`HTTPException`.
104
Georg Brandl116aa622007-08-15 14:28:22 +0000105
106.. exception:: ImproperConnectionState
107
108 A subclass of :exc:`HTTPException`.
109
Georg Brandl116aa622007-08-15 14:28:22 +0000110
111.. exception:: CannotSendRequest
112
113 A subclass of :exc:`ImproperConnectionState`.
114
Georg Brandl116aa622007-08-15 14:28:22 +0000115
116.. exception:: CannotSendHeader
117
118 A subclass of :exc:`ImproperConnectionState`.
119
Georg Brandl116aa622007-08-15 14:28:22 +0000120
121.. exception:: ResponseNotReady
122
123 A subclass of :exc:`ImproperConnectionState`.
124
Georg Brandl116aa622007-08-15 14:28:22 +0000125
126.. exception:: BadStatusLine
127
128 A subclass of :exc:`HTTPException`. Raised if a server responds with a HTTP
129 status code that we don't understand.
130
Georg Brandl116aa622007-08-15 14:28:22 +0000131The constants defined in this module are:
132
133
134.. data:: HTTP_PORT
135
136 The default port for the HTTP protocol (always ``80``).
137
138
139.. data:: HTTPS_PORT
140
141 The default port for the HTTPS protocol (always ``443``).
142
143and also the following constants for integer status codes:
144
145+------------------------------------------+---------+-----------------------------------------------------------------------+
146| Constant | Value | Definition |
147+==========================================+=========+=======================================================================+
148| :const:`CONTINUE` | ``100`` | HTTP/1.1, `RFC 2616, Section |
149| | | 10.1.1 |
150| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.1.1>`_ |
151+------------------------------------------+---------+-----------------------------------------------------------------------+
152| :const:`SWITCHING_PROTOCOLS` | ``101`` | HTTP/1.1, `RFC 2616, Section |
153| | | 10.1.2 |
154| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.1.2>`_ |
155+------------------------------------------+---------+-----------------------------------------------------------------------+
156| :const:`PROCESSING` | ``102`` | WEBDAV, `RFC 2518, Section 10.1 |
157| | | <http://www.webdav.org/specs/rfc2518.html#STATUS_102>`_ |
158+------------------------------------------+---------+-----------------------------------------------------------------------+
159| :const:`OK` | ``200`` | HTTP/1.1, `RFC 2616, Section |
160| | | 10.2.1 |
161| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.1>`_ |
162+------------------------------------------+---------+-----------------------------------------------------------------------+
163| :const:`CREATED` | ``201`` | HTTP/1.1, `RFC 2616, Section |
164| | | 10.2.2 |
165| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.2>`_ |
166+------------------------------------------+---------+-----------------------------------------------------------------------+
167| :const:`ACCEPTED` | ``202`` | HTTP/1.1, `RFC 2616, Section |
168| | | 10.2.3 |
169| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.3>`_ |
170+------------------------------------------+---------+-----------------------------------------------------------------------+
171| :const:`NON_AUTHORITATIVE_INFORMATION` | ``203`` | HTTP/1.1, `RFC 2616, Section |
172| | | 10.2.4 |
173| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.4>`_ |
174+------------------------------------------+---------+-----------------------------------------------------------------------+
175| :const:`NO_CONTENT` | ``204`` | HTTP/1.1, `RFC 2616, Section |
176| | | 10.2.5 |
177| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.5>`_ |
178+------------------------------------------+---------+-----------------------------------------------------------------------+
179| :const:`RESET_CONTENT` | ``205`` | HTTP/1.1, `RFC 2616, Section |
180| | | 10.2.6 |
181| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.6>`_ |
182+------------------------------------------+---------+-----------------------------------------------------------------------+
183| :const:`PARTIAL_CONTENT` | ``206`` | HTTP/1.1, `RFC 2616, Section |
184| | | 10.2.7 |
185| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.7>`_ |
186+------------------------------------------+---------+-----------------------------------------------------------------------+
187| :const:`MULTI_STATUS` | ``207`` | WEBDAV `RFC 2518, Section 10.2 |
188| | | <http://www.webdav.org/specs/rfc2518.html#STATUS_207>`_ |
189+------------------------------------------+---------+-----------------------------------------------------------------------+
190| :const:`IM_USED` | ``226`` | Delta encoding in HTTP, |
191| | | :rfc:`3229`, Section 10.4.1 |
192+------------------------------------------+---------+-----------------------------------------------------------------------+
193| :const:`MULTIPLE_CHOICES` | ``300`` | HTTP/1.1, `RFC 2616, Section |
194| | | 10.3.1 |
195| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.1>`_ |
196+------------------------------------------+---------+-----------------------------------------------------------------------+
197| :const:`MOVED_PERMANENTLY` | ``301`` | HTTP/1.1, `RFC 2616, Section |
198| | | 10.3.2 |
199| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.2>`_ |
200+------------------------------------------+---------+-----------------------------------------------------------------------+
201| :const:`FOUND` | ``302`` | HTTP/1.1, `RFC 2616, Section |
202| | | 10.3.3 |
203| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.3>`_ |
204+------------------------------------------+---------+-----------------------------------------------------------------------+
205| :const:`SEE_OTHER` | ``303`` | HTTP/1.1, `RFC 2616, Section |
206| | | 10.3.4 |
207| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.4>`_ |
208+------------------------------------------+---------+-----------------------------------------------------------------------+
209| :const:`NOT_MODIFIED` | ``304`` | HTTP/1.1, `RFC 2616, Section |
210| | | 10.3.5 |
211| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.5>`_ |
212+------------------------------------------+---------+-----------------------------------------------------------------------+
213| :const:`USE_PROXY` | ``305`` | HTTP/1.1, `RFC 2616, Section |
214| | | 10.3.6 |
215| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.6>`_ |
216+------------------------------------------+---------+-----------------------------------------------------------------------+
217| :const:`TEMPORARY_REDIRECT` | ``307`` | HTTP/1.1, `RFC 2616, Section |
218| | | 10.3.8 |
219| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.8>`_ |
220+------------------------------------------+---------+-----------------------------------------------------------------------+
221| :const:`BAD_REQUEST` | ``400`` | HTTP/1.1, `RFC 2616, Section |
222| | | 10.4.1 |
223| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.1>`_ |
224+------------------------------------------+---------+-----------------------------------------------------------------------+
225| :const:`UNAUTHORIZED` | ``401`` | HTTP/1.1, `RFC 2616, Section |
226| | | 10.4.2 |
227| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.2>`_ |
228+------------------------------------------+---------+-----------------------------------------------------------------------+
229| :const:`PAYMENT_REQUIRED` | ``402`` | HTTP/1.1, `RFC 2616, Section |
230| | | 10.4.3 |
231| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.3>`_ |
232+------------------------------------------+---------+-----------------------------------------------------------------------+
233| :const:`FORBIDDEN` | ``403`` | HTTP/1.1, `RFC 2616, Section |
234| | | 10.4.4 |
235| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.4>`_ |
236+------------------------------------------+---------+-----------------------------------------------------------------------+
237| :const:`NOT_FOUND` | ``404`` | HTTP/1.1, `RFC 2616, Section |
238| | | 10.4.5 |
239| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.5>`_ |
240+------------------------------------------+---------+-----------------------------------------------------------------------+
241| :const:`METHOD_NOT_ALLOWED` | ``405`` | HTTP/1.1, `RFC 2616, Section |
242| | | 10.4.6 |
243| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.6>`_ |
244+------------------------------------------+---------+-----------------------------------------------------------------------+
245| :const:`NOT_ACCEPTABLE` | ``406`` | HTTP/1.1, `RFC 2616, Section |
246| | | 10.4.7 |
247| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.7>`_ |
248+------------------------------------------+---------+-----------------------------------------------------------------------+
249| :const:`PROXY_AUTHENTICATION_REQUIRED` | ``407`` | HTTP/1.1, `RFC 2616, Section |
250| | | 10.4.8 |
251| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.8>`_ |
252+------------------------------------------+---------+-----------------------------------------------------------------------+
253| :const:`REQUEST_TIMEOUT` | ``408`` | HTTP/1.1, `RFC 2616, Section |
254| | | 10.4.9 |
255| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.9>`_ |
256+------------------------------------------+---------+-----------------------------------------------------------------------+
257| :const:`CONFLICT` | ``409`` | HTTP/1.1, `RFC 2616, Section |
258| | | 10.4.10 |
259| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.10>`_ |
260+------------------------------------------+---------+-----------------------------------------------------------------------+
261| :const:`GONE` | ``410`` | HTTP/1.1, `RFC 2616, Section |
262| | | 10.4.11 |
263| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.11>`_ |
264+------------------------------------------+---------+-----------------------------------------------------------------------+
265| :const:`LENGTH_REQUIRED` | ``411`` | HTTP/1.1, `RFC 2616, Section |
266| | | 10.4.12 |
267| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.12>`_ |
268+------------------------------------------+---------+-----------------------------------------------------------------------+
269| :const:`PRECONDITION_FAILED` | ``412`` | HTTP/1.1, `RFC 2616, Section |
270| | | 10.4.13 |
271| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.13>`_ |
272+------------------------------------------+---------+-----------------------------------------------------------------------+
273| :const:`REQUEST_ENTITY_TOO_LARGE` | ``413`` | HTTP/1.1, `RFC 2616, Section |
274| | | 10.4.14 |
275| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.14>`_ |
276+------------------------------------------+---------+-----------------------------------------------------------------------+
277| :const:`REQUEST_URI_TOO_LONG` | ``414`` | HTTP/1.1, `RFC 2616, Section |
278| | | 10.4.15 |
279| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.15>`_ |
280+------------------------------------------+---------+-----------------------------------------------------------------------+
281| :const:`UNSUPPORTED_MEDIA_TYPE` | ``415`` | HTTP/1.1, `RFC 2616, Section |
282| | | 10.4.16 |
283| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.16>`_ |
284+------------------------------------------+---------+-----------------------------------------------------------------------+
285| :const:`REQUESTED_RANGE_NOT_SATISFIABLE` | ``416`` | HTTP/1.1, `RFC 2616, Section |
286| | | 10.4.17 |
287| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.17>`_ |
288+------------------------------------------+---------+-----------------------------------------------------------------------+
289| :const:`EXPECTATION_FAILED` | ``417`` | HTTP/1.1, `RFC 2616, Section |
290| | | 10.4.18 |
291| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.18>`_ |
292+------------------------------------------+---------+-----------------------------------------------------------------------+
293| :const:`UNPROCESSABLE_ENTITY` | ``422`` | WEBDAV, `RFC 2518, Section 10.3 |
294| | | <http://www.webdav.org/specs/rfc2518.html#STATUS_422>`_ |
295+------------------------------------------+---------+-----------------------------------------------------------------------+
296| :const:`LOCKED` | ``423`` | WEBDAV `RFC 2518, Section 10.4 |
297| | | <http://www.webdav.org/specs/rfc2518.html#STATUS_423>`_ |
298+------------------------------------------+---------+-----------------------------------------------------------------------+
299| :const:`FAILED_DEPENDENCY` | ``424`` | WEBDAV, `RFC 2518, Section 10.5 |
300| | | <http://www.webdav.org/specs/rfc2518.html#STATUS_424>`_ |
301+------------------------------------------+---------+-----------------------------------------------------------------------+
302| :const:`UPGRADE_REQUIRED` | ``426`` | HTTP Upgrade to TLS, |
303| | | :rfc:`2817`, Section 6 |
304+------------------------------------------+---------+-----------------------------------------------------------------------+
305| :const:`INTERNAL_SERVER_ERROR` | ``500`` | HTTP/1.1, `RFC 2616, Section |
306| | | 10.5.1 |
307| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.1>`_ |
308+------------------------------------------+---------+-----------------------------------------------------------------------+
309| :const:`NOT_IMPLEMENTED` | ``501`` | HTTP/1.1, `RFC 2616, Section |
310| | | 10.5.2 |
311| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.2>`_ |
312+------------------------------------------+---------+-----------------------------------------------------------------------+
313| :const:`BAD_GATEWAY` | ``502`` | HTTP/1.1 `RFC 2616, Section |
314| | | 10.5.3 |
315| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.3>`_ |
316+------------------------------------------+---------+-----------------------------------------------------------------------+
317| :const:`SERVICE_UNAVAILABLE` | ``503`` | HTTP/1.1, `RFC 2616, Section |
318| | | 10.5.4 |
319| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.4>`_ |
320+------------------------------------------+---------+-----------------------------------------------------------------------+
321| :const:`GATEWAY_TIMEOUT` | ``504`` | HTTP/1.1 `RFC 2616, Section |
322| | | 10.5.5 |
323| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.5>`_ |
324+------------------------------------------+---------+-----------------------------------------------------------------------+
325| :const:`HTTP_VERSION_NOT_SUPPORTED` | ``505`` | HTTP/1.1, `RFC 2616, Section |
326| | | 10.5.6 |
327| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.6>`_ |
328+------------------------------------------+---------+-----------------------------------------------------------------------+
329| :const:`INSUFFICIENT_STORAGE` | ``507`` | WEBDAV, `RFC 2518, Section 10.6 |
330| | | <http://www.webdav.org/specs/rfc2518.html#STATUS_507>`_ |
331+------------------------------------------+---------+-----------------------------------------------------------------------+
332| :const:`NOT_EXTENDED` | ``510`` | An HTTP Extension Framework, |
333| | | :rfc:`2774`, Section 7 |
334+------------------------------------------+---------+-----------------------------------------------------------------------+
335
336
337.. data:: responses
338
339 This dictionary maps the HTTP 1.1 status codes to the W3C names.
340
Georg Brandl24420152008-05-26 16:32:26 +0000341 Example: ``http.client.responses[http.client.NOT_FOUND]`` is ``'Not Found'``.
Georg Brandl116aa622007-08-15 14:28:22 +0000342
Georg Brandl116aa622007-08-15 14:28:22 +0000343
344.. _httpconnection-objects:
345
346HTTPConnection Objects
347----------------------
348
349:class:`HTTPConnection` instances have the following methods:
350
351
Georg Brandl036490d2009-05-17 13:00:36 +0000352.. method:: HTTPConnection.request(method, url, body=None, headers={})
Georg Brandl116aa622007-08-15 14:28:22 +0000353
Jeremy Hylton236654b2009-03-27 20:24:34 +0000354 This will send a request to the server using the HTTP request
355 method *method* and the selector *url*. If the *body* argument is
356 present, it should be string or bytes object of data to send after
357 the headers are finished. Strings are encoded as ISO-8859-1, the
358 default charset for HTTP. To use other encodings, pass a bytes
359 object. The Content-Length header is set to the length of the
360 string.
Georg Brandl116aa622007-08-15 14:28:22 +0000361
Jeremy Hylton236654b2009-03-27 20:24:34 +0000362 The *body* may also be an open file object, in which case the
363 contents of the file is sent; this file object should support
364 ``fileno()`` and ``read()`` methods. The header Content-Length is
365 automatically set to the length of the file as reported by
366 stat.
367
368 The *headers* argument should be a mapping of extra HTTP
369 headers to send with the request.
Georg Brandl116aa622007-08-15 14:28:22 +0000370
371.. method:: HTTPConnection.getresponse()
372
373 Should be called after a request is sent to get the response from the server.
374 Returns an :class:`HTTPResponse` instance.
375
376 .. note::
377
378 Note that you must have read the whole response before you can send a new
379 request to the server.
380
381
382.. method:: HTTPConnection.set_debuglevel(level)
383
384 Set the debugging level (the amount of debugging output printed). The default
385 debug level is ``0``, meaning no debugging output is printed.
386
Benjamin Petersonfa0d7032009-06-01 22:42:33 +0000387 .. versionadded:: 2.7
388
Senthil Kumaran97f0c6b2009-07-25 04:24:38 +0000389.. method:: HTTPConnection.set_tunnel(host, port=None)
390
391 Set the host and the port for HTTP Connect Tunnelling. Normally used when it
392 is required to a HTTPS Connection through a proxy server.
393
Senthil Kumaran2e910fd2009-07-25 04:27:38 +0000394 .. versionadded:: 3.2
Georg Brandl116aa622007-08-15 14:28:22 +0000395
396.. method:: HTTPConnection.connect()
397
398 Connect to the server specified when the object was created.
399
400
401.. method:: HTTPConnection.close()
402
403 Close the connection to the server.
404
405As an alternative to using the :meth:`request` method described above, you can
406also send your request step by step, by using the four functions below.
407
408
Georg Brandl036490d2009-05-17 13:00:36 +0000409.. method:: HTTPConnection.putrequest(request, selector, skip_host=False, skip_accept_encoding=False)
Georg Brandl116aa622007-08-15 14:28:22 +0000410
411 This should be the first call after the connection to the server has been made.
412 It sends a line to the server consisting of the *request* string, the *selector*
413 string, and the HTTP version (``HTTP/1.1``). To disable automatic sending of
414 ``Host:`` or ``Accept-Encoding:`` headers (for example to accept additional
415 content encodings), specify *skip_host* or *skip_accept_encoding* with non-False
416 values.
417
Georg Brandl116aa622007-08-15 14:28:22 +0000418
419.. method:: HTTPConnection.putheader(header, argument[, ...])
420
421 Send an :rfc:`822`\ -style header to the server. It sends a line to the server
422 consisting of the header, a colon and a space, and the first argument. If more
423 arguments are given, continuation lines are sent, each consisting of a tab and
424 an argument.
425
426
427.. method:: HTTPConnection.endheaders()
428
429 Send a blank line to the server, signalling the end of the headers.
430
431
432.. method:: HTTPConnection.send(data)
433
434 Send data to the server. This should be used directly only after the
435 :meth:`endheaders` method has been called and before :meth:`getresponse` is
436 called.
437
438
439.. _httpresponse-objects:
440
441HTTPResponse Objects
442--------------------
443
Jeremy Hylton1052f892009-03-31 14:40:19 +0000444An :class:`HTTPResponse` instance wraps the HTTP response from the
445server. It provides access to the request headers and the entity
446body. The response is an iterable object and can be used in a with
447statement.
Georg Brandl116aa622007-08-15 14:28:22 +0000448
449
450.. method:: HTTPResponse.read([amt])
451
452 Reads and returns the response body, or up to the next *amt* bytes.
453
454
Georg Brandl036490d2009-05-17 13:00:36 +0000455.. method:: HTTPResponse.getheader(name, default=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000456
457 Get the contents of the header *name*, or *default* if there is no matching
458 header.
459
460
461.. method:: HTTPResponse.getheaders()
462
463 Return a list of (header, value) tuples.
464
Georg Brandl116aa622007-08-15 14:28:22 +0000465
466.. attribute:: HTTPResponse.msg
467
Jeremy Hylton1052f892009-03-31 14:40:19 +0000468 A :class:`http.client.HTTPMessage` instance containing the response
469 headers. :class:`http.client.HTTPMessage` is a subclass of
470 :class:`email.message.Message`.
Georg Brandl116aa622007-08-15 14:28:22 +0000471
472
473.. attribute:: HTTPResponse.version
474
475 HTTP protocol version used by server. 10 for HTTP/1.0, 11 for HTTP/1.1.
476
477
478.. attribute:: HTTPResponse.status
479
480 Status code returned by server.
481
482
483.. attribute:: HTTPResponse.reason
484
485 Reason phrase returned by server.
486
487
Jeremy Hylton1052f892009-03-31 14:40:19 +0000488.. attribute:: HTTPResponse.debuglevel
489
490 A debugging hook. If `debuglevel` is greater than zero, messages
491 will be printed to stdout as the response is read and parsed.
492
493
Georg Brandl116aa622007-08-15 14:28:22 +0000494Examples
495--------
496
497Here is an example session that uses the ``GET`` method::
498
Georg Brandl24420152008-05-26 16:32:26 +0000499 >>> import http.client
500 >>> conn = http.client.HTTPConnection("www.python.org")
Georg Brandl116aa622007-08-15 14:28:22 +0000501 >>> conn.request("GET", "/index.html")
502 >>> r1 = conn.getresponse()
Georg Brandl6911e3c2007-09-04 07:15:32 +0000503 >>> print(r1.status, r1.reason)
Georg Brandl116aa622007-08-15 14:28:22 +0000504 200 OK
505 >>> data1 = r1.read()
506 >>> conn.request("GET", "/parrot.spam")
507 >>> r2 = conn.getresponse()
Georg Brandl6911e3c2007-09-04 07:15:32 +0000508 >>> print(r2.status, r2.reason)
Georg Brandl116aa622007-08-15 14:28:22 +0000509 404 Not Found
510 >>> data2 = r2.read()
511 >>> conn.close()
512
513Here is an example session that shows how to ``POST`` requests::
514
Senthil Kumaranaca8fd72008-06-23 04:41:59 +0000515 >>> import http.client, urllib.parse
516 >>> params = urllib.parse.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})
Georg Brandl116aa622007-08-15 14:28:22 +0000517 >>> headers = {"Content-type": "application/x-www-form-urlencoded",
518 ... "Accept": "text/plain"}
Georg Brandl24420152008-05-26 16:32:26 +0000519 >>> conn = http.client.HTTPConnection("musi-cal.mojam.com:80")
Georg Brandl116aa622007-08-15 14:28:22 +0000520 >>> conn.request("POST", "/cgi-bin/query", params, headers)
521 >>> response = conn.getresponse()
Georg Brandl6911e3c2007-09-04 07:15:32 +0000522 >>> print(response.status, response.reason)
Georg Brandl116aa622007-08-15 14:28:22 +0000523 200 OK
524 >>> data = response.read()
525 >>> conn.close()
526
Jeremy Hylton1052f892009-03-31 14:40:19 +0000527
528.. _httpmessage-objects:
529
530HTTPMessage Objects
531-------------------
532
Benjamin Peterson605b9d92009-04-02 00:24:00 +0000533An :class:`http.client.HTTPMessage` instance holds the headers from an HTTP
534response. It is implemented using the :class:`email.message.Message` class.
Jeremy Hylton1052f892009-03-31 14:40:19 +0000535
Benjamin Peterson605b9d92009-04-02 00:24:00 +0000536.. XXX Define the methods that clients can depend upon between versions.