/Doc/library/httplib.rst

http://unladen-swallow.googlecode.com/ · ReStructuredText · 557 lines · 395 code · 162 blank · 0 comment · 0 complexity · a1c71a35294908e4749dd45767b5e82a MD5 · raw file

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