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