blob: aae2219cfba27c47b533ce46540e59b45096717b [file] [log] [blame]
Georg Brandl8ec7f652007-08-15 14:28:01 +00001
2:mod:`httplib` --- HTTP protocol client
3=======================================
4
5.. module:: httplib
6 :synopsis: HTTP and HTTPS protocol client (requires sockets).
7
8
9.. index::
10 pair: HTTP; protocol
11 single: HTTP; httplib (standard module)
12
13.. index:: module: urllib
14
15This module defines classes which implement the client side of the HTTP and
16HTTPS protocols. It is normally not used directly --- the module :mod:`urllib`
17uses it to handle URLs that use HTTP and HTTPS.
18
19.. note::
20
21 HTTPS support is only available if the :mod:`socket` module was compiled with
22 SSL support.
23
24.. note::
25
26 The public interface for this module changed substantially in Python 2.0. The
27 :class:`HTTP` class is retained only for backward compatibility with 1.5.2. It
28 should not be used in new code. Refer to the online docstrings for usage.
29
30The module provides the following classes:
31
32
33.. class:: HTTPConnection(host[, port[, strict[, timeout]]])
34
35 An :class:`HTTPConnection` instance represents one transaction with an HTTP
36 server. It should be instantiated passing it a host and optional port number.
37 If no port number is passed, the port is extracted from the host string if it
38 has the form ``host:port``, else the default HTTP port (80) is used. When True,
39 the optional parameter *strict* causes ``BadStatusLine`` to be raised if the
40 status line can't be parsed as a valid HTTP/1.0 or 1.1 status line. If the
41 optional *timeout* parameter is given, connection attempts will timeout after
42 that many seconds (if it is not given or ``None``, the global default timeout
43 setting is used).
44
45 For example, the following calls all create instances that connect to the server
46 at the same host and port::
47
48 >>> h1 = httplib.HTTPConnection('www.cwi.nl')
49 >>> h2 = httplib.HTTPConnection('www.cwi.nl:80')
50 >>> h3 = httplib.HTTPConnection('www.cwi.nl', 80)
51 >>> h3 = httplib.HTTPConnection('www.cwi.nl', 80, timeout=10)
52
53 .. versionadded:: 2.0
54
55 .. versionchanged:: 2.6
56 *timeout* was added.
57
58
59.. class:: HTTPSConnection(host[, port[, key_file[, cert_file[, strict[, timeout]]]]])
60
61 A subclass of :class:`HTTPConnection` that uses SSL for communication with
62 secure servers. Default port is ``443``. *key_file* is the name of a PEM
63 formatted file that contains your private key. *cert_file* is a PEM formatted
64 certificate chain file.
65
66 .. warning::
67
68 This does not do any certificate verification!
69
70 .. versionadded:: 2.0
71
72 .. versionchanged:: 2.6
73 *timeout* was added.
74
75
76.. class:: HTTPResponse(sock[, debuglevel=0][, strict=0])
77
78 Class whose instances are returned upon successful connection. Not instantiated
79 directly by user.
80
81 .. versionadded:: 2.0
82
83The following exceptions are raised as appropriate:
84
85
86.. exception:: HTTPException
87
88 The base class of the other exceptions in this module. It is a subclass of
89 :exc:`Exception`.
90
91 .. versionadded:: 2.0
92
93
94.. exception:: NotConnected
95
96 A subclass of :exc:`HTTPException`.
97
98 .. versionadded:: 2.0
99
100
101.. exception:: InvalidURL
102
103 A subclass of :exc:`HTTPException`, raised if a port is given and is either
104 non-numeric or empty.
105
106 .. versionadded:: 2.3
107
108
109.. exception:: UnknownProtocol
110
111 A subclass of :exc:`HTTPException`.
112
113 .. versionadded:: 2.0
114
115
116.. exception:: UnknownTransferEncoding
117
118 A subclass of :exc:`HTTPException`.
119
120 .. versionadded:: 2.0
121
122
123.. exception:: UnimplementedFileMode
124
125 A subclass of :exc:`HTTPException`.
126
127 .. versionadded:: 2.0
128
129
130.. exception:: IncompleteRead
131
132 A subclass of :exc:`HTTPException`.
133
134 .. versionadded:: 2.0
135
136
137.. exception:: ImproperConnectionState
138
139 A subclass of :exc:`HTTPException`.
140
141 .. versionadded:: 2.0
142
143
144.. exception:: CannotSendRequest
145
146 A subclass of :exc:`ImproperConnectionState`.
147
148 .. versionadded:: 2.0
149
150
151.. exception:: CannotSendHeader
152
153 A subclass of :exc:`ImproperConnectionState`.
154
155 .. versionadded:: 2.0
156
157
158.. exception:: ResponseNotReady
159
160 A subclass of :exc:`ImproperConnectionState`.
161
162 .. versionadded:: 2.0
163
164
165.. exception:: BadStatusLine
166
167 A subclass of :exc:`HTTPException`. Raised if a server responds with a HTTP
168 status code that we don't understand.
169
170 .. versionadded:: 2.0
171
172The constants defined in this module are:
173
174
175.. data:: HTTP_PORT
176
177 The default port for the HTTP protocol (always ``80``).
178
179
180.. data:: HTTPS_PORT
181
182 The default port for the HTTPS protocol (always ``443``).
183
184and also the following constants for integer status codes:
185
186+------------------------------------------+---------+-----------------------------------------------------------------------+
187| Constant | Value | Definition |
188+==========================================+=========+=======================================================================+
189| :const:`CONTINUE` | ``100`` | HTTP/1.1, `RFC 2616, Section |
190| | | 10.1.1 |
191| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.1.1>`_ |
192+------------------------------------------+---------+-----------------------------------------------------------------------+
193| :const:`SWITCHING_PROTOCOLS` | ``101`` | HTTP/1.1, `RFC 2616, Section |
194| | | 10.1.2 |
195| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.1.2>`_ |
196+------------------------------------------+---------+-----------------------------------------------------------------------+
197| :const:`PROCESSING` | ``102`` | WEBDAV, `RFC 2518, Section 10.1 |
198| | | <http://www.webdav.org/specs/rfc2518.html#STATUS_102>`_ |
199+------------------------------------------+---------+-----------------------------------------------------------------------+
200| :const:`OK` | ``200`` | HTTP/1.1, `RFC 2616, Section |
201| | | 10.2.1 |
202| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.1>`_ |
203+------------------------------------------+---------+-----------------------------------------------------------------------+
204| :const:`CREATED` | ``201`` | HTTP/1.1, `RFC 2616, Section |
205| | | 10.2.2 |
206| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.2>`_ |
207+------------------------------------------+---------+-----------------------------------------------------------------------+
208| :const:`ACCEPTED` | ``202`` | HTTP/1.1, `RFC 2616, Section |
209| | | 10.2.3 |
210| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.3>`_ |
211+------------------------------------------+---------+-----------------------------------------------------------------------+
212| :const:`NON_AUTHORITATIVE_INFORMATION` | ``203`` | HTTP/1.1, `RFC 2616, Section |
213| | | 10.2.4 |
214| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.4>`_ |
215+------------------------------------------+---------+-----------------------------------------------------------------------+
216| :const:`NO_CONTENT` | ``204`` | HTTP/1.1, `RFC 2616, Section |
217| | | 10.2.5 |
218| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.5>`_ |
219+------------------------------------------+---------+-----------------------------------------------------------------------+
220| :const:`RESET_CONTENT` | ``205`` | HTTP/1.1, `RFC 2616, Section |
221| | | 10.2.6 |
222| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.6>`_ |
223+------------------------------------------+---------+-----------------------------------------------------------------------+
224| :const:`PARTIAL_CONTENT` | ``206`` | HTTP/1.1, `RFC 2616, Section |
225| | | 10.2.7 |
226| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.7>`_ |
227+------------------------------------------+---------+-----------------------------------------------------------------------+
228| :const:`MULTI_STATUS` | ``207`` | WEBDAV `RFC 2518, Section 10.2 |
229| | | <http://www.webdav.org/specs/rfc2518.html#STATUS_207>`_ |
230+------------------------------------------+---------+-----------------------------------------------------------------------+
231| :const:`IM_USED` | ``226`` | Delta encoding in HTTP, |
232| | | :rfc:`3229`, Section 10.4.1 |
233+------------------------------------------+---------+-----------------------------------------------------------------------+
234| :const:`MULTIPLE_CHOICES` | ``300`` | HTTP/1.1, `RFC 2616, Section |
235| | | 10.3.1 |
236| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.1>`_ |
237+------------------------------------------+---------+-----------------------------------------------------------------------+
238| :const:`MOVED_PERMANENTLY` | ``301`` | HTTP/1.1, `RFC 2616, Section |
239| | | 10.3.2 |
240| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.2>`_ |
241+------------------------------------------+---------+-----------------------------------------------------------------------+
242| :const:`FOUND` | ``302`` | HTTP/1.1, `RFC 2616, Section |
243| | | 10.3.3 |
244| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.3>`_ |
245+------------------------------------------+---------+-----------------------------------------------------------------------+
246| :const:`SEE_OTHER` | ``303`` | HTTP/1.1, `RFC 2616, Section |
247| | | 10.3.4 |
248| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.4>`_ |
249+------------------------------------------+---------+-----------------------------------------------------------------------+
250| :const:`NOT_MODIFIED` | ``304`` | HTTP/1.1, `RFC 2616, Section |
251| | | 10.3.5 |
252| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.5>`_ |
253+------------------------------------------+---------+-----------------------------------------------------------------------+
254| :const:`USE_PROXY` | ``305`` | HTTP/1.1, `RFC 2616, Section |
255| | | 10.3.6 |
256| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.6>`_ |
257+------------------------------------------+---------+-----------------------------------------------------------------------+
258| :const:`TEMPORARY_REDIRECT` | ``307`` | HTTP/1.1, `RFC 2616, Section |
259| | | 10.3.8 |
260| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.8>`_ |
261+------------------------------------------+---------+-----------------------------------------------------------------------+
262| :const:`BAD_REQUEST` | ``400`` | HTTP/1.1, `RFC 2616, Section |
263| | | 10.4.1 |
264| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.1>`_ |
265+------------------------------------------+---------+-----------------------------------------------------------------------+
266| :const:`UNAUTHORIZED` | ``401`` | HTTP/1.1, `RFC 2616, Section |
267| | | 10.4.2 |
268| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.2>`_ |
269+------------------------------------------+---------+-----------------------------------------------------------------------+
270| :const:`PAYMENT_REQUIRED` | ``402`` | HTTP/1.1, `RFC 2616, Section |
271| | | 10.4.3 |
272| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.3>`_ |
273+------------------------------------------+---------+-----------------------------------------------------------------------+
274| :const:`FORBIDDEN` | ``403`` | HTTP/1.1, `RFC 2616, Section |
275| | | 10.4.4 |
276| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.4>`_ |
277+------------------------------------------+---------+-----------------------------------------------------------------------+
278| :const:`NOT_FOUND` | ``404`` | HTTP/1.1, `RFC 2616, Section |
279| | | 10.4.5 |
280| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.5>`_ |
281+------------------------------------------+---------+-----------------------------------------------------------------------+
282| :const:`METHOD_NOT_ALLOWED` | ``405`` | HTTP/1.1, `RFC 2616, Section |
283| | | 10.4.6 |
284| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.6>`_ |
285+------------------------------------------+---------+-----------------------------------------------------------------------+
286| :const:`NOT_ACCEPTABLE` | ``406`` | HTTP/1.1, `RFC 2616, Section |
287| | | 10.4.7 |
288| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.7>`_ |
289+------------------------------------------+---------+-----------------------------------------------------------------------+
290| :const:`PROXY_AUTHENTICATION_REQUIRED` | ``407`` | HTTP/1.1, `RFC 2616, Section |
291| | | 10.4.8 |
292| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.8>`_ |
293+------------------------------------------+---------+-----------------------------------------------------------------------+
294| :const:`REQUEST_TIMEOUT` | ``408`` | HTTP/1.1, `RFC 2616, Section |
295| | | 10.4.9 |
296| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.9>`_ |
297+------------------------------------------+---------+-----------------------------------------------------------------------+
298| :const:`CONFLICT` | ``409`` | HTTP/1.1, `RFC 2616, Section |
299| | | 10.4.10 |
300| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.10>`_ |
301+------------------------------------------+---------+-----------------------------------------------------------------------+
302| :const:`GONE` | ``410`` | HTTP/1.1, `RFC 2616, Section |
303| | | 10.4.11 |
304| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.11>`_ |
305+------------------------------------------+---------+-----------------------------------------------------------------------+
306| :const:`LENGTH_REQUIRED` | ``411`` | HTTP/1.1, `RFC 2616, Section |
307| | | 10.4.12 |
308| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.12>`_ |
309+------------------------------------------+---------+-----------------------------------------------------------------------+
310| :const:`PRECONDITION_FAILED` | ``412`` | HTTP/1.1, `RFC 2616, Section |
311| | | 10.4.13 |
312| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.13>`_ |
313+------------------------------------------+---------+-----------------------------------------------------------------------+
314| :const:`REQUEST_ENTITY_TOO_LARGE` | ``413`` | HTTP/1.1, `RFC 2616, Section |
315| | | 10.4.14 |
316| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.14>`_ |
317+------------------------------------------+---------+-----------------------------------------------------------------------+
318| :const:`REQUEST_URI_TOO_LONG` | ``414`` | HTTP/1.1, `RFC 2616, Section |
319| | | 10.4.15 |
320| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.15>`_ |
321+------------------------------------------+---------+-----------------------------------------------------------------------+
322| :const:`UNSUPPORTED_MEDIA_TYPE` | ``415`` | HTTP/1.1, `RFC 2616, Section |
323| | | 10.4.16 |
324| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.16>`_ |
325+------------------------------------------+---------+-----------------------------------------------------------------------+
326| :const:`REQUESTED_RANGE_NOT_SATISFIABLE` | ``416`` | HTTP/1.1, `RFC 2616, Section |
327| | | 10.4.17 |
328| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.17>`_ |
329+------------------------------------------+---------+-----------------------------------------------------------------------+
330| :const:`EXPECTATION_FAILED` | ``417`` | HTTP/1.1, `RFC 2616, Section |
331| | | 10.4.18 |
332| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.18>`_ |
333+------------------------------------------+---------+-----------------------------------------------------------------------+
334| :const:`UNPROCESSABLE_ENTITY` | ``422`` | WEBDAV, `RFC 2518, Section 10.3 |
335| | | <http://www.webdav.org/specs/rfc2518.html#STATUS_422>`_ |
336+------------------------------------------+---------+-----------------------------------------------------------------------+
337| :const:`LOCKED` | ``423`` | WEBDAV `RFC 2518, Section 10.4 |
338| | | <http://www.webdav.org/specs/rfc2518.html#STATUS_423>`_ |
339+------------------------------------------+---------+-----------------------------------------------------------------------+
340| :const:`FAILED_DEPENDENCY` | ``424`` | WEBDAV, `RFC 2518, Section 10.5 |
341| | | <http://www.webdav.org/specs/rfc2518.html#STATUS_424>`_ |
342+------------------------------------------+---------+-----------------------------------------------------------------------+
343| :const:`UPGRADE_REQUIRED` | ``426`` | HTTP Upgrade to TLS, |
344| | | :rfc:`2817`, Section 6 |
345+------------------------------------------+---------+-----------------------------------------------------------------------+
346| :const:`INTERNAL_SERVER_ERROR` | ``500`` | HTTP/1.1, `RFC 2616, Section |
347| | | 10.5.1 |
348| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.1>`_ |
349+------------------------------------------+---------+-----------------------------------------------------------------------+
350| :const:`NOT_IMPLEMENTED` | ``501`` | HTTP/1.1, `RFC 2616, Section |
351| | | 10.5.2 |
352| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.2>`_ |
353+------------------------------------------+---------+-----------------------------------------------------------------------+
354| :const:`BAD_GATEWAY` | ``502`` | HTTP/1.1 `RFC 2616, Section |
355| | | 10.5.3 |
356| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.3>`_ |
357+------------------------------------------+---------+-----------------------------------------------------------------------+
358| :const:`SERVICE_UNAVAILABLE` | ``503`` | HTTP/1.1, `RFC 2616, Section |
359| | | 10.5.4 |
360| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.4>`_ |
361+------------------------------------------+---------+-----------------------------------------------------------------------+
362| :const:`GATEWAY_TIMEOUT` | ``504`` | HTTP/1.1 `RFC 2616, Section |
363| | | 10.5.5 |
364| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.5>`_ |
365+------------------------------------------+---------+-----------------------------------------------------------------------+
366| :const:`HTTP_VERSION_NOT_SUPPORTED` | ``505`` | HTTP/1.1, `RFC 2616, Section |
367| | | 10.5.6 |
368| | | <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5.6>`_ |
369+------------------------------------------+---------+-----------------------------------------------------------------------+
370| :const:`INSUFFICIENT_STORAGE` | ``507`` | WEBDAV, `RFC 2518, Section 10.6 |
371| | | <http://www.webdav.org/specs/rfc2518.html#STATUS_507>`_ |
372+------------------------------------------+---------+-----------------------------------------------------------------------+
373| :const:`NOT_EXTENDED` | ``510`` | An HTTP Extension Framework, |
374| | | :rfc:`2774`, Section 7 |
375+------------------------------------------+---------+-----------------------------------------------------------------------+
376
377
378.. data:: responses
379
380 This dictionary maps the HTTP 1.1 status codes to the W3C names.
381
382 Example: ``httplib.responses[httplib.NOT_FOUND]`` is ``'Not Found'``.
383
384 .. versionadded:: 2.5
385
386
387.. _httpconnection-objects:
388
389HTTPConnection Objects
390----------------------
391
392:class:`HTTPConnection` instances have the following methods:
393
394
395.. method:: HTTPConnection.request(method, url[, body[, headers]])
396
397 This will send a request to the server using the HTTP request method *method*
398 and the selector *url*. If the *body* argument is present, it should be a
399 string of data to send after the headers are finished. Alternatively, it may
400 be an open file object, in which case the contents of the file is sent; this
401 file object should support ``fileno()`` and ``read()`` methods. The header
402 Content-Length is automatically set to the correct value. The *headers*
403 argument should be a mapping of extra HTTP headers to send with the request.
404
405 .. versionchanged:: 2.6
406 *body* can be a file object.
407
408
409.. method:: HTTPConnection.getresponse()
410
411 Should be called after a request is sent to get the response from the server.
412 Returns an :class:`HTTPResponse` instance.
413
414 .. note::
415
416 Note that you must have read the whole response before you can send a new
417 request to the server.
418
419
420.. method:: HTTPConnection.set_debuglevel(level)
421
422 Set the debugging level (the amount of debugging output printed). The default
423 debug level is ``0``, meaning no debugging output is printed.
424
425
426.. method:: HTTPConnection.connect()
427
428 Connect to the server specified when the object was created.
429
430
431.. method:: HTTPConnection.close()
432
433 Close the connection to the server.
434
435As an alternative to using the :meth:`request` method described above, you can
436also send your request step by step, by using the four functions below.
437
438
439.. method:: HTTPConnection.putrequest(request, selector[, skip_host[, skip_accept_encoding]])
440
441 This should be the first call after the connection to the server has been made.
442 It sends a line to the server consisting of the *request* string, the *selector*
443 string, and the HTTP version (``HTTP/1.1``). To disable automatic sending of
444 ``Host:`` or ``Accept-Encoding:`` headers (for example to accept additional
445 content encodings), specify *skip_host* or *skip_accept_encoding* with non-False
446 values.
447
448 .. versionchanged:: 2.4
449 *skip_accept_encoding* argument added.
450
451
452.. method:: HTTPConnection.putheader(header, argument[, ...])
453
454 Send an :rfc:`822`\ -style header to the server. It sends a line to the server
455 consisting of the header, a colon and a space, and the first argument. If more
456 arguments are given, continuation lines are sent, each consisting of a tab and
457 an argument.
458
459
460.. method:: HTTPConnection.endheaders()
461
462 Send a blank line to the server, signalling the end of the headers.
463
464
465.. method:: HTTPConnection.send(data)
466
467 Send data to the server. This should be used directly only after the
468 :meth:`endheaders` method has been called and before :meth:`getresponse` is
469 called.
470
471
472.. _httpresponse-objects:
473
474HTTPResponse Objects
475--------------------
476
477:class:`HTTPResponse` instances have the following methods and attributes:
478
479
480.. method:: HTTPResponse.read([amt])
481
482 Reads and returns the response body, or up to the next *amt* bytes.
483
484
485.. method:: HTTPResponse.getheader(name[, default])
486
487 Get the contents of the header *name*, or *default* if there is no matching
488 header.
489
490
491.. method:: HTTPResponse.getheaders()
492
493 Return a list of (header, value) tuples.
494
495 .. versionadded:: 2.4
496
497
498.. attribute:: HTTPResponse.msg
499
500 A :class:`mimetools.Message` instance containing the response headers.
501
502
503.. attribute:: HTTPResponse.version
504
505 HTTP protocol version used by server. 10 for HTTP/1.0, 11 for HTTP/1.1.
506
507
508.. attribute:: HTTPResponse.status
509
510 Status code returned by server.
511
512
513.. attribute:: HTTPResponse.reason
514
515 Reason phrase returned by server.
516
517
518.. _httplib-examples:
519
520Examples
521--------
522
523Here is an example session that uses the ``GET`` method::
524
525 >>> import httplib
526 >>> conn = httplib.HTTPConnection("www.python.org")
527 >>> conn.request("GET", "/index.html")
528 >>> r1 = conn.getresponse()
529 >>> print r1.status, r1.reason
530 200 OK
531 >>> data1 = r1.read()
532 >>> conn.request("GET", "/parrot.spam")
533 >>> r2 = conn.getresponse()
534 >>> print r2.status, r2.reason
535 404 Not Found
536 >>> data2 = r2.read()
537 >>> conn.close()
538
539Here is an example session that shows how to ``POST`` requests::
540
541 >>> import httplib, urllib
542 >>> params = urllib.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})
543 >>> headers = {"Content-type": "application/x-www-form-urlencoded",
544 ... "Accept": "text/plain"}
545 >>> conn = httplib.HTTPConnection("musi-cal.mojam.com:80")
546 >>> conn.request("POST", "/cgi-bin/query", params, headers)
547 >>> response = conn.getresponse()
548 >>> print response.status, response.reason
549 200 OK
550 >>> data = response.read()
551 >>> conn.close()
552