1 :mod:`httplib` --- HTTP protocol client
2 =======================================
5 :synopsis: HTTP and HTTPS protocol client (requires sockets).
8 The :mod:`httplib` module has been renamed to :mod:`http.client` in Python
9 3.0. The :term:`2to3` tool will automatically adapt imports when converting
15 single: HTTP; httplib (standard module)
17 .. index:: module: urllib
19 This module defines classes which implement the client side of the HTTP and
20 HTTPS protocols. It is normally not used directly --- the module :mod:`urllib`
21 uses it to handle URLs that use HTTP and HTTPS.
25 HTTPS support is only available if the :mod:`socket` module was compiled with
30 The public interface for this module changed substantially in Python 2.0. The
31 :class:`HTTP` class is retained only for backward compatibility with 1.5.2. It
32 should not be used in new code. Refer to the online docstrings for usage.
34 The module provides the following classes:
37 .. class:: HTTPConnection(host[, port[, strict[, timeout]]])
39 An :class:`HTTPConnection` instance represents one transaction with an HTTP
40 server. It should be instantiated passing it a host and optional port
41 number. If no port number is passed, the port is extracted from the host
42 string if it has the form ``host:port``, else the default HTTP port (80) is
43 used. When True, the optional parameter *strict* (which defaults to a false
44 value) causes ``BadStatusLine`` to
45 be raised if the status line can't be parsed as a valid HTTP/1.0 or 1.1
46 status line. If the optional *timeout* parameter is given, blocking
47 operations (like connection attempts) will timeout after that many seconds
48 (if it is not given, the global default timeout setting is used).
50 For example, the following calls all create instances that connect to the server
51 at the same host and port::
53 >>> h1 = httplib.HTTPConnection('www.cwi.nl')
54 >>> h2 = httplib.HTTPConnection('www.cwi.nl:80')
55 >>> h3 = httplib.HTTPConnection('www.cwi.nl', 80)
56 >>> h3 = httplib.HTTPConnection('www.cwi.nl', 80, timeout=10)
60 .. versionchanged:: 2.6
64 .. class:: HTTPSConnection(host[, port[, key_file[, cert_file[, strict[, timeout]]]]])
66 A subclass of :class:`HTTPConnection` that uses SSL for communication with
67 secure servers. Default port is ``443``. *key_file* is the name of a PEM
68 formatted file that contains your private key. *cert_file* is a PEM formatted
69 certificate chain file.
73 This does not do any certificate verification.
77 .. versionchanged:: 2.6
81 .. class:: HTTPResponse(sock[, debuglevel=0][, strict=0])
83 Class whose instances are returned upon successful connection. Not instantiated
88 The following exceptions are raised as appropriate:
91 .. exception:: HTTPException
93 The base class of the other exceptions in this module. It is a subclass of
99 .. exception:: NotConnected
101 A subclass of :exc:`HTTPException`.
103 .. versionadded:: 2.0
106 .. exception:: InvalidURL
108 A subclass of :exc:`HTTPException`, raised if a port is given and is either
109 non-numeric or empty.
111 .. versionadded:: 2.3
114 .. exception:: UnknownProtocol
116 A subclass of :exc:`HTTPException`.
118 .. versionadded:: 2.0
121 .. exception:: UnknownTransferEncoding
123 A subclass of :exc:`HTTPException`.
125 .. versionadded:: 2.0
128 .. exception:: UnimplementedFileMode
130 A subclass of :exc:`HTTPException`.
132 .. versionadded:: 2.0
135 .. exception:: IncompleteRead
137 A subclass of :exc:`HTTPException`.
139 .. versionadded:: 2.0
142 .. exception:: ImproperConnectionState
144 A subclass of :exc:`HTTPException`.
146 .. versionadded:: 2.0
149 .. exception:: CannotSendRequest
151 A subclass of :exc:`ImproperConnectionState`.
153 .. versionadded:: 2.0
156 .. exception:: CannotSendHeader
158 A subclass of :exc:`ImproperConnectionState`.
160 .. versionadded:: 2.0
163 .. exception:: ResponseNotReady
165 A subclass of :exc:`ImproperConnectionState`.
167 .. versionadded:: 2.0
170 .. exception:: BadStatusLine
172 A subclass of :exc:`HTTPException`. Raised if a server responds with a HTTP
173 status code that we don't understand.
175 .. versionadded:: 2.0
177 The constants defined in this module are:
182 The default port for the HTTP protocol (always ``80``).
187 The default port for the HTTPS protocol (always ``443``).
189 and also the following constants for integer status codes:
191 +------------------------------------------+---------+-----------------------------------------------------------------------+
192 | Constant | Value | Definition |
193 +==========================================+=========+=======================================================================+
194 | :const:`CONTINUE` | ``100`` | HTTP/1.1, `RFC 2616, Section |
196 | | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.1.1>`_ |
197 +------------------------------------------+---------+-----------------------------------------------------------------------+
198 | :const:`SWITCHING_PROTOCOLS` | ``101`` | HTTP/1.1, `RFC 2616, Section |
200 | | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.1.2>`_ |
201 +------------------------------------------+---------+-----------------------------------------------------------------------+
202 | :const:`PROCESSING` | ``102`` | WEBDAV, `RFC 2518, Section 10.1 |
203 | | | <http://www.webdav.org/specs/rfc2518.html#STATUS_102>`_ |
204 +------------------------------------------+---------+-----------------------------------------------------------------------+
205 | :const:`OK` | ``200`` | HTTP/1.1, `RFC 2616, Section |
207 | | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.1>`_ |
208 +------------------------------------------+---------+-----------------------------------------------------------------------+
209 | :const:`CREATED` | ``201`` | HTTP/1.1, `RFC 2616, Section |
211 | | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.2>`_ |
212 +------------------------------------------+---------+-----------------------------------------------------------------------+
213 | :const:`ACCEPTED` | ``202`` | HTTP/1.1, `RFC 2616, Section |
215 | | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.3>`_ |
216 +------------------------------------------+---------+-----------------------------------------------------------------------+
217 | :const:`NON_AUTHORITATIVE_INFORMATION` | ``203`` | HTTP/1.1, `RFC 2616, Section |
219 | | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.4>`_ |
220 +------------------------------------------+---------+-----------------------------------------------------------------------+
221 | :const:`NO_CONTENT` | ``204`` | HTTP/1.1, `RFC 2616, Section |
223 | | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.5>`_ |
224 +------------------------------------------+---------+-----------------------------------------------------------------------+
225 | :const:`RESET_CONTENT` | ``205`` | HTTP/1.1, `RFC 2616, Section |
227 | | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.6>`_ |
228 +------------------------------------------+---------+-----------------------------------------------------------------------+
229 | :const:`PARTIAL_CONTENT` | ``206`` | HTTP/1.1, `RFC 2616, Section |
231 | | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.7>`_ |
232 +------------------------------------------+---------+-----------------------------------------------------------------------+
233 | :const:`MULTI_STATUS` | ``207`` | WEBDAV `RFC 2518, Section 10.2 |
234 | | | <http://www.webdav.org/specs/rfc2518.html#STATUS_207>`_ |
235 +------------------------------------------+---------+-----------------------------------------------------------------------+
236 | :const:`IM_USED` | ``226`` | Delta encoding in HTTP, |
237 | | | :rfc:`3229`, Section 10.4.1 |
238 +------------------------------------------+---------+-----------------------------------------------------------------------+
239 | :const:`MULTIPLE_CHOICES` | ``300`` | HTTP/1.1, `RFC 2616, Section |
241 | | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.1>`_ |
242 +------------------------------------------+---------+-----------------------------------------------------------------------+
243 | :const:`MOVED_PERMANENTLY` | ``301`` | HTTP/1.1, `RFC 2616, Section |
245 | | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.2>`_ |
246 +------------------------------------------+---------+-----------------------------------------------------------------------+
247 | :const:`FOUND` | ``302`` | HTTP/1.1, `RFC 2616, Section |
249 | | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.3>`_ |
250 +------------------------------------------+---------+-----------------------------------------------------------------------+
251 | :const:`SEE_OTHER` | ``303`` | HTTP/1.1, `RFC 2616, Section |
253 | | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.4>`_ |
254 +------------------------------------------+---------+-----------------------------------------------------------------------+
255 | :const:`NOT_MODIFIED` | ``304`` | HTTP/1.1, `RFC 2616, Section |
257 | | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.5>`_ |
258 +------------------------------------------+---------+-----------------------------------------------------------------------+
259 | :const:`USE_PROXY` | ``305`` | HTTP/1.1, `RFC 2616, Section |
261 | | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.6>`_ |
262 +------------------------------------------+---------+-----------------------------------------------------------------------+
263 | :const:`TEMPORARY_REDIRECT` | ``307`` | HTTP/1.1, `RFC 2616, Section |
265 | | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.8>`_ |
266 +------------------------------------------+---------+-----------------------------------------------------------------------+
267 | :const:`BAD_REQUEST` | ``400`` | HTTP/1.1, `RFC 2616, Section |
269 | | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.1>`_ |
270 +------------------------------------------+---------+-----------------------------------------------------------------------+
271 | :const:`UNAUTHORIZED` | ``401`` | HTTP/1.1, `RFC 2616, Section |
273 | | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.2>`_ |
274 +------------------------------------------+---------+-----------------------------------------------------------------------+
275 | :const:`PAYMENT_REQUIRED` | ``402`` | HTTP/1.1, `RFC 2616, Section |
277 | | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.3>`_ |
278 +------------------------------------------+---------+-----------------------------------------------------------------------+
279 | :const:`FORBIDDEN` | ``403`` | HTTP/1.1, `RFC 2616, Section |
281 | | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.4>`_ |
282 +------------------------------------------+---------+-----------------------------------------------------------------------+
283 | :const:`NOT_FOUND` | ``404`` | HTTP/1.1, `RFC 2616, Section |
285 | | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.5>`_ |
286 +------------------------------------------+---------+-----------------------------------------------------------------------+
287 | :const:`METHOD_NOT_ALLOWED` | ``405`` | HTTP/1.1, `RFC 2616, Section |
289 | | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.6>`_ |
290 +------------------------------------------+---------+-----------------------------------------------------------------------+
291 | :const:`NOT_ACCEPTABLE` | ``406`` | HTTP/1.1, `RFC 2616, Section |
293 | | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.7>`_ |
294 +------------------------------------------+---------+-----------------------------------------------------------------------+
295 | :const:`PROXY_AUTHENTICATION_REQUIRED` | ``407`` | HTTP/1.1, `RFC 2616, Section |
297 | | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.8>`_ |
298 +------------------------------------------+---------+-----------------------------------------------------------------------+
299 | :const:`REQUEST_TIMEOUT` | ``408`` | HTTP/1.1, `RFC 2616, Section |
301 | | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.9>`_ |
302 +------------------------------------------+---------+-----------------------------------------------------------------------+
303 | :const:`CONFLICT` | ``409`` | HTTP/1.1, `RFC 2616, Section |
305 | | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.10>`_ |
306 +------------------------------------------+---------+-----------------------------------------------------------------------+
307 | :const:`GONE` | ``410`` | HTTP/1.1, `RFC 2616, Section |
309 | | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.11>`_ |
310 +------------------------------------------+---------+-----------------------------------------------------------------------+
311 | :const:`LENGTH_REQUIRED` | ``411`` | HTTP/1.1, `RFC 2616, Section |
313 | | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.12>`_ |
314 +------------------------------------------+---------+-----------------------------------------------------------------------+
315 | :const:`PRECONDITION_FAILED` | ``412`` | HTTP/1.1, `RFC 2616, Section |
317 | | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.13>`_ |
318 +------------------------------------------+---------+-----------------------------------------------------------------------+
319 | :const:`REQUEST_ENTITY_TOO_LARGE` | ``413`` | HTTP/1.1, `RFC 2616, Section |
321 | | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.14>`_ |
322 +------------------------------------------+---------+-----------------------------------------------------------------------+
323 | :const:`REQUEST_URI_TOO_LONG` | ``414`` | HTTP/1.1, `RFC 2616, Section |
325 | | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.15>`_ |
326 +------------------------------------------+---------+-----------------------------------------------------------------------+
327 | :const:`UNSUPPORTED_MEDIA_TYPE` | ``415`` | HTTP/1.1, `RFC 2616, Section |
329 | | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.16>`_ |
330 +------------------------------------------+---------+-----------------------------------------------------------------------+
331 | :const:`REQUESTED_RANGE_NOT_SATISFIABLE` | ``416`` | HTTP/1.1, `RFC 2616, Section |
333 | | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.17>`_ |
334 +------------------------------------------+---------+-----------------------------------------------------------------------+
335 | :const:`EXPECTATION_FAILED` | ``417`` | HTTP/1.1, `RFC 2616, Section |
337 | | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.18>`_ |
338 +------------------------------------------+---------+-----------------------------------------------------------------------+
339 | :const:`UNPROCESSABLE_ENTITY` | ``422`` | WEBDAV, `RFC 2518, Section 10.3 |
340 | | | <http://www.webdav.org/specs/rfc2518.html#STATUS_422>`_ |
341 +------------------------------------------+---------+-----------------------------------------------------------------------+
342 | :const:`LOCKED` | ``423`` | WEBDAV `RFC 2518, Section 10.4 |
343 | | | <http://www.webdav.org/specs/rfc2518.html#STATUS_423>`_ |
344 +------------------------------------------+---------+-----------------------------------------------------------------------+
345 | :const:`FAILED_DEPENDENCY` | ``424`` | WEBDAV, `RFC 2518, Section 10.5 |
346 | | | <http://www.webdav.org/specs/rfc2518.html#STATUS_424>`_ |
347 +------------------------------------------+---------+-----------------------------------------------------------------------+
348 | :const:`UPGRADE_REQUIRED` | ``426`` | HTTP Upgrade to TLS, |
349 | | | :rfc:`2817`, Section 6 |
350 +------------------------------------------+---------+-----------------------------------------------------------------------+
351 | :const:`INTERNAL_SERVER_ERROR` | ``500`` | HTTP/1.1, `RFC 2616, Section |
353 | | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.1>`_ |
354 +------------------------------------------+---------+-----------------------------------------------------------------------+
355 | :const:`NOT_IMPLEMENTED` | ``501`` | HTTP/1.1, `RFC 2616, Section |
357 | | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.2>`_ |
358 +------------------------------------------+---------+-----------------------------------------------------------------------+
359 | :const:`BAD_GATEWAY` | ``502`` | HTTP/1.1 `RFC 2616, Section |
361 | | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.3>`_ |
362 +------------------------------------------+---------+-----------------------------------------------------------------------+
363 | :const:`SERVICE_UNAVAILABLE` | ``503`` | HTTP/1.1, `RFC 2616, Section |
365 | | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.4>`_ |
366 +------------------------------------------+---------+-----------------------------------------------------------------------+
367 | :const:`GATEWAY_TIMEOUT` | ``504`` | HTTP/1.1 `RFC 2616, Section |
369 | | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.5>`_ |
370 +------------------------------------------+---------+-----------------------------------------------------------------------+
371 | :const:`HTTP_VERSION_NOT_SUPPORTED` | ``505`` | HTTP/1.1, `RFC 2616, Section |
373 | | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.6>`_ |
374 +------------------------------------------+---------+-----------------------------------------------------------------------+
375 | :const:`INSUFFICIENT_STORAGE` | ``507`` | WEBDAV, `RFC 2518, Section 10.6 |
376 | | | <http://www.webdav.org/specs/rfc2518.html#STATUS_507>`_ |
377 +------------------------------------------+---------+-----------------------------------------------------------------------+
378 | :const:`NOT_EXTENDED` | ``510`` | An HTTP Extension Framework, |
379 | | | :rfc:`2774`, Section 7 |
380 +------------------------------------------+---------+-----------------------------------------------------------------------+
385 This dictionary maps the HTTP 1.1 status codes to the W3C names.
387 Example: ``httplib.responses[httplib.NOT_FOUND]`` is ``'Not Found'``.
389 .. versionadded:: 2.5
392 .. _httpconnection-objects:
394 HTTPConnection Objects
395 ----------------------
397 :class:`HTTPConnection` instances have the following methods:
400 .. method:: HTTPConnection.request(method, url[, body[, headers]])
402 This will send a request to the server using the HTTP request method *method*
403 and the selector *url*. If the *body* argument is present, it should be a
404 string of data to send after the headers are finished. Alternatively, it may
405 be an open file object, in which case the contents of the file is sent; this
406 file object should support ``fileno()`` and ``read()`` methods. The header
407 Content-Length is automatically set to the correct value. The *headers*
408 argument should be a mapping of extra HTTP headers to send with the request.
410 .. versionchanged:: 2.6
411 *body* can be a file object.
414 .. method:: HTTPConnection.getresponse()
416 Should be called after a request is sent to get the response from the server.
417 Returns an :class:`HTTPResponse` instance.
421 Note that you must have read the whole response before you can send a new
422 request to the server.
425 .. method:: HTTPConnection.set_debuglevel(level)
427 Set the debugging level (the amount of debugging output printed). The default
428 debug level is ``0``, meaning no debugging output is printed.
431 .. method:: HTTPConnection.set_tunnel(host,port=None)
433 Set the host and the port for HTTP Connect Tunnelling. Normally used when
434 it is required to do HTTPS Conection through a proxy server.
436 .. versionadded:: 2.7
439 .. method:: HTTPConnection.connect()
441 Connect to the server specified when the object was created.
444 .. method:: HTTPConnection.close()
446 Close the connection to the server.
448 As an alternative to using the :meth:`request` method described above, you can
449 also send your request step by step, by using the four functions below.
452 .. method:: HTTPConnection.putrequest(request, selector[, skip_host[, skip_accept_encoding]])
454 This should be the first call after the connection to the server has been made.
455 It sends a line to the server consisting of the *request* string, the *selector*
456 string, and the HTTP version (``HTTP/1.1``). To disable automatic sending of
457 ``Host:`` or ``Accept-Encoding:`` headers (for example to accept additional
458 content encodings), specify *skip_host* or *skip_accept_encoding* with non-False
461 .. versionchanged:: 2.4
462 *skip_accept_encoding* argument added.
465 .. method:: HTTPConnection.putheader(header, argument[, ...])
467 Send an :rfc:`822`\ -style header to the server. It sends a line to the server
468 consisting of the header, a colon and a space, and the first argument. If more
469 arguments are given, continuation lines are sent, each consisting of a tab and
473 .. method:: HTTPConnection.endheaders()
475 Send a blank line to the server, signalling the end of the headers.
478 .. method:: HTTPConnection.send(data)
480 Send data to the server. This should be used directly only after the
481 :meth:`endheaders` method has been called and before :meth:`getresponse` is
485 .. _httpresponse-objects:
490 :class:`HTTPResponse` instances have the following methods and attributes:
493 .. method:: HTTPResponse.read([amt])
495 Reads and returns the response body, or up to the next *amt* bytes.
498 .. method:: HTTPResponse.getheader(name[, default])
500 Get the contents of the header *name*, or *default* if there is no matching
504 .. method:: HTTPResponse.getheaders()
506 Return a list of (header, value) tuples.
508 .. versionadded:: 2.4
511 .. attribute:: HTTPResponse.msg
513 A :class:`mimetools.Message` instance containing the response headers.
516 .. attribute:: HTTPResponse.version
518 HTTP protocol version used by server. 10 for HTTP/1.0, 11 for HTTP/1.1.
521 .. attribute:: HTTPResponse.status
523 Status code returned by server.
526 .. attribute:: HTTPResponse.reason
528 Reason phrase returned by server.
531 .. _httplib-examples:
536 Here is an example session that uses the ``GET`` method::
539 >>> conn = httplib.HTTPConnection("www.python.org")
540 >>> conn.request("GET", "/index.html")
541 >>> r1 = conn.getresponse()
542 >>> print r1.status, r1.reason
544 >>> data1 = r1.read()
545 >>> conn.request("GET", "/parrot.spam")
546 >>> r2 = conn.getresponse()
547 >>> print r2.status, r2.reason
549 >>> data2 = r2.read()
552 Here is an example session that shows how to ``POST`` requests::
554 >>> import httplib, urllib
555 >>> params = urllib.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})
556 >>> headers = {"Content-type": "application/x-www-form-urlencoded",
557 ... "Accept": "text/plain"}
558 >>> conn = httplib.HTTPConnection("musi-cal.mojam.com:80")
559 >>> conn.request("POST", "/cgi-bin/query", params, headers)
560 >>> response = conn.getresponse()
561 >>> print response.status, response.reason
563 >>> data = response.read()