| Georg Brandl | 2442015 | 2008-05-26 16:32:26 +0000 | [diff] [blame] | 1 | :mod:`http.client` --- HTTP protocol client |
| 2 | =========================================== |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3 | |
| Georg Brandl | 2442015 | 2008-05-26 16:32:26 +0000 | [diff] [blame] | 4 | .. module:: http.client |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 5 | :synopsis: HTTP and HTTPS protocol client (requires sockets). |
| 6 | |
| 7 | |
| 8 | .. index:: |
| 9 | pair: HTTP; protocol |
| Georg Brandl | 2442015 | 2008-05-26 16:32:26 +0000 | [diff] [blame] | 10 | single: HTTP; http.client (standard module) |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 11 | |
| Senthil Kumaran | aca8fd7 | 2008-06-23 04:41:59 +0000 | [diff] [blame] | 12 | .. index:: module: urllib.request |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 13 | |
| Raymond Hettinger | 469271d | 2011-01-27 20:38:46 +0000 | [diff] [blame] | 14 | **Source code:** :source:`Lib/http/client.py` |
| 15 | |
| 16 | -------------- |
| 17 | |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 18 | This module defines classes which implement the client side of the HTTP and |
| Senthil Kumaran | aca8fd7 | 2008-06-23 04:41:59 +0000 | [diff] [blame] | 19 | HTTPS protocols. It is normally not used directly --- the module |
| Georg Brandl | 0f7ede4 | 2008-06-23 11:23:31 +0000 | [diff] [blame] | 20 | :mod:`urllib.request` uses it to handle URLs that use HTTP and HTTPS. |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 21 | |
| 22 | .. note:: |
| 23 | |
| Antoine Pitrou | 1ab19ca | 2010-10-13 10:39:21 +0000 | [diff] [blame] | 24 | HTTPS support is only available if Python was compiled with SSL support |
| 25 | (through the :mod:`ssl` module). |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 26 | |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 27 | The module provides the following classes: |
| 28 | |
| 29 | |
| Senthil Kumaran | 052ddb0 | 2013-03-18 14:11:41 -0700 | [diff] [blame] | 30 | .. class:: HTTPConnection(host, port=None[, timeout], \ |
| Ezio Melotti | e0add76 | 2012-09-14 06:32:35 +0300 | [diff] [blame] | 31 | source_address=None) |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 32 | |
| 33 | An :class:`HTTPConnection` instance represents one transaction with an HTTP |
| Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 34 | server. It should be instantiated passing it a host and optional port |
| 35 | number. If no port number is passed, the port is extracted from the host |
| 36 | string if it has the form ``host:port``, else the default HTTP port (80) is |
| Antoine Pitrou | 988dbd7 | 2010-12-17 17:35:56 +0000 | [diff] [blame] | 37 | used. If the optional *timeout* parameter is given, blocking |
| Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 38 | operations (like connection attempts) will timeout after that many seconds |
| Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 39 | (if it is not given, the global default timeout setting is used). |
| Raymond Hettinger | 519c308 | 2011-01-30 00:39:00 +0000 | [diff] [blame] | 40 | The optional *source_address* parameter may be a tuple of a (host, port) |
| Gregory P. Smith | b406637 | 2010-01-03 03:28:29 +0000 | [diff] [blame] | 41 | to use as the source address the HTTP connection is made from. |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 42 | |
| 43 | For example, the following calls all create instances that connect to the server |
| 44 | at the same host and port:: |
| 45 | |
| Antoine Pitrou | c5e075f | 2014-03-22 18:19:11 +0100 | [diff] [blame] | 46 | >>> h1 = http.client.HTTPConnection('www.python.org') |
| 47 | >>> h2 = http.client.HTTPConnection('www.python.org:80') |
| 48 | >>> h3 = http.client.HTTPConnection('www.python.org', 80) |
| 49 | >>> h4 = http.client.HTTPConnection('www.python.org', 80, timeout=10) |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 50 | |
| Gregory P. Smith | b406637 | 2010-01-03 03:28:29 +0000 | [diff] [blame] | 51 | .. versionchanged:: 3.2 |
| 52 | *source_address* was added. |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 53 | |
| Senthil Kumaran | aced69f | 2013-03-19 01:22:56 -0700 | [diff] [blame] | 54 | .. versionchanged:: 3.4 |
| R David Murray | 0056936 | 2014-01-03 13:04:25 -0500 | [diff] [blame] | 55 | The *strict* parameter was removed. HTTP 0.9-style "Simple Responses" are |
| 56 | not longer supported. |
| Gregory P. Smith | b406637 | 2010-01-03 03:28:29 +0000 | [diff] [blame] | 57 | |
| Antoine Pitrou | 988dbd7 | 2010-12-17 17:35:56 +0000 | [diff] [blame] | 58 | |
| Ezio Melotti | e0add76 | 2012-09-14 06:32:35 +0300 | [diff] [blame] | 59 | .. class:: HTTPSConnection(host, port=None, key_file=None, \ |
| Senthil Kumaran | 052ddb0 | 2013-03-18 14:11:41 -0700 | [diff] [blame] | 60 | cert_file=None[, timeout], \ |
| Ezio Melotti | e0add76 | 2012-09-14 06:32:35 +0300 | [diff] [blame] | 61 | source_address=None, *, context=None, \ |
| 62 | check_hostname=None) |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 63 | |
| 64 | A subclass of :class:`HTTPConnection` that uses SSL for communication with |
| Antoine Pitrou | 803e6d6 | 2010-10-13 10:36:15 +0000 | [diff] [blame] | 65 | secure servers. Default port is ``443``. If *context* is specified, it |
| 66 | must be a :class:`ssl.SSLContext` instance describing the various SSL |
| Antoine Pitrou | c5e075f | 2014-03-22 18:19:11 +0100 | [diff] [blame] | 67 | options. |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 68 | |
| Antoine Pitrou | 803e6d6 | 2010-10-13 10:36:15 +0000 | [diff] [blame] | 69 | *key_file* and *cert_file* are deprecated, please use |
| Antoine Pitrou | c5e075f | 2014-03-22 18:19:11 +0100 | [diff] [blame] | 70 | :meth:`ssl.SSLContext.load_cert_chain` instead, or let |
| 71 | :func:`ssl.create_default_context` select the system's trusted CA |
| Benjamin Peterson | a090f01 | 2014-12-07 13:18:25 -0500 | [diff] [blame] | 72 | certificates for you. The *check_hostname* parameter is also deprecated; the |
| Benjamin Peterson | e3b743c | 2014-12-07 17:26:38 -0500 | [diff] [blame] | 73 | :attr:`ssl.SSLContext.check_hostname` attribute of *context* should be used |
| Benjamin Peterson | a090f01 | 2014-12-07 13:18:25 -0500 | [diff] [blame] | 74 | instead. |
| Antoine Pitrou | 803e6d6 | 2010-10-13 10:36:15 +0000 | [diff] [blame] | 75 | |
| Antoine Pitrou | c5e075f | 2014-03-22 18:19:11 +0100 | [diff] [blame] | 76 | Please read :ref:`ssl-security` for more information on best practices. |
| 77 | |
| Gregory P. Smith | b406637 | 2010-01-03 03:28:29 +0000 | [diff] [blame] | 78 | .. versionchanged:: 3.2 |
| Antoine Pitrou | 803e6d6 | 2010-10-13 10:36:15 +0000 | [diff] [blame] | 79 | *source_address*, *context* and *check_hostname* were added. |
| Gregory P. Smith | b406637 | 2010-01-03 03:28:29 +0000 | [diff] [blame] | 80 | |
| Antoine Pitrou | d532321 | 2010-10-22 18:19:07 +0000 | [diff] [blame] | 81 | .. versionchanged:: 3.2 |
| 82 | This class now supports HTTPS virtual hosts if possible (that is, |
| 83 | if :data:`ssl.HAS_SNI` is true). |
| 84 | |
| Senthil Kumaran | aced69f | 2013-03-19 01:22:56 -0700 | [diff] [blame] | 85 | .. versionchanged:: 3.4 |
| R David Murray | 0056936 | 2014-01-03 13:04:25 -0500 | [diff] [blame] | 86 | The *strict* parameter was removed. HTTP 0.9-style "Simple Responses" are |
| 87 | no longer supported. |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 88 | |
| Benjamin Peterson | 4ffb075 | 2014-11-03 14:29:33 -0500 | [diff] [blame] | 89 | .. versionchanged:: 3.4.3 |
| 90 | This class now performs all the necessary certificate and hostname checks |
| 91 | by default. To revert to the previous, unverified, behavior |
| 92 | :func:`ssl._create_unverified_context` can be passed to the *context* |
| 93 | parameter. |
| 94 | |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 95 | |
| Senthil Kumaran | 052ddb0 | 2013-03-18 14:11:41 -0700 | [diff] [blame] | 96 | .. class:: HTTPResponse(sock, debuglevel=0, method=None, url=None) |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 97 | |
| Jeremy Hylton | 1052f89 | 2009-03-31 14:40:19 +0000 | [diff] [blame] | 98 | Class whose instances are returned upon successful connection. Not |
| 99 | instantiated directly by user. |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 100 | |
| Senthil Kumaran | aced69f | 2013-03-19 01:22:56 -0700 | [diff] [blame] | 101 | .. versionchanged:: 3.4 |
| R David Murray | 0056936 | 2014-01-03 13:04:25 -0500 | [diff] [blame] | 102 | The *strict* parameter was removed. HTTP 0.9 style "Simple Responses" are |
| 103 | no longer supported. |
| Antoine Pitrou | 988dbd7 | 2010-12-17 17:35:56 +0000 | [diff] [blame] | 104 | |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 105 | |
| 106 | The following exceptions are raised as appropriate: |
| 107 | |
| 108 | |
| 109 | .. exception:: HTTPException |
| 110 | |
| 111 | The base class of the other exceptions in this module. It is a subclass of |
| 112 | :exc:`Exception`. |
| 113 | |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 114 | |
| 115 | .. exception:: NotConnected |
| 116 | |
| 117 | A subclass of :exc:`HTTPException`. |
| 118 | |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 119 | |
| 120 | .. exception:: InvalidURL |
| 121 | |
| 122 | A subclass of :exc:`HTTPException`, raised if a port is given and is either |
| 123 | non-numeric or empty. |
| 124 | |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 125 | |
| 126 | .. exception:: UnknownProtocol |
| 127 | |
| 128 | A subclass of :exc:`HTTPException`. |
| 129 | |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 130 | |
| 131 | .. exception:: UnknownTransferEncoding |
| 132 | |
| 133 | A subclass of :exc:`HTTPException`. |
| 134 | |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 135 | |
| 136 | .. exception:: UnimplementedFileMode |
| 137 | |
| 138 | A subclass of :exc:`HTTPException`. |
| 139 | |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 140 | |
| 141 | .. exception:: IncompleteRead |
| 142 | |
| 143 | A subclass of :exc:`HTTPException`. |
| 144 | |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 145 | |
| 146 | .. exception:: ImproperConnectionState |
| 147 | |
| 148 | A subclass of :exc:`HTTPException`. |
| 149 | |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 150 | |
| 151 | .. exception:: CannotSendRequest |
| 152 | |
| 153 | A subclass of :exc:`ImproperConnectionState`. |
| 154 | |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 155 | |
| 156 | .. exception:: CannotSendHeader |
| 157 | |
| 158 | A subclass of :exc:`ImproperConnectionState`. |
| 159 | |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 160 | |
| 161 | .. exception:: ResponseNotReady |
| 162 | |
| 163 | A subclass of :exc:`ImproperConnectionState`. |
| 164 | |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 165 | |
| 166 | .. exception:: BadStatusLine |
| 167 | |
| 168 | A subclass of :exc:`HTTPException`. Raised if a server responds with a HTTP |
| 169 | status code that we don't understand. |
| 170 | |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 171 | |
| Georg Brandl | bf3f8eb | 2013-10-27 07:34:48 +0100 | [diff] [blame] | 172 | The constants defined in this module are: |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 173 | |
| 174 | .. data:: HTTP_PORT |
| 175 | |
| 176 | The default port for the HTTP protocol (always ``80``). |
| 177 | |
| 178 | |
| 179 | .. data:: HTTPS_PORT |
| 180 | |
| 181 | The default port for the HTTPS protocol (always ``443``). |
| 182 | |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 183 | .. data:: responses |
| 184 | |
| 185 | This dictionary maps the HTTP 1.1 status codes to the W3C names. |
| 186 | |
| Georg Brandl | 2442015 | 2008-05-26 16:32:26 +0000 | [diff] [blame] | 187 | Example: ``http.client.responses[http.client.NOT_FOUND]`` is ``'Not Found'``. |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 188 | |
| Berker Peksag | cb18b95 | 2015-01-20 06:30:46 +0200 | [diff] [blame] | 189 | See :ref:`http-status-codes` for a list of HTTP status codes that are |
| 190 | available in this module as constants. |
| 191 | |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 192 | |
| 193 | .. _httpconnection-objects: |
| 194 | |
| 195 | HTTPConnection Objects |
| 196 | ---------------------- |
| 197 | |
| 198 | :class:`HTTPConnection` instances have the following methods: |
| 199 | |
| 200 | |
| Georg Brandl | 036490d | 2009-05-17 13:00:36 +0000 | [diff] [blame] | 201 | .. method:: HTTPConnection.request(method, url, body=None, headers={}) |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 202 | |
| Jeremy Hylton | 236654b | 2009-03-27 20:24:34 +0000 | [diff] [blame] | 203 | This will send a request to the server using the HTTP request |
| 204 | method *method* and the selector *url*. If the *body* argument is |
| 205 | present, it should be string or bytes object of data to send after |
| 206 | the headers are finished. Strings are encoded as ISO-8859-1, the |
| 207 | default charset for HTTP. To use other encodings, pass a bytes |
| 208 | object. The Content-Length header is set to the length of the |
| 209 | string. |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 210 | |
| Antoine Pitrou | 11cb961 | 2010-09-15 11:11:28 +0000 | [diff] [blame] | 211 | The *body* may also be an open :term:`file object`, in which case the |
| Senthil Kumaran | 7bc0d87 | 2010-12-19 10:49:52 +0000 | [diff] [blame] | 212 | contents of the file is sent; this file object should support ``fileno()`` |
| 213 | and ``read()`` methods. The header Content-Length is automatically set to |
| 214 | the length of the file as reported by stat. The *body* argument may also be |
| Raymond Hettinger | 519c308 | 2011-01-30 00:39:00 +0000 | [diff] [blame] | 215 | an iterable and Content-Length header should be explicitly provided when the |
| Senthil Kumaran | 7bc0d87 | 2010-12-19 10:49:52 +0000 | [diff] [blame] | 216 | body is an iterable. |
| Jeremy Hylton | 236654b | 2009-03-27 20:24:34 +0000 | [diff] [blame] | 217 | |
| 218 | The *headers* argument should be a mapping of extra HTTP |
| 219 | headers to send with the request. |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 220 | |
| Senthil Kumaran | 7bc0d87 | 2010-12-19 10:49:52 +0000 | [diff] [blame] | 221 | .. versionadded:: 3.2 |
| Georg Brandl | 09a7df8 | 2010-12-19 12:33:52 +0000 | [diff] [blame] | 222 | *body* can now be an iterable. |
| Senthil Kumaran | 7bc0d87 | 2010-12-19 10:49:52 +0000 | [diff] [blame] | 223 | |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 224 | .. method:: HTTPConnection.getresponse() |
| 225 | |
| 226 | Should be called after a request is sent to get the response from the server. |
| 227 | Returns an :class:`HTTPResponse` instance. |
| 228 | |
| 229 | .. note:: |
| 230 | |
| 231 | Note that you must have read the whole response before you can send a new |
| 232 | request to the server. |
| 233 | |
| 234 | |
| 235 | .. method:: HTTPConnection.set_debuglevel(level) |
| 236 | |
| R. David Murray | d89bc3f | 2010-12-15 02:19:14 +0000 | [diff] [blame] | 237 | Set the debugging level. The default debug level is ``0``, meaning no |
| 238 | debugging output is printed. Any value greater than ``0`` will cause all |
| 239 | currently defined debug output to be printed to stdout. The ``debuglevel`` |
| 240 | is passed to any new :class:`HTTPResponse` objects that are created. |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 241 | |
| Mark Dickinson | 574b1d6 | 2009-10-01 20:20:09 +0000 | [diff] [blame] | 242 | .. versionadded:: 3.1 |
| Benjamin Peterson | fa0d703 | 2009-06-01 22:42:33 +0000 | [diff] [blame] | 243 | |
| Georg Brandl | 67b21b7 | 2010-08-17 15:07:14 +0000 | [diff] [blame] | 244 | |
| Senthil Kumaran | 7e5229c | 2009-12-20 07:31:21 +0000 | [diff] [blame] | 245 | .. method:: HTTPConnection.set_tunnel(host, port=None, headers=None) |
| Senthil Kumaran | 97f0c6b | 2009-07-25 04:24:38 +0000 | [diff] [blame] | 246 | |
| Benjamin Peterson | a48d9ea | 2014-03-16 15:55:39 -0500 | [diff] [blame] | 247 | Set the host and the port for HTTP Connect Tunnelling. This allows running |
| 248 | the connection through a proxy server. |
| Senthil Kumaran | 97f0c6b | 2009-07-25 04:24:38 +0000 | [diff] [blame] | 249 | |
| Benjamin Peterson | a48d9ea | 2014-03-16 15:55:39 -0500 | [diff] [blame] | 250 | The host and port arguments specify the endpoint of the tunneled connection |
| 251 | (i.e. the address included in the CONNECT request, *not* the address of the |
| 252 | proxy server). |
| 253 | |
| 254 | The headers argument should be a mapping of extra HTTP headers to send with |
| 255 | the CONNECT request. |
| 256 | |
| 257 | For example, to tunnel through a HTTPS proxy server running locally on port |
| 258 | 8080, we would pass the address of the proxy to the :class:`HTTPSConnection` |
| 259 | constructor, and the address of the host that we eventually want to reach to |
| 260 | the :meth:`~HTTPConnection.set_tunnel` method:: |
| 261 | |
| 262 | >>> import http.client |
| 263 | >>> conn = http.client.HTTPSConnection("localhost", 8080) |
| 264 | >>> conn.set_tunnel("www.python.org") |
| 265 | >>> conn.request("HEAD","/index.html") |
| Senthil Kumaran | 7e5229c | 2009-12-20 07:31:21 +0000 | [diff] [blame] | 266 | |
| Senthil Kumaran | 2e910fd | 2009-07-25 04:27:38 +0000 | [diff] [blame] | 267 | .. versionadded:: 3.2 |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 268 | |
| Georg Brandl | 67b21b7 | 2010-08-17 15:07:14 +0000 | [diff] [blame] | 269 | |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 270 | .. method:: HTTPConnection.connect() |
| 271 | |
| 272 | Connect to the server specified when the object was created. |
| 273 | |
| 274 | |
| 275 | .. method:: HTTPConnection.close() |
| 276 | |
| 277 | Close the connection to the server. |
| 278 | |
| 279 | As an alternative to using the :meth:`request` method described above, you can |
| 280 | also send your request step by step, by using the four functions below. |
| 281 | |
| 282 | |
| Georg Brandl | 036490d | 2009-05-17 13:00:36 +0000 | [diff] [blame] | 283 | .. method:: HTTPConnection.putrequest(request, selector, skip_host=False, skip_accept_encoding=False) |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 284 | |
| 285 | This should be the first call after the connection to the server has been made. |
| 286 | It sends a line to the server consisting of the *request* string, the *selector* |
| 287 | string, and the HTTP version (``HTTP/1.1``). To disable automatic sending of |
| 288 | ``Host:`` or ``Accept-Encoding:`` headers (for example to accept additional |
| 289 | content encodings), specify *skip_host* or *skip_accept_encoding* with non-False |
| 290 | values. |
| 291 | |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 292 | |
| 293 | .. method:: HTTPConnection.putheader(header, argument[, ...]) |
| 294 | |
| 295 | Send an :rfc:`822`\ -style header to the server. It sends a line to the server |
| 296 | consisting of the header, a colon and a space, and the first argument. If more |
| 297 | arguments are given, continuation lines are sent, each consisting of a tab and |
| 298 | an argument. |
| 299 | |
| 300 | |
| Senthil Kumaran | 5d0de3f | 2011-10-03 07:27:06 +0800 | [diff] [blame] | 301 | .. method:: HTTPConnection.endheaders(message_body=None) |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 302 | |
| Senthil Kumaran | 5d0de3f | 2011-10-03 07:27:06 +0800 | [diff] [blame] | 303 | Send a blank line to the server, signalling the end of the headers. The |
| Senthil Kumaran | ad87fa6 | 2011-10-05 23:26:49 +0800 | [diff] [blame] | 304 | optional *message_body* argument can be used to pass a message body |
| 305 | associated with the request. The message body will be sent in the same |
| 306 | packet as the message headers if it is string, otherwise it is sent in a |
| 307 | separate packet. |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 308 | |
| 309 | .. method:: HTTPConnection.send(data) |
| 310 | |
| 311 | Send data to the server. This should be used directly only after the |
| 312 | :meth:`endheaders` method has been called and before :meth:`getresponse` is |
| 313 | called. |
| 314 | |
| 315 | |
| 316 | .. _httpresponse-objects: |
| 317 | |
| 318 | HTTPResponse Objects |
| 319 | -------------------- |
| 320 | |
| Jeremy Hylton | 1052f89 | 2009-03-31 14:40:19 +0000 | [diff] [blame] | 321 | An :class:`HTTPResponse` instance wraps the HTTP response from the |
| 322 | server. It provides access to the request headers and the entity |
| 323 | body. The response is an iterable object and can be used in a with |
| 324 | statement. |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 325 | |
| 326 | |
| 327 | .. method:: HTTPResponse.read([amt]) |
| 328 | |
| 329 | Reads and returns the response body, or up to the next *amt* bytes. |
| 330 | |
| Antoine Pitrou | 38d9643 | 2011-12-06 22:33:57 +0100 | [diff] [blame] | 331 | .. method:: HTTPResponse.readinto(b) |
| 332 | |
| 333 | Reads up to the next len(b) bytes of the response body into the buffer *b*. |
| 334 | Returns the number of bytes read. |
| 335 | |
| 336 | .. versionadded:: 3.3 |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 337 | |
| Georg Brandl | 036490d | 2009-05-17 13:00:36 +0000 | [diff] [blame] | 338 | .. method:: HTTPResponse.getheader(name, default=None) |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 339 | |
| Senthil Kumaran | 790f831 | 2010-08-02 17:09:02 +0000 | [diff] [blame] | 340 | Return the value of the header *name*, or *default* if there is no header |
| 341 | matching *name*. If there is more than one header with the name *name*, |
| 342 | return all of the values joined by ', '. If 'default' is any iterable other |
| 343 | than a single string, its elements are similarly returned joined by commas. |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 344 | |
| 345 | |
| 346 | .. method:: HTTPResponse.getheaders() |
| 347 | |
| 348 | Return a list of (header, value) tuples. |
| 349 | |
| Senthil Kumaran | ceff566 | 2010-09-21 01:57:43 +0000 | [diff] [blame] | 350 | .. method:: HTTPResponse.fileno() |
| 351 | |
| 352 | Return the ``fileno`` of the underlying socket. |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 353 | |
| 354 | .. attribute:: HTTPResponse.msg |
| 355 | |
| Jeremy Hylton | 1052f89 | 2009-03-31 14:40:19 +0000 | [diff] [blame] | 356 | A :class:`http.client.HTTPMessage` instance containing the response |
| 357 | headers. :class:`http.client.HTTPMessage` is a subclass of |
| 358 | :class:`email.message.Message`. |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 359 | |
| 360 | |
| 361 | .. attribute:: HTTPResponse.version |
| 362 | |
| 363 | HTTP protocol version used by server. 10 for HTTP/1.0, 11 for HTTP/1.1. |
| 364 | |
| 365 | |
| 366 | .. attribute:: HTTPResponse.status |
| 367 | |
| 368 | Status code returned by server. |
| 369 | |
| 370 | |
| 371 | .. attribute:: HTTPResponse.reason |
| 372 | |
| 373 | Reason phrase returned by server. |
| 374 | |
| 375 | |
| Jeremy Hylton | 1052f89 | 2009-03-31 14:40:19 +0000 | [diff] [blame] | 376 | .. attribute:: HTTPResponse.debuglevel |
| 377 | |
| Georg Brandl | ef871f6 | 2010-03-12 10:06:40 +0000 | [diff] [blame] | 378 | A debugging hook. If :attr:`debuglevel` is greater than zero, messages |
| Jeremy Hylton | 1052f89 | 2009-03-31 14:40:19 +0000 | [diff] [blame] | 379 | will be printed to stdout as the response is read and parsed. |
| 380 | |
| Senthil Kumaran | ce9b596 | 2011-06-19 16:56:49 -0700 | [diff] [blame] | 381 | .. attribute:: HTTPResponse.closed |
| 382 | |
| Serhiy Storchaka | fbc1c26 | 2013-11-29 12:17:13 +0200 | [diff] [blame] | 383 | Is ``True`` if the stream is closed. |
| Jeremy Hylton | 1052f89 | 2009-03-31 14:40:19 +0000 | [diff] [blame] | 384 | |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 385 | Examples |
| 386 | -------- |
| 387 | |
| 388 | Here is an example session that uses the ``GET`` method:: |
| 389 | |
| Georg Brandl | 2442015 | 2008-05-26 16:32:26 +0000 | [diff] [blame] | 390 | >>> import http.client |
| 391 | >>> conn = http.client.HTTPConnection("www.python.org") |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 392 | >>> conn.request("GET", "/index.html") |
| 393 | >>> r1 = conn.getresponse() |
| Georg Brandl | 6911e3c | 2007-09-04 07:15:32 +0000 | [diff] [blame] | 394 | >>> print(r1.status, r1.reason) |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 395 | 200 OK |
| Senthil Kumaran | ce9b596 | 2011-06-19 16:56:49 -0700 | [diff] [blame] | 396 | >>> data1 = r1.read() # This will return entire content. |
| 397 | >>> # The following example demonstrates reading data in chunks. |
| 398 | >>> conn.request("GET", "/index.html") |
| 399 | >>> r1 = conn.getresponse() |
| 400 | >>> while not r1.closed: |
| 401 | ... print(r1.read(200)) # 200 bytes |
| 402 | b'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"... |
| 403 | ... |
| 404 | >>> # Example of an invalid request |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 405 | >>> conn.request("GET", "/parrot.spam") |
| 406 | >>> r2 = conn.getresponse() |
| Georg Brandl | 6911e3c | 2007-09-04 07:15:32 +0000 | [diff] [blame] | 407 | >>> print(r2.status, r2.reason) |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 408 | 404 Not Found |
| 409 | >>> data2 = r2.read() |
| 410 | >>> conn.close() |
| 411 | |
| Fred Drake | 1587e3d | 2010-05-12 01:36:11 +0000 | [diff] [blame] | 412 | Here is an example session that uses the ``HEAD`` method. Note that the |
| 413 | ``HEAD`` method never returns any data. :: |
| Senthil Kumaran | 71fb6c8 | 2010-04-28 17:39:48 +0000 | [diff] [blame] | 414 | |
| 415 | >>> import http.client |
| 416 | >>> conn = http.client.HTTPConnection("www.python.org") |
| 417 | >>> conn.request("HEAD","/index.html") |
| 418 | >>> res = conn.getresponse() |
| 419 | >>> print(res.status, res.reason) |
| 420 | 200 OK |
| 421 | >>> data = res.read() |
| 422 | >>> print(len(data)) |
| 423 | 0 |
| 424 | >>> data == b'' |
| 425 | True |
| 426 | |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 427 | Here is an example session that shows how to ``POST`` requests:: |
| 428 | |
| Senthil Kumaran | aca8fd7 | 2008-06-23 04:41:59 +0000 | [diff] [blame] | 429 | >>> import http.client, urllib.parse |
| Senthil Kumaran | 96c84a4 | 2011-07-20 21:56:24 +0800 | [diff] [blame] | 430 | >>> params = urllib.parse.urlencode({'@number': 12524, '@type': 'issue', '@action': 'show'}) |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 431 | >>> headers = {"Content-type": "application/x-www-form-urlencoded", |
| 432 | ... "Accept": "text/plain"} |
| Senthil Kumaran | 96c84a4 | 2011-07-20 21:56:24 +0800 | [diff] [blame] | 433 | >>> conn = http.client.HTTPConnection("bugs.python.org") |
| 434 | >>> conn.request("POST", "", params, headers) |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 435 | >>> response = conn.getresponse() |
| Georg Brandl | 6911e3c | 2007-09-04 07:15:32 +0000 | [diff] [blame] | 436 | >>> print(response.status, response.reason) |
| Senthil Kumaran | 96c84a4 | 2011-07-20 21:56:24 +0800 | [diff] [blame] | 437 | 302 Found |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 438 | >>> data = response.read() |
| Senthil Kumaran | 96c84a4 | 2011-07-20 21:56:24 +0800 | [diff] [blame] | 439 | >>> data |
| 440 | b'Redirecting to <a href="http://bugs.python.org/issue12524">http://bugs.python.org/issue12524</a>' |
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 441 | >>> conn.close() |
| 442 | |
| Senthil Kumaran | b5fe247 | 2013-03-13 13:38:33 -0700 | [diff] [blame] | 443 | Client side ``HTTP PUT`` requests are very similar to ``POST`` requests. The |
| 444 | difference lies only the server side where HTTP server will allow resources to |
| Senthil Kumaran | e66cc81 | 2013-03-13 13:42:47 -0700 | [diff] [blame] | 445 | be created via ``PUT`` request. It should be noted that custom HTTP methods |
| 446 | +are also handled in :class:`urllib.request.Request` by sending the appropriate |
| 447 | +method attribute.Here is an example session that shows how to do ``PUT`` |
| 448 | request using http.client:: |
| Senthil Kumaran | b5fe247 | 2013-03-13 13:38:33 -0700 | [diff] [blame] | 449 | |
| 450 | >>> # This creates an HTTP message |
| 451 | >>> # with the content of BODY as the enclosed representation |
| Senthil Kumaran | 8b4a272 | 2014-04-16 23:33:02 -0400 | [diff] [blame] | 452 | >>> # for the resource http://localhost:8080/file |
| Senthil Kumaran | b5fe247 | 2013-03-13 13:38:33 -0700 | [diff] [blame] | 453 | ... |
| 454 | >>> import http.client |
| 455 | >>> BODY = "***filecontents***" |
| 456 | >>> conn = http.client.HTTPConnection("localhost", 8080) |
| 457 | >>> conn.request("PUT", "/file", BODY) |
| 458 | >>> response = conn.getresponse() |
| Georg Brandl | d277a56 | 2013-10-06 12:42:18 +0200 | [diff] [blame] | 459 | >>> print(response.status, response.reason) |
| Senthil Kumaran | b5fe247 | 2013-03-13 13:38:33 -0700 | [diff] [blame] | 460 | 200, OK |
| Jeremy Hylton | 1052f89 | 2009-03-31 14:40:19 +0000 | [diff] [blame] | 461 | |
| 462 | .. _httpmessage-objects: |
| 463 | |
| 464 | HTTPMessage Objects |
| 465 | ------------------- |
| 466 | |
| Benjamin Peterson | 605b9d9 | 2009-04-02 00:24:00 +0000 | [diff] [blame] | 467 | An :class:`http.client.HTTPMessage` instance holds the headers from an HTTP |
| 468 | response. It is implemented using the :class:`email.message.Message` class. |
| Jeremy Hylton | 1052f89 | 2009-03-31 14:40:19 +0000 | [diff] [blame] | 469 | |
| Benjamin Peterson | 605b9d9 | 2009-04-02 00:24:00 +0000 | [diff] [blame] | 470 | .. XXX Define the methods that clients can depend upon between versions. |