blob: a4bff4f230a56c756f1f42d261367f01b139fe71 [file] [log] [blame]
Georg Brandl8ec7f652007-08-15 14:28:01 +00001:mod:`httplib` --- HTTP protocol client
2=======================================
3
4.. module:: httplib
5 :synopsis: HTTP and HTTPS protocol client (requires sockets).
6
Georg Brandl8de91192008-05-26 15:01:48 +00007.. note::
8 The :mod:`httplib` module has been renamed to :mod:`http.client` in Python
Ezio Melotti510ff542012-05-03 19:21:40 +03009 3. The :term:`2to3` tool will automatically adapt imports when converting
10 your sources to Python 3.
Georg Brandl8de91192008-05-26 15:01:48 +000011
Georg Brandl8ec7f652007-08-15 14:28:01 +000012
13.. index::
14 pair: HTTP; protocol
15 single: HTTP; httplib (standard module)
16
17.. index:: module: urllib
18
Éric Araujo29a0b572011-08-19 02:14:03 +020019**Source code:** :source:`Lib/httplib.py`
20
21--------------
22
Georg Brandl8ec7f652007-08-15 14:28:01 +000023This module defines classes which implement the client side of the HTTP and
24HTTPS protocols. It is normally not used directly --- the module :mod:`urllib`
25uses it to handle URLs that use HTTP and HTTPS.
26
27.. note::
28
29 HTTPS support is only available if the :mod:`socket` module was compiled with
30 SSL support.
31
32.. note::
33
34 The public interface for this module changed substantially in Python 2.0. The
35 :class:`HTTP` class is retained only for backward compatibility with 1.5.2. It
36 should not be used in new code. Refer to the online docstrings for usage.
37
38The module provides the following classes:
39
40
Gregory P. Smith9d325212010-01-03 02:06:07 +000041.. class:: HTTPConnection(host[, port[, strict[, timeout[, source_address]]]])
Georg Brandl8ec7f652007-08-15 14:28:01 +000042
43 An :class:`HTTPConnection` instance represents one transaction with an HTTP
Georg Brandlab756f62008-05-11 11:09:35 +000044 server. It should be instantiated passing it a host and optional port
45 number. If no port number is passed, the port is extracted from the host
46 string if it has the form ``host:port``, else the default HTTP port (80) is
Brett Cannon031f3fb2008-11-15 22:33:25 +000047 used. When True, the optional parameter *strict* (which defaults to a false
48 value) causes ``BadStatusLine`` to
Georg Brandlab756f62008-05-11 11:09:35 +000049 be raised if the status line can't be parsed as a valid HTTP/1.0 or 1.1
50 status line. If the optional *timeout* parameter is given, blocking
51 operations (like connection attempts) will timeout after that many seconds
Facundo Batista4f1b1ed2008-05-29 16:39:26 +000052 (if it is not given, the global default timeout setting is used).
Gregory P. Smith9d325212010-01-03 02:06:07 +000053 The optional *source_address* parameter may be a tuple of a (host, port)
54 to use as the source address the HTTP connection is made from.
Georg Brandl8ec7f652007-08-15 14:28:01 +000055
56 For example, the following calls all create instances that connect to the server
57 at the same host and port::
58
59 >>> h1 = httplib.HTTPConnection('www.cwi.nl')
60 >>> h2 = httplib.HTTPConnection('www.cwi.nl:80')
61 >>> h3 = httplib.HTTPConnection('www.cwi.nl', 80)
62 >>> h3 = httplib.HTTPConnection('www.cwi.nl', 80, timeout=10)
63
64 .. versionadded:: 2.0
65
66 .. versionchanged:: 2.6
67 *timeout* was added.
68
Gregory P. Smith9d325212010-01-03 02:06:07 +000069 .. versionchanged:: 2.7
70 *source_address* was added.
Georg Brandl8ec7f652007-08-15 14:28:01 +000071
Gregory P. Smith9d325212010-01-03 02:06:07 +000072
Benjamin Peterson227f6e02014-12-07 13:41:26 -050073.. class:: HTTPSConnection(host[, port[, key_file[, cert_file[, strict[, timeout[, source_address[, context]]]]]]])
Georg Brandl8ec7f652007-08-15 14:28:01 +000074
75 A subclass of :class:`HTTPConnection` that uses SSL for communication with
Benjamin Petersonfcfb18e2014-11-23 11:42:45 -060076 secure servers. Default port is ``443``. If *context* is specified, it must
77 be a :class:`ssl.SSLContext` instance describing the various SSL options.
78
79 *key_file* and *cert_file* are deprecated, please use
80 :meth:`ssl.SSLContext.load_cert_chain` instead, or let
81 :func:`ssl.create_default_context` select the system's trusted CA
82 certificates for you.
83
84 Please read :ref:`ssl-security` for more information on best practices.
85
Georg Brandl8ec7f652007-08-15 14:28:01 +000086 .. versionadded:: 2.0
87
88 .. versionchanged:: 2.6
89 *timeout* was added.
90
Gregory P. Smith9d325212010-01-03 02:06:07 +000091 .. versionchanged:: 2.7
92 *source_address* was added.
93
Benjamin Petersonfcfb18e2014-11-23 11:42:45 -060094 .. versionchanged:: 2.7.9
Benjamin Peterson227f6e02014-12-07 13:41:26 -050095 *context* was added.
Benjamin Petersonfcfb18e2014-11-23 11:42:45 -060096
Benjamin Petersone3e7d402014-11-23 21:02:02 -060097 This class now performs all the necessary certificate and hostname checks
98 by default. To revert to the previous, unverified, behavior
99 :func:`ssl._create_unverified_context` can be passed to the *context*
100 parameter.
101
Georg Brandl8ec7f652007-08-15 14:28:01 +0000102
Hynek Schlawacke58ce012012-05-22 10:27:40 +0200103.. class:: HTTPResponse(sock, debuglevel=0, strict=0)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000104
105 Class whose instances are returned upon successful connection. Not instantiated
106 directly by user.
107
108 .. versionadded:: 2.0
109
Senthil Kumaran2af7e6d2010-06-28 10:54:17 +0000110.. class:: HTTPMessage
111
112 An :class:`HTTPMessage` instance is used to hold the headers from an HTTP
113 response. It is implemented using the :class:`mimetools.Message` class and
114 provides utility functions to deal with HTTP Headers. It is not directly
115 instantiated by the users.
116
117
Georg Brandl8ec7f652007-08-15 14:28:01 +0000118The following exceptions are raised as appropriate:
119
120
121.. exception:: HTTPException
122
123 The base class of the other exceptions in this module. It is a subclass of
124 :exc:`Exception`.
125
126 .. versionadded:: 2.0
127
128
129.. exception:: NotConnected
130
131 A subclass of :exc:`HTTPException`.
132
133 .. versionadded:: 2.0
134
135
136.. exception:: InvalidURL
137
138 A subclass of :exc:`HTTPException`, raised if a port is given and is either
139 non-numeric or empty.
140
141 .. versionadded:: 2.3
142
143
144.. exception:: UnknownProtocol
145
146 A subclass of :exc:`HTTPException`.
147
148 .. versionadded:: 2.0
149
150
151.. exception:: UnknownTransferEncoding
152
153 A subclass of :exc:`HTTPException`.
154
155 .. versionadded:: 2.0
156
157
158.. exception:: UnimplementedFileMode
159
160 A subclass of :exc:`HTTPException`.
161
162 .. versionadded:: 2.0
163
164
165.. exception:: IncompleteRead
166
167 A subclass of :exc:`HTTPException`.
168
169 .. versionadded:: 2.0
170
171
172.. exception:: ImproperConnectionState
173
174 A subclass of :exc:`HTTPException`.
175
176 .. versionadded:: 2.0
177
178
179.. exception:: CannotSendRequest
180
181 A subclass of :exc:`ImproperConnectionState`.
182
183 .. versionadded:: 2.0
184
185
186.. exception:: CannotSendHeader
187
188 A subclass of :exc:`ImproperConnectionState`.
189
190 .. versionadded:: 2.0
191
192
193.. exception:: ResponseNotReady
194
195 A subclass of :exc:`ImproperConnectionState`.
196
197 .. versionadded:: 2.0
198
199
200.. exception:: BadStatusLine
201
202 A subclass of :exc:`HTTPException`. Raised if a server responds with a HTTP
203 status code that we don't understand.
204
205 .. versionadded:: 2.0
206
207The constants defined in this module are:
208
209
210.. data:: HTTP_PORT
211
212 The default port for the HTTP protocol (always ``80``).
213
214
215.. data:: HTTPS_PORT
216
217 The default port for the HTTPS protocol (always ``443``).
218
219and also the following constants for integer status codes:
220
221+------------------------------------------+---------+-----------------------------------------------------------------------+
222| Constant | Value | Definition |
223+==========================================+=========+=======================================================================+
224| :const:`CONTINUE` | ``100`` | HTTP/1.1, `RFC 2616, Section |
225| | | 10.1.1 |
226| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.1.1>`_ |
227+------------------------------------------+---------+-----------------------------------------------------------------------+
228| :const:`SWITCHING_PROTOCOLS` | ``101`` | HTTP/1.1, `RFC 2616, Section |
229| | | 10.1.2 |
230| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.1.2>`_ |
231+------------------------------------------+---------+-----------------------------------------------------------------------+
232| :const:`PROCESSING` | ``102`` | WEBDAV, `RFC 2518, Section 10.1 |
233| | | <http://www.webdav.org/specs/rfc2518.html#STATUS_102>`_ |
234+------------------------------------------+---------+-----------------------------------------------------------------------+
235| :const:`OK` | ``200`` | HTTP/1.1, `RFC 2616, Section |
236| | | 10.2.1 |
237| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.1>`_ |
238+------------------------------------------+---------+-----------------------------------------------------------------------+
239| :const:`CREATED` | ``201`` | HTTP/1.1, `RFC 2616, Section |
240| | | 10.2.2 |
241| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.2>`_ |
242+------------------------------------------+---------+-----------------------------------------------------------------------+
243| :const:`ACCEPTED` | ``202`` | HTTP/1.1, `RFC 2616, Section |
244| | | 10.2.3 |
245| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.3>`_ |
246+------------------------------------------+---------+-----------------------------------------------------------------------+
247| :const:`NON_AUTHORITATIVE_INFORMATION` | ``203`` | HTTP/1.1, `RFC 2616, Section |
248| | | 10.2.4 |
249| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.4>`_ |
250+------------------------------------------+---------+-----------------------------------------------------------------------+
251| :const:`NO_CONTENT` | ``204`` | HTTP/1.1, `RFC 2616, Section |
252| | | 10.2.5 |
253| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.5>`_ |
254+------------------------------------------+---------+-----------------------------------------------------------------------+
255| :const:`RESET_CONTENT` | ``205`` | HTTP/1.1, `RFC 2616, Section |
256| | | 10.2.6 |
257| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.6>`_ |
258+------------------------------------------+---------+-----------------------------------------------------------------------+
259| :const:`PARTIAL_CONTENT` | ``206`` | HTTP/1.1, `RFC 2616, Section |
260| | | 10.2.7 |
261| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.7>`_ |
262+------------------------------------------+---------+-----------------------------------------------------------------------+
263| :const:`MULTI_STATUS` | ``207`` | WEBDAV `RFC 2518, Section 10.2 |
264| | | <http://www.webdav.org/specs/rfc2518.html#STATUS_207>`_ |
265+------------------------------------------+---------+-----------------------------------------------------------------------+
266| :const:`IM_USED` | ``226`` | Delta encoding in HTTP, |
267| | | :rfc:`3229`, Section 10.4.1 |
268+------------------------------------------+---------+-----------------------------------------------------------------------+
269| :const:`MULTIPLE_CHOICES` | ``300`` | HTTP/1.1, `RFC 2616, Section |
270| | | 10.3.1 |
271| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.1>`_ |
272+------------------------------------------+---------+-----------------------------------------------------------------------+
273| :const:`MOVED_PERMANENTLY` | ``301`` | HTTP/1.1, `RFC 2616, Section |
274| | | 10.3.2 |
275| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.2>`_ |
276+------------------------------------------+---------+-----------------------------------------------------------------------+
277| :const:`FOUND` | ``302`` | HTTP/1.1, `RFC 2616, Section |
278| | | 10.3.3 |
279| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.3>`_ |
280+------------------------------------------+---------+-----------------------------------------------------------------------+
281| :const:`SEE_OTHER` | ``303`` | HTTP/1.1, `RFC 2616, Section |
282| | | 10.3.4 |
283| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.4>`_ |
284+------------------------------------------+---------+-----------------------------------------------------------------------+
285| :const:`NOT_MODIFIED` | ``304`` | HTTP/1.1, `RFC 2616, Section |
286| | | 10.3.5 |
287| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.5>`_ |
288+------------------------------------------+---------+-----------------------------------------------------------------------+
289| :const:`USE_PROXY` | ``305`` | HTTP/1.1, `RFC 2616, Section |
290| | | 10.3.6 |
291| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.6>`_ |
292+------------------------------------------+---------+-----------------------------------------------------------------------+
293| :const:`TEMPORARY_REDIRECT` | ``307`` | HTTP/1.1, `RFC 2616, Section |
294| | | 10.3.8 |
295| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.8>`_ |
296+------------------------------------------+---------+-----------------------------------------------------------------------+
297| :const:`BAD_REQUEST` | ``400`` | HTTP/1.1, `RFC 2616, Section |
298| | | 10.4.1 |
299| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.1>`_ |
300+------------------------------------------+---------+-----------------------------------------------------------------------+
301| :const:`UNAUTHORIZED` | ``401`` | HTTP/1.1, `RFC 2616, Section |
302| | | 10.4.2 |
303| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.2>`_ |
304+------------------------------------------+---------+-----------------------------------------------------------------------+
305| :const:`PAYMENT_REQUIRED` | ``402`` | HTTP/1.1, `RFC 2616, Section |
306| | | 10.4.3 |
307| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.3>`_ |
308+------------------------------------------+---------+-----------------------------------------------------------------------+
309| :const:`FORBIDDEN` | ``403`` | HTTP/1.1, `RFC 2616, Section |
310| | | 10.4.4 |
311| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.4>`_ |
312+------------------------------------------+---------+-----------------------------------------------------------------------+
313| :const:`NOT_FOUND` | ``404`` | HTTP/1.1, `RFC 2616, Section |
314| | | 10.4.5 |
315| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.5>`_ |
316+------------------------------------------+---------+-----------------------------------------------------------------------+
317| :const:`METHOD_NOT_ALLOWED` | ``405`` | HTTP/1.1, `RFC 2616, Section |
318| | | 10.4.6 |
319| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.6>`_ |
320+------------------------------------------+---------+-----------------------------------------------------------------------+
321| :const:`NOT_ACCEPTABLE` | ``406`` | HTTP/1.1, `RFC 2616, Section |
322| | | 10.4.7 |
323| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.7>`_ |
324+------------------------------------------+---------+-----------------------------------------------------------------------+
325| :const:`PROXY_AUTHENTICATION_REQUIRED` | ``407`` | HTTP/1.1, `RFC 2616, Section |
326| | | 10.4.8 |
327| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.8>`_ |
328+------------------------------------------+---------+-----------------------------------------------------------------------+
329| :const:`REQUEST_TIMEOUT` | ``408`` | HTTP/1.1, `RFC 2616, Section |
330| | | 10.4.9 |
331| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.9>`_ |
332+------------------------------------------+---------+-----------------------------------------------------------------------+
333| :const:`CONFLICT` | ``409`` | HTTP/1.1, `RFC 2616, Section |
334| | | 10.4.10 |
335| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.10>`_ |
336+------------------------------------------+---------+-----------------------------------------------------------------------+
337| :const:`GONE` | ``410`` | HTTP/1.1, `RFC 2616, Section |
338| | | 10.4.11 |
339| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.11>`_ |
340+------------------------------------------+---------+-----------------------------------------------------------------------+
341| :const:`LENGTH_REQUIRED` | ``411`` | HTTP/1.1, `RFC 2616, Section |
342| | | 10.4.12 |
343| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.12>`_ |
344+------------------------------------------+---------+-----------------------------------------------------------------------+
345| :const:`PRECONDITION_FAILED` | ``412`` | HTTP/1.1, `RFC 2616, Section |
346| | | 10.4.13 |
347| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.13>`_ |
348+------------------------------------------+---------+-----------------------------------------------------------------------+
349| :const:`REQUEST_ENTITY_TOO_LARGE` | ``413`` | HTTP/1.1, `RFC 2616, Section |
350| | | 10.4.14 |
351| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.14>`_ |
352+------------------------------------------+---------+-----------------------------------------------------------------------+
353| :const:`REQUEST_URI_TOO_LONG` | ``414`` | HTTP/1.1, `RFC 2616, Section |
354| | | 10.4.15 |
355| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.15>`_ |
356+------------------------------------------+---------+-----------------------------------------------------------------------+
357| :const:`UNSUPPORTED_MEDIA_TYPE` | ``415`` | HTTP/1.1, `RFC 2616, Section |
358| | | 10.4.16 |
359| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.16>`_ |
360+------------------------------------------+---------+-----------------------------------------------------------------------+
361| :const:`REQUESTED_RANGE_NOT_SATISFIABLE` | ``416`` | HTTP/1.1, `RFC 2616, Section |
362| | | 10.4.17 |
363| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.17>`_ |
364+------------------------------------------+---------+-----------------------------------------------------------------------+
365| :const:`EXPECTATION_FAILED` | ``417`` | HTTP/1.1, `RFC 2616, Section |
366| | | 10.4.18 |
367| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.18>`_ |
368+------------------------------------------+---------+-----------------------------------------------------------------------+
369| :const:`UNPROCESSABLE_ENTITY` | ``422`` | WEBDAV, `RFC 2518, Section 10.3 |
370| | | <http://www.webdav.org/specs/rfc2518.html#STATUS_422>`_ |
371+------------------------------------------+---------+-----------------------------------------------------------------------+
372| :const:`LOCKED` | ``423`` | WEBDAV `RFC 2518, Section 10.4 |
373| | | <http://www.webdav.org/specs/rfc2518.html#STATUS_423>`_ |
374+------------------------------------------+---------+-----------------------------------------------------------------------+
375| :const:`FAILED_DEPENDENCY` | ``424`` | WEBDAV, `RFC 2518, Section 10.5 |
376| | | <http://www.webdav.org/specs/rfc2518.html#STATUS_424>`_ |
377+------------------------------------------+---------+-----------------------------------------------------------------------+
378| :const:`UPGRADE_REQUIRED` | ``426`` | HTTP Upgrade to TLS, |
379| | | :rfc:`2817`, Section 6 |
380+------------------------------------------+---------+-----------------------------------------------------------------------+
381| :const:`INTERNAL_SERVER_ERROR` | ``500`` | HTTP/1.1, `RFC 2616, Section |
382| | | 10.5.1 |
383| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.1>`_ |
384+------------------------------------------+---------+-----------------------------------------------------------------------+
385| :const:`NOT_IMPLEMENTED` | ``501`` | HTTP/1.1, `RFC 2616, Section |
386| | | 10.5.2 |
387| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.2>`_ |
388+------------------------------------------+---------+-----------------------------------------------------------------------+
389| :const:`BAD_GATEWAY` | ``502`` | HTTP/1.1 `RFC 2616, Section |
390| | | 10.5.3 |
391| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.3>`_ |
392+------------------------------------------+---------+-----------------------------------------------------------------------+
393| :const:`SERVICE_UNAVAILABLE` | ``503`` | HTTP/1.1, `RFC 2616, Section |
394| | | 10.5.4 |
395| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.4>`_ |
396+------------------------------------------+---------+-----------------------------------------------------------------------+
397| :const:`GATEWAY_TIMEOUT` | ``504`` | HTTP/1.1 `RFC 2616, Section |
398| | | 10.5.5 |
399| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.5>`_ |
400+------------------------------------------+---------+-----------------------------------------------------------------------+
401| :const:`HTTP_VERSION_NOT_SUPPORTED` | ``505`` | HTTP/1.1, `RFC 2616, Section |
402| | | 10.5.6 |
403| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.6>`_ |
404+------------------------------------------+---------+-----------------------------------------------------------------------+
405| :const:`INSUFFICIENT_STORAGE` | ``507`` | WEBDAV, `RFC 2518, Section 10.6 |
406| | | <http://www.webdav.org/specs/rfc2518.html#STATUS_507>`_ |
407+------------------------------------------+---------+-----------------------------------------------------------------------+
408| :const:`NOT_EXTENDED` | ``510`` | An HTTP Extension Framework, |
409| | | :rfc:`2774`, Section 7 |
410+------------------------------------------+---------+-----------------------------------------------------------------------+
411
412
413.. data:: responses
414
415 This dictionary maps the HTTP 1.1 status codes to the W3C names.
416
417 Example: ``httplib.responses[httplib.NOT_FOUND]`` is ``'Not Found'``.
418
419 .. versionadded:: 2.5
420
421
422.. _httpconnection-objects:
423
424HTTPConnection Objects
425----------------------
426
427:class:`HTTPConnection` instances have the following methods:
428
429
430.. method:: HTTPConnection.request(method, url[, body[, headers]])
431
432 This will send a request to the server using the HTTP request method *method*
433 and the selector *url*. If the *body* argument is present, it should be a
434 string of data to send after the headers are finished. Alternatively, it may
435 be an open file object, in which case the contents of the file is sent; this
R David Murrayb4b000f2015-03-22 15:15:44 -0400436 file object should support ``fileno()`` and ``read()`` methods. The
437 *headers* argument should be a mapping of extra HTTP headers to send with
438 the request.
439
440 If one is not provided in *headers*, a ``Content-Length`` header is added
441 automatically for all methods if the length of the body can be determined,
442 either from the length of the ``str`` representation, or from the reported
443 size of the file on disk. If *body* is ``None`` the header is not set except
444 for methods that expect a body (``PUT``, ``POST``, and ``PATCH``) in which
445 case it is set to ``0``.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000446
447 .. versionchanged:: 2.6
448 *body* can be a file object.
449
450
451.. method:: HTTPConnection.getresponse()
452
453 Should be called after a request is sent to get the response from the server.
454 Returns an :class:`HTTPResponse` instance.
455
456 .. note::
457
458 Note that you must have read the whole response before you can send a new
459 request to the server.
460
461
462.. method:: HTTPConnection.set_debuglevel(level)
463
464 Set the debugging level (the amount of debugging output printed). The default
465 debug level is ``0``, meaning no debugging output is printed.
466
467
Senthil Kumaran0732fd92009-12-20 07:29:31 +0000468.. method:: HTTPConnection.set_tunnel(host,port=None, headers=None)
Senthil Kumarane266f252009-05-24 09:14:50 +0000469
470 Set the host and the port for HTTP Connect Tunnelling. Normally used when
471 it is required to do HTTPS Conection through a proxy server.
472
Sandro Tosi45262732011-12-25 11:43:37 +0100473 The headers argument should be a mapping of extra HTTP headers to send
Senthil Kumaran0732fd92009-12-20 07:29:31 +0000474 with the CONNECT request.
475
Gregory P. Smithd03e1b42009-05-24 18:00:13 +0000476 .. versionadded:: 2.7
477
Senthil Kumarane266f252009-05-24 09:14:50 +0000478
Georg Brandl8ec7f652007-08-15 14:28:01 +0000479.. method:: HTTPConnection.connect()
480
481 Connect to the server specified when the object was created.
482
483
484.. method:: HTTPConnection.close()
485
486 Close the connection to the server.
487
488As an alternative to using the :meth:`request` method described above, you can
489also send your request step by step, by using the four functions below.
490
491
492.. method:: HTTPConnection.putrequest(request, selector[, skip_host[, skip_accept_encoding]])
493
494 This should be the first call after the connection to the server has been made.
495 It sends a line to the server consisting of the *request* string, the *selector*
496 string, and the HTTP version (``HTTP/1.1``). To disable automatic sending of
497 ``Host:`` or ``Accept-Encoding:`` headers (for example to accept additional
498 content encodings), specify *skip_host* or *skip_accept_encoding* with non-False
499 values.
500
501 .. versionchanged:: 2.4
502 *skip_accept_encoding* argument added.
503
504
505.. method:: HTTPConnection.putheader(header, argument[, ...])
506
507 Send an :rfc:`822`\ -style header to the server. It sends a line to the server
508 consisting of the header, a colon and a space, and the first argument. If more
509 arguments are given, continuation lines are sent, each consisting of a tab and
510 an argument.
511
512
Senthil Kumaran83cc5122011-10-03 07:37:58 +0800513.. method:: HTTPConnection.endheaders(message_body=None)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000514
Senthil Kumaran41d663f2011-10-05 23:52:49 +0800515 Send a blank line to the server, signalling the end of the headers. The
516 optional *message_body* argument can be used to pass a message body
517 associated with the request. The message body will be sent in the same
518 packet as the message headers if it is string, otherwise it is sent in a
519 separate packet.
520
521 .. versionchanged:: 2.7
522 *message_body* was added.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000523
524
525.. method:: HTTPConnection.send(data)
526
527 Send data to the server. This should be used directly only after the
528 :meth:`endheaders` method has been called and before :meth:`getresponse` is
529 called.
530
531
532.. _httpresponse-objects:
533
534HTTPResponse Objects
535--------------------
536
537:class:`HTTPResponse` instances have the following methods and attributes:
538
539
540.. method:: HTTPResponse.read([amt])
541
542 Reads and returns the response body, or up to the next *amt* bytes.
543
544
545.. method:: HTTPResponse.getheader(name[, default])
546
547 Get the contents of the header *name*, or *default* if there is no matching
548 header.
549
550
551.. method:: HTTPResponse.getheaders()
552
553 Return a list of (header, value) tuples.
554
555 .. versionadded:: 2.4
556
Senthil Kumaranc916dd72010-09-21 02:10:45 +0000557.. method:: HTTPResponse.fileno()
558
559 Returns the ``fileno`` of the underlying socket.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000560
561.. attribute:: HTTPResponse.msg
562
563 A :class:`mimetools.Message` instance containing the response headers.
564
565
566.. attribute:: HTTPResponse.version
567
568 HTTP protocol version used by server. 10 for HTTP/1.0, 11 for HTTP/1.1.
569
570
571.. attribute:: HTTPResponse.status
572
573 Status code returned by server.
574
575
576.. attribute:: HTTPResponse.reason
577
578 Reason phrase returned by server.
579
580
581.. _httplib-examples:
582
583Examples
584--------
585
586Here is an example session that uses the ``GET`` method::
587
588 >>> import httplib
589 >>> conn = httplib.HTTPConnection("www.python.org")
590 >>> conn.request("GET", "/index.html")
591 >>> r1 = conn.getresponse()
592 >>> print r1.status, r1.reason
593 200 OK
594 >>> data1 = r1.read()
595 >>> conn.request("GET", "/parrot.spam")
596 >>> r2 = conn.getresponse()
597 >>> print r2.status, r2.reason
598 404 Not Found
599 >>> data2 = r2.read()
600 >>> conn.close()
601
Fred Drake58404692010-05-12 01:22:03 +0000602Here is an example session that uses the ``HEAD`` method. Note that the
603``HEAD`` method never returns any data. ::
Senthil Kumaraned9204342010-04-28 17:20:43 +0000604
605 >>> import httplib
606 >>> conn = httplib.HTTPConnection("www.python.org")
607 >>> conn.request("HEAD","/index.html")
608 >>> res = conn.getresponse()
609 >>> print res.status, res.reason
610 200 OK
611 >>> data = res.read()
612 >>> print len(data)
613 0
614 >>> data == ''
615 True
616
Georg Brandl8ec7f652007-08-15 14:28:01 +0000617Here is an example session that shows how to ``POST`` requests::
618
619 >>> import httplib, urllib
Senthil Kumarancd57ef12011-07-20 22:02:27 +0800620 >>> params = urllib.urlencode({'@number': 12524, '@type': 'issue', '@action': 'show'})
Georg Brandl8ec7f652007-08-15 14:28:01 +0000621 >>> headers = {"Content-type": "application/x-www-form-urlencoded",
622 ... "Accept": "text/plain"}
Senthil Kumarancd57ef12011-07-20 22:02:27 +0800623 >>> conn = httplib.HTTPConnection("bugs.python.org")
624 >>> conn.request("POST", "", params, headers)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000625 >>> response = conn.getresponse()
626 >>> print response.status, response.reason
Senthil Kumarancd57ef12011-07-20 22:02:27 +0800627 302 Found
Georg Brandl8ec7f652007-08-15 14:28:01 +0000628 >>> data = response.read()
Senthil Kumarancd57ef12011-07-20 22:02:27 +0800629 >>> data
630 'Redirecting to <a href="http://bugs.python.org/issue12524">http://bugs.python.org/issue12524</a>'
Georg Brandl8ec7f652007-08-15 14:28:01 +0000631 >>> conn.close()
632
Senthil Kumaran69c66f92013-03-13 13:30:25 -0700633Client side ``HTTP PUT`` requests are very similar to ``POST`` requests. The
634difference lies only the server side where HTTP server will allow resources to
635be created via ``PUT`` request. Here is an example session that shows how to do
636``PUT`` request using httplib::
637
638 >>> # This creates an HTTP message
639 >>> # with the content of BODY as the enclosed representation
640 >>> # for the resource http://localhost:8080/foobar
641 ...
642 >>> import httplib
643 >>> BODY = "***filecontents***"
644 >>> conn = httplib.HTTPConnection("localhost", 8080)
645 >>> conn.request("PUT", "/file", BODY)
646 >>> response = conn.getresponse()
Georg Brandld6896502013-10-06 12:42:18 +0200647 >>> print response.status, response.reason
Senthil Kumaran69c66f92013-03-13 13:30:25 -0700648 200, OK
649