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 | |
Terry Jan Reedy | fa089b9 | 2016-06-11 15:02:54 -0400 | [diff] [blame] | 7 | **Source code:** :source:`Lib/http/client.py` |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 8 | |
| 9 | .. index:: |
| 10 | pair: HTTP; protocol |
Georg Brandl | 2442015 | 2008-05-26 16:32:26 +0000 | [diff] [blame] | 11 | single: HTTP; http.client (standard module) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 12 | |
Senthil Kumaran | aca8fd7 | 2008-06-23 04:41:59 +0000 | [diff] [blame] | 13 | .. index:: module: urllib.request |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 14 | |
Raymond Hettinger | 469271d | 2011-01-27 20:38:46 +0000 | [diff] [blame] | 15 | -------------- |
| 16 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 17 | 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] | 18 | HTTPS protocols. It is normally not used directly --- the module |
Georg Brandl | 0f7ede4 | 2008-06-23 11:23:31 +0000 | [diff] [blame] | 19 | :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] | 20 | |
Benjamin Peterson | 6de708f | 2015-04-20 18:18:14 -0400 | [diff] [blame] | 21 | .. seealso:: |
| 22 | |
Andrew Kuchling | 58c534d | 2016-11-08 22:33:31 -0500 | [diff] [blame] | 23 | The `Requests package <http://docs.python-requests.org/>`_ |
Martin Panter | fe289c0 | 2016-05-28 02:20:39 +0000 | [diff] [blame] | 24 | is recommended for a higher-level HTTP client interface. |
Benjamin Peterson | 6de708f | 2015-04-20 18:18:14 -0400 | [diff] [blame] | 25 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 26 | .. note:: |
| 27 | |
Antoine Pitrou | 1ab19ca | 2010-10-13 10:39:21 +0000 | [diff] [blame] | 28 | HTTPS support is only available if Python was compiled with SSL support |
| 29 | (through the :mod:`ssl` module). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 30 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 31 | The module provides the following classes: |
| 32 | |
| 33 | |
Nir Soffer | ad455cd | 2017-11-06 23:16:37 +0200 | [diff] [blame] | 34 | .. class:: HTTPConnection(host, port=None[, timeout], source_address=None, \ |
| 35 | blocksize=8192) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 36 | |
| 37 | An :class:`HTTPConnection` instance represents one transaction with an HTTP |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 38 | server. It should be instantiated passing it a host and optional port |
| 39 | number. If no port number is passed, the port is extracted from the host |
| 40 | 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] | 41 | used. If the optional *timeout* parameter is given, blocking |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 42 | operations (like connection attempts) will timeout after that many seconds |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 43 | (if it is not given, the global default timeout setting is used). |
Raymond Hettinger | 519c308 | 2011-01-30 00:39:00 +0000 | [diff] [blame] | 44 | 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] | 45 | to use as the source address the HTTP connection is made from. |
Nir Soffer | ad455cd | 2017-11-06 23:16:37 +0200 | [diff] [blame] | 46 | The optional *blocksize* parameter sets the buffer size in bytes for |
| 47 | sending a file-like message body. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 48 | |
| 49 | For example, the following calls all create instances that connect to the server |
| 50 | at the same host and port:: |
| 51 | |
Antoine Pitrou | c5e075f | 2014-03-22 18:19:11 +0100 | [diff] [blame] | 52 | >>> h1 = http.client.HTTPConnection('www.python.org') |
| 53 | >>> h2 = http.client.HTTPConnection('www.python.org:80') |
| 54 | >>> h3 = http.client.HTTPConnection('www.python.org', 80) |
| 55 | >>> h4 = http.client.HTTPConnection('www.python.org', 80, timeout=10) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 56 | |
Gregory P. Smith | b406637 | 2010-01-03 03:28:29 +0000 | [diff] [blame] | 57 | .. versionchanged:: 3.2 |
| 58 | *source_address* was added. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 59 | |
Senthil Kumaran | aced69f | 2013-03-19 01:22:56 -0700 | [diff] [blame] | 60 | .. versionchanged:: 3.4 |
R David Murray | 0056936 | 2014-01-03 13:04:25 -0500 | [diff] [blame] | 61 | The *strict* parameter was removed. HTTP 0.9-style "Simple Responses" are |
| 62 | not longer supported. |
Gregory P. Smith | b406637 | 2010-01-03 03:28:29 +0000 | [diff] [blame] | 63 | |
Nir Soffer | ad455cd | 2017-11-06 23:16:37 +0200 | [diff] [blame] | 64 | .. versionchanged:: 3.7 |
| 65 | *blocksize* parameter was added. |
| 66 | |
Antoine Pitrou | 988dbd7 | 2010-12-17 17:35:56 +0000 | [diff] [blame] | 67 | |
Ezio Melotti | e0add76 | 2012-09-14 06:32:35 +0300 | [diff] [blame] | 68 | .. class:: HTTPSConnection(host, port=None, key_file=None, \ |
Senthil Kumaran | 052ddb0 | 2013-03-18 14:11:41 -0700 | [diff] [blame] | 69 | cert_file=None[, timeout], \ |
Ezio Melotti | e0add76 | 2012-09-14 06:32:35 +0300 | [diff] [blame] | 70 | source_address=None, *, context=None, \ |
Nir Soffer | ad455cd | 2017-11-06 23:16:37 +0200 | [diff] [blame] | 71 | check_hostname=None, blocksize=8192) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 72 | |
| 73 | A subclass of :class:`HTTPConnection` that uses SSL for communication with |
Antoine Pitrou | 803e6d6 | 2010-10-13 10:36:15 +0000 | [diff] [blame] | 74 | secure servers. Default port is ``443``. If *context* is specified, it |
| 75 | must be a :class:`ssl.SSLContext` instance describing the various SSL |
Antoine Pitrou | c5e075f | 2014-03-22 18:19:11 +0100 | [diff] [blame] | 76 | options. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 77 | |
Antoine Pitrou | c5e075f | 2014-03-22 18:19:11 +0100 | [diff] [blame] | 78 | Please read :ref:`ssl-security` for more information on best practices. |
| 79 | |
Gregory P. Smith | b406637 | 2010-01-03 03:28:29 +0000 | [diff] [blame] | 80 | .. versionchanged:: 3.2 |
Antoine Pitrou | 803e6d6 | 2010-10-13 10:36:15 +0000 | [diff] [blame] | 81 | *source_address*, *context* and *check_hostname* were added. |
Gregory P. Smith | b406637 | 2010-01-03 03:28:29 +0000 | [diff] [blame] | 82 | |
Antoine Pitrou | d532321 | 2010-10-22 18:19:07 +0000 | [diff] [blame] | 83 | .. versionchanged:: 3.2 |
| 84 | This class now supports HTTPS virtual hosts if possible (that is, |
| 85 | if :data:`ssl.HAS_SNI` is true). |
| 86 | |
Senthil Kumaran | aced69f | 2013-03-19 01:22:56 -0700 | [diff] [blame] | 87 | .. versionchanged:: 3.4 |
R David Murray | 0056936 | 2014-01-03 13:04:25 -0500 | [diff] [blame] | 88 | The *strict* parameter was removed. HTTP 0.9-style "Simple Responses" are |
| 89 | no longer supported. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 90 | |
Benjamin Peterson | 4ffb075 | 2014-11-03 14:29:33 -0500 | [diff] [blame] | 91 | .. versionchanged:: 3.4.3 |
| 92 | This class now performs all the necessary certificate and hostname checks |
| 93 | by default. To revert to the previous, unverified, behavior |
| 94 | :func:`ssl._create_unverified_context` can be passed to the *context* |
| 95 | parameter. |
| 96 | |
Christian Heimes | d048637 | 2016-09-10 23:23:33 +0200 | [diff] [blame] | 97 | .. deprecated:: 3.6 |
| 98 | |
| 99 | *key_file* and *cert_file* are deprecated in favor of *context*. |
| 100 | Please use :meth:`ssl.SSLContext.load_cert_chain` instead, or let |
| 101 | :func:`ssl.create_default_context` select the system's trusted CA |
| 102 | certificates for you. |
| 103 | |
| 104 | The *check_hostname* parameter is also deprecated; the |
| 105 | :attr:`ssl.SSLContext.check_hostname` attribute of *context* should |
| 106 | be used instead. |
| 107 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 108 | |
Senthil Kumaran | 052ddb0 | 2013-03-18 14:11:41 -0700 | [diff] [blame] | 109 | .. class:: HTTPResponse(sock, debuglevel=0, method=None, url=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 110 | |
Jeremy Hylton | 1052f89 | 2009-03-31 14:40:19 +0000 | [diff] [blame] | 111 | Class whose instances are returned upon successful connection. Not |
| 112 | instantiated directly by user. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 113 | |
Senthil Kumaran | aced69f | 2013-03-19 01:22:56 -0700 | [diff] [blame] | 114 | .. versionchanged:: 3.4 |
R David Murray | 0056936 | 2014-01-03 13:04:25 -0500 | [diff] [blame] | 115 | The *strict* parameter was removed. HTTP 0.9 style "Simple Responses" are |
| 116 | no longer supported. |
Antoine Pitrou | 988dbd7 | 2010-12-17 17:35:56 +0000 | [diff] [blame] | 117 | |
Ashwin Ramaswami | 478f829 | 2019-01-18 07:49:16 -0800 | [diff] [blame^] | 118 | This module provides the following function: |
| 119 | |
| 120 | .. function:: parse_headers(fp) |
| 121 | |
| 122 | Parse the headers from a file pointer *fp* representing a HTTP |
| 123 | request/response. The file has to be a :class:`BufferedIOBase` reader |
| 124 | (i.e. not text) and must provide a valid :rfc:`2822` style header. |
| 125 | |
| 126 | This function returns an instance of :class:`http.client.HTTPMessage` |
| 127 | that holds the header fields, but no payload |
| 128 | (the same as :attr:`HTTPResponse.msg` |
| 129 | and :attr:`http.server.BaseHTTPRequestHandler.headers`). |
| 130 | After returning, the file pointer *fp* is ready to read the HTTP body. |
| 131 | |
| 132 | .. note:: |
| 133 | :meth:`parse_headers` does not parse the start-line of a HTTP message; |
| 134 | it only parses the ``Name: value`` lines. The file has to be ready to |
| 135 | read these field lines, so the first line should already be consumed |
| 136 | before calling the function. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 137 | |
| 138 | The following exceptions are raised as appropriate: |
| 139 | |
| 140 | |
| 141 | .. exception:: HTTPException |
| 142 | |
| 143 | The base class of the other exceptions in this module. It is a subclass of |
| 144 | :exc:`Exception`. |
| 145 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 146 | |
| 147 | .. exception:: NotConnected |
| 148 | |
| 149 | A subclass of :exc:`HTTPException`. |
| 150 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 151 | |
| 152 | .. exception:: InvalidURL |
| 153 | |
| 154 | A subclass of :exc:`HTTPException`, raised if a port is given and is either |
| 155 | non-numeric or empty. |
| 156 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 157 | |
| 158 | .. exception:: UnknownProtocol |
| 159 | |
| 160 | A subclass of :exc:`HTTPException`. |
| 161 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 162 | |
| 163 | .. exception:: UnknownTransferEncoding |
| 164 | |
| 165 | A subclass of :exc:`HTTPException`. |
| 166 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 167 | |
| 168 | .. exception:: UnimplementedFileMode |
| 169 | |
| 170 | A subclass of :exc:`HTTPException`. |
| 171 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 172 | |
| 173 | .. exception:: IncompleteRead |
| 174 | |
| 175 | A subclass of :exc:`HTTPException`. |
| 176 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 177 | |
| 178 | .. exception:: ImproperConnectionState |
| 179 | |
| 180 | A subclass of :exc:`HTTPException`. |
| 181 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 182 | |
| 183 | .. exception:: CannotSendRequest |
| 184 | |
| 185 | A subclass of :exc:`ImproperConnectionState`. |
| 186 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 187 | |
| 188 | .. exception:: CannotSendHeader |
| 189 | |
| 190 | A subclass of :exc:`ImproperConnectionState`. |
| 191 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 192 | |
| 193 | .. exception:: ResponseNotReady |
| 194 | |
| 195 | A subclass of :exc:`ImproperConnectionState`. |
| 196 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 197 | |
| 198 | .. exception:: BadStatusLine |
| 199 | |
| 200 | A subclass of :exc:`HTTPException`. Raised if a server responds with a HTTP |
| 201 | status code that we don't understand. |
| 202 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 203 | |
Berker Peksag | babc688 | 2015-02-20 09:39:38 +0200 | [diff] [blame] | 204 | .. exception:: LineTooLong |
| 205 | |
| 206 | A subclass of :exc:`HTTPException`. Raised if an excessively long line |
| 207 | is received in the HTTP protocol from the server. |
| 208 | |
| 209 | |
R David Murray | cae7bdb | 2015-04-05 19:26:29 -0400 | [diff] [blame] | 210 | .. exception:: RemoteDisconnected |
| 211 | |
| 212 | A subclass of :exc:`ConnectionResetError` and :exc:`BadStatusLine`. Raised |
| 213 | by :meth:`HTTPConnection.getresponse` when the attempt to read the response |
| 214 | results in no data read from the connection, indicating that the remote end |
| 215 | has closed the connection. |
| 216 | |
| 217 | .. versionadded:: 3.5 |
| 218 | Previously, :exc:`BadStatusLine`\ ``('')`` was raised. |
| 219 | |
| 220 | |
Georg Brandl | bf3f8eb | 2013-10-27 07:34:48 +0100 | [diff] [blame] | 221 | The constants defined in this module are: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 222 | |
| 223 | .. data:: HTTP_PORT |
| 224 | |
| 225 | The default port for the HTTP protocol (always ``80``). |
| 226 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 227 | .. data:: HTTPS_PORT |
| 228 | |
| 229 | The default port for the HTTPS protocol (always ``443``). |
| 230 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 231 | .. data:: responses |
| 232 | |
| 233 | This dictionary maps the HTTP 1.1 status codes to the W3C names. |
| 234 | |
Georg Brandl | 2442015 | 2008-05-26 16:32:26 +0000 | [diff] [blame] | 235 | Example: ``http.client.responses[http.client.NOT_FOUND]`` is ``'Not Found'``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 236 | |
Berker Peksag | cb18b95 | 2015-01-20 06:30:46 +0200 | [diff] [blame] | 237 | See :ref:`http-status-codes` for a list of HTTP status codes that are |
| 238 | available in this module as constants. |
| 239 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 240 | |
| 241 | .. _httpconnection-objects: |
| 242 | |
| 243 | HTTPConnection Objects |
| 244 | ---------------------- |
| 245 | |
| 246 | :class:`HTTPConnection` instances have the following methods: |
| 247 | |
| 248 | |
Martin Panter | 3c0d0ba | 2016-08-24 06:33:33 +0000 | [diff] [blame] | 249 | .. method:: HTTPConnection.request(method, url, body=None, headers={}, *, \ |
| 250 | encode_chunked=False) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 251 | |
Jeremy Hylton | 236654b | 2009-03-27 20:24:34 +0000 | [diff] [blame] | 252 | This will send a request to the server using the HTTP request |
R David Murray | beed840 | 2015-03-22 15:18:23 -0400 | [diff] [blame] | 253 | method *method* and the selector *url*. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 254 | |
R David Murray | beed840 | 2015-03-22 15:18:23 -0400 | [diff] [blame] | 255 | If *body* is specified, the specified data is sent after the headers are |
Martin Panter | 3c0d0ba | 2016-08-24 06:33:33 +0000 | [diff] [blame] | 256 | finished. It may be a :class:`str`, a :term:`bytes-like object`, an |
| 257 | open :term:`file object`, or an iterable of :class:`bytes`. If *body* |
| 258 | is a string, it is encoded as ISO-8859-1, the default for HTTP. If it |
| 259 | is a bytes-like object, the bytes are sent as is. If it is a :term:`file |
| 260 | object`, the contents of the file is sent; this file object should |
| 261 | support at least the ``read()`` method. If the file object is an |
| 262 | instance of :class:`io.TextIOBase`, the data returned by the ``read()`` |
| 263 | method will be encoded as ISO-8859-1, otherwise the data returned by |
| 264 | ``read()`` is sent as is. If *body* is an iterable, the elements of the |
| 265 | iterable are sent as is until the iterable is exhausted. |
Jeremy Hylton | 236654b | 2009-03-27 20:24:34 +0000 | [diff] [blame] | 266 | |
Martin Panter | 3c0d0ba | 2016-08-24 06:33:33 +0000 | [diff] [blame] | 267 | The *headers* argument should be a mapping of extra HTTP headers to send |
| 268 | with the request. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 269 | |
Martin Panter | ef91bb2 | 2016-08-27 01:39:26 +0000 | [diff] [blame] | 270 | If *headers* contains neither Content-Length nor Transfer-Encoding, |
| 271 | but there is a request body, one of those |
| 272 | header fields will be added automatically. If |
Martin Panter | 3c0d0ba | 2016-08-24 06:33:33 +0000 | [diff] [blame] | 273 | *body* is ``None``, the Content-Length header is set to ``0`` for |
| 274 | methods that expect a body (``PUT``, ``POST``, and ``PATCH``). If |
Martin Panter | ef91bb2 | 2016-08-27 01:39:26 +0000 | [diff] [blame] | 275 | *body* is a string or a bytes-like object that is not also a |
| 276 | :term:`file <file object>`, the Content-Length header is |
| 277 | set to its length. Any other type of *body* (files |
| 278 | and iterables in general) will be chunk-encoded, and the |
| 279 | Transfer-Encoding header will automatically be set instead of |
| 280 | Content-Length. |
Martin Panter | 3c0d0ba | 2016-08-24 06:33:33 +0000 | [diff] [blame] | 281 | |
| 282 | The *encode_chunked* argument is only relevant if Transfer-Encoding is |
| 283 | specified in *headers*. If *encode_chunked* is ``False``, the |
| 284 | HTTPConnection object assumes that all encoding is handled by the |
| 285 | calling code. If it is ``True``, the body will be chunk-encoded. |
| 286 | |
| 287 | .. note:: |
| 288 | Chunked transfer encoding has been added to the HTTP protocol |
| 289 | version 1.1. Unless the HTTP server is known to handle HTTP 1.1, |
Martin Panter | ef91bb2 | 2016-08-27 01:39:26 +0000 | [diff] [blame] | 290 | the caller must either specify the Content-Length, or must pass a |
| 291 | :class:`str` or bytes-like object that is not also a file as the |
| 292 | body representation. |
R David Murray | beed840 | 2015-03-22 15:18:23 -0400 | [diff] [blame] | 293 | |
Senthil Kumaran | 7bc0d87 | 2010-12-19 10:49:52 +0000 | [diff] [blame] | 294 | .. versionadded:: 3.2 |
Georg Brandl | 09a7df8 | 2010-12-19 12:33:52 +0000 | [diff] [blame] | 295 | *body* can now be an iterable. |
Senthil Kumaran | 7bc0d87 | 2010-12-19 10:49:52 +0000 | [diff] [blame] | 296 | |
Martin Panter | 3c0d0ba | 2016-08-24 06:33:33 +0000 | [diff] [blame] | 297 | .. versionchanged:: 3.6 |
| 298 | If neither Content-Length nor Transfer-Encoding are set in |
Martin Panter | ef91bb2 | 2016-08-27 01:39:26 +0000 | [diff] [blame] | 299 | *headers*, file and iterable *body* objects are now chunk-encoded. |
| 300 | The *encode_chunked* argument was added. |
| 301 | No attempt is made to determine the Content-Length for file |
Martin Panter | 3c0d0ba | 2016-08-24 06:33:33 +0000 | [diff] [blame] | 302 | objects. |
| 303 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 304 | .. method:: HTTPConnection.getresponse() |
| 305 | |
| 306 | Should be called after a request is sent to get the response from the server. |
| 307 | Returns an :class:`HTTPResponse` instance. |
| 308 | |
| 309 | .. note:: |
| 310 | |
| 311 | Note that you must have read the whole response before you can send a new |
| 312 | request to the server. |
| 313 | |
R David Murray | cae7bdb | 2015-04-05 19:26:29 -0400 | [diff] [blame] | 314 | .. versionchanged:: 3.5 |
| 315 | If a :exc:`ConnectionError` or subclass is raised, the |
| 316 | :class:`HTTPConnection` object will be ready to reconnect when |
| 317 | a new request is sent. |
| 318 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 319 | |
| 320 | .. method:: HTTPConnection.set_debuglevel(level) |
| 321 | |
R. David Murray | d89bc3f | 2010-12-15 02:19:14 +0000 | [diff] [blame] | 322 | Set the debugging level. The default debug level is ``0``, meaning no |
| 323 | debugging output is printed. Any value greater than ``0`` will cause all |
| 324 | currently defined debug output to be printed to stdout. The ``debuglevel`` |
| 325 | is passed to any new :class:`HTTPResponse` objects that are created. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 326 | |
Mark Dickinson | 574b1d6 | 2009-10-01 20:20:09 +0000 | [diff] [blame] | 327 | .. versionadded:: 3.1 |
Benjamin Peterson | fa0d703 | 2009-06-01 22:42:33 +0000 | [diff] [blame] | 328 | |
Georg Brandl | 67b21b7 | 2010-08-17 15:07:14 +0000 | [diff] [blame] | 329 | |
Senthil Kumaran | 7e5229c | 2009-12-20 07:31:21 +0000 | [diff] [blame] | 330 | .. method:: HTTPConnection.set_tunnel(host, port=None, headers=None) |
Senthil Kumaran | 97f0c6b | 2009-07-25 04:24:38 +0000 | [diff] [blame] | 331 | |
Benjamin Peterson | a48d9ea | 2014-03-16 15:55:39 -0500 | [diff] [blame] | 332 | Set the host and the port for HTTP Connect Tunnelling. This allows running |
| 333 | the connection through a proxy server. |
Senthil Kumaran | 97f0c6b | 2009-07-25 04:24:38 +0000 | [diff] [blame] | 334 | |
Benjamin Peterson | a48d9ea | 2014-03-16 15:55:39 -0500 | [diff] [blame] | 335 | The host and port arguments specify the endpoint of the tunneled connection |
| 336 | (i.e. the address included in the CONNECT request, *not* the address of the |
| 337 | proxy server). |
| 338 | |
| 339 | The headers argument should be a mapping of extra HTTP headers to send with |
| 340 | the CONNECT request. |
| 341 | |
| 342 | For example, to tunnel through a HTTPS proxy server running locally on port |
| 343 | 8080, we would pass the address of the proxy to the :class:`HTTPSConnection` |
| 344 | constructor, and the address of the host that we eventually want to reach to |
| 345 | the :meth:`~HTTPConnection.set_tunnel` method:: |
| 346 | |
| 347 | >>> import http.client |
| 348 | >>> conn = http.client.HTTPSConnection("localhost", 8080) |
| 349 | >>> conn.set_tunnel("www.python.org") |
| 350 | >>> conn.request("HEAD","/index.html") |
Senthil Kumaran | 7e5229c | 2009-12-20 07:31:21 +0000 | [diff] [blame] | 351 | |
Senthil Kumaran | 2e910fd | 2009-07-25 04:27:38 +0000 | [diff] [blame] | 352 | .. versionadded:: 3.2 |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 353 | |
Georg Brandl | 67b21b7 | 2010-08-17 15:07:14 +0000 | [diff] [blame] | 354 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 355 | .. method:: HTTPConnection.connect() |
| 356 | |
R David Murray | cae7bdb | 2015-04-05 19:26:29 -0400 | [diff] [blame] | 357 | Connect to the server specified when the object was created. By default, |
| 358 | this is called automatically when making a request if the client does not |
| 359 | already have a connection. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 360 | |
| 361 | |
| 362 | .. method:: HTTPConnection.close() |
| 363 | |
| 364 | Close the connection to the server. |
| 365 | |
Nir Soffer | ad455cd | 2017-11-06 23:16:37 +0200 | [diff] [blame] | 366 | |
| 367 | .. attribute:: HTTPConnection.blocksize |
| 368 | |
| 369 | Buffer size in bytes for sending a file-like message body. |
| 370 | |
| 371 | .. versionadded:: 3.7 |
| 372 | |
| 373 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 374 | As an alternative to using the :meth:`request` method described above, you can |
| 375 | also send your request step by step, by using the four functions below. |
| 376 | |
| 377 | |
Senthil Kumaran | 5dc504c | 2016-09-08 14:28:01 -0700 | [diff] [blame] | 378 | .. method:: HTTPConnection.putrequest(method, url, skip_host=False, \ |
| 379 | skip_accept_encoding=False) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 380 | |
Senthil Kumaran | 5dc504c | 2016-09-08 14:28:01 -0700 | [diff] [blame] | 381 | This should be the first call after the connection to the server has been |
| 382 | made. It sends a line to the server consisting of the *method* string, |
| 383 | the *url* string, and the HTTP version (``HTTP/1.1``). To disable automatic |
| 384 | sending of ``Host:`` or ``Accept-Encoding:`` headers (for example to accept |
| 385 | additional content encodings), specify *skip_host* or *skip_accept_encoding* |
| 386 | with non-False values. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 387 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 388 | |
| 389 | .. method:: HTTPConnection.putheader(header, argument[, ...]) |
| 390 | |
| 391 | Send an :rfc:`822`\ -style header to the server. It sends a line to the server |
| 392 | consisting of the header, a colon and a space, and the first argument. If more |
| 393 | arguments are given, continuation lines are sent, each consisting of a tab and |
| 394 | an argument. |
| 395 | |
| 396 | |
Martin Panter | 3c0d0ba | 2016-08-24 06:33:33 +0000 | [diff] [blame] | 397 | .. method:: HTTPConnection.endheaders(message_body=None, *, encode_chunked=False) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 398 | |
Senthil Kumaran | 5d0de3f | 2011-10-03 07:27:06 +0800 | [diff] [blame] | 399 | 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] | 400 | optional *message_body* argument can be used to pass a message body |
Martin Panter | 3c0d0ba | 2016-08-24 06:33:33 +0000 | [diff] [blame] | 401 | associated with the request. |
| 402 | |
| 403 | If *encode_chunked* is ``True``, the result of each iteration of |
| 404 | *message_body* will be chunk-encoded as specified in :rfc:`7230`, |
| 405 | Section 3.3.1. How the data is encoded is dependent on the type of |
| 406 | *message_body*. If *message_body* implements the :ref:`buffer interface |
| 407 | <bufferobjects>` the encoding will result in a single chunk. |
Serhiy Storchaka | 2e576f5 | 2017-04-24 09:05:00 +0300 | [diff] [blame] | 408 | If *message_body* is a :class:`collections.abc.Iterable`, each iteration |
Martin Panter | 3c0d0ba | 2016-08-24 06:33:33 +0000 | [diff] [blame] | 409 | of *message_body* will result in a chunk. If *message_body* is a |
| 410 | :term:`file object`, each call to ``.read()`` will result in a chunk. |
| 411 | The method automatically signals the end of the chunk-encoded data |
| 412 | immediately after *message_body*. |
| 413 | |
| 414 | .. note:: Due to the chunked encoding specification, empty chunks |
| 415 | yielded by an iterator body will be ignored by the chunk-encoder. |
| 416 | This is to avoid premature termination of the read of the request by |
| 417 | the target server due to malformed encoding. |
| 418 | |
| 419 | .. versionadded:: 3.6 |
| 420 | Chunked encoding support. The *encode_chunked* parameter was |
| 421 | added. |
| 422 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 423 | |
| 424 | .. method:: HTTPConnection.send(data) |
| 425 | |
| 426 | Send data to the server. This should be used directly only after the |
| 427 | :meth:`endheaders` method has been called and before :meth:`getresponse` is |
| 428 | called. |
| 429 | |
| 430 | |
| 431 | .. _httpresponse-objects: |
| 432 | |
| 433 | HTTPResponse Objects |
| 434 | -------------------- |
| 435 | |
Jeremy Hylton | 1052f89 | 2009-03-31 14:40:19 +0000 | [diff] [blame] | 436 | An :class:`HTTPResponse` instance wraps the HTTP response from the |
| 437 | server. It provides access to the request headers and the entity |
| 438 | body. The response is an iterable object and can be used in a with |
| 439 | statement. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 440 | |
Martin Panter | ce911c3 | 2016-03-17 06:42:48 +0000 | [diff] [blame] | 441 | .. versionchanged:: 3.5 |
| 442 | The :class:`io.BufferedIOBase` interface is now implemented and |
| 443 | all of its reader operations are supported. |
| 444 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 445 | |
| 446 | .. method:: HTTPResponse.read([amt]) |
| 447 | |
| 448 | Reads and returns the response body, or up to the next *amt* bytes. |
| 449 | |
Antoine Pitrou | 38d9643 | 2011-12-06 22:33:57 +0100 | [diff] [blame] | 450 | .. method:: HTTPResponse.readinto(b) |
| 451 | |
| 452 | Reads up to the next len(b) bytes of the response body into the buffer *b*. |
| 453 | Returns the number of bytes read. |
| 454 | |
| 455 | .. versionadded:: 3.3 |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 456 | |
Georg Brandl | 036490d | 2009-05-17 13:00:36 +0000 | [diff] [blame] | 457 | .. method:: HTTPResponse.getheader(name, default=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 458 | |
Senthil Kumaran | 790f831 | 2010-08-02 17:09:02 +0000 | [diff] [blame] | 459 | Return the value of the header *name*, or *default* if there is no header |
| 460 | matching *name*. If there is more than one header with the name *name*, |
| 461 | return all of the values joined by ', '. If 'default' is any iterable other |
| 462 | than a single string, its elements are similarly returned joined by commas. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 463 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 464 | .. method:: HTTPResponse.getheaders() |
| 465 | |
| 466 | Return a list of (header, value) tuples. |
| 467 | |
Senthil Kumaran | ceff566 | 2010-09-21 01:57:43 +0000 | [diff] [blame] | 468 | .. method:: HTTPResponse.fileno() |
| 469 | |
| 470 | Return the ``fileno`` of the underlying socket. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 471 | |
| 472 | .. attribute:: HTTPResponse.msg |
| 473 | |
Jeremy Hylton | 1052f89 | 2009-03-31 14:40:19 +0000 | [diff] [blame] | 474 | A :class:`http.client.HTTPMessage` instance containing the response |
| 475 | headers. :class:`http.client.HTTPMessage` is a subclass of |
| 476 | :class:`email.message.Message`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 477 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 478 | .. attribute:: HTTPResponse.version |
| 479 | |
| 480 | HTTP protocol version used by server. 10 for HTTP/1.0, 11 for HTTP/1.1. |
| 481 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 482 | .. attribute:: HTTPResponse.status |
| 483 | |
| 484 | Status code returned by server. |
| 485 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 486 | .. attribute:: HTTPResponse.reason |
| 487 | |
| 488 | Reason phrase returned by server. |
| 489 | |
Jeremy Hylton | 1052f89 | 2009-03-31 14:40:19 +0000 | [diff] [blame] | 490 | .. attribute:: HTTPResponse.debuglevel |
| 491 | |
Georg Brandl | ef871f6 | 2010-03-12 10:06:40 +0000 | [diff] [blame] | 492 | A debugging hook. If :attr:`debuglevel` is greater than zero, messages |
Jeremy Hylton | 1052f89 | 2009-03-31 14:40:19 +0000 | [diff] [blame] | 493 | will be printed to stdout as the response is read and parsed. |
| 494 | |
Senthil Kumaran | ce9b596 | 2011-06-19 16:56:49 -0700 | [diff] [blame] | 495 | .. attribute:: HTTPResponse.closed |
| 496 | |
Serhiy Storchaka | fbc1c26 | 2013-11-29 12:17:13 +0200 | [diff] [blame] | 497 | Is ``True`` if the stream is closed. |
Jeremy Hylton | 1052f89 | 2009-03-31 14:40:19 +0000 | [diff] [blame] | 498 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 499 | Examples |
| 500 | -------- |
| 501 | |
| 502 | Here is an example session that uses the ``GET`` method:: |
| 503 | |
Georg Brandl | 2442015 | 2008-05-26 16:32:26 +0000 | [diff] [blame] | 504 | >>> import http.client |
Benjamin Peterson | ac87ed7 | 2015-05-03 12:59:09 -0400 | [diff] [blame] | 505 | >>> conn = http.client.HTTPSConnection("www.python.org") |
| 506 | >>> conn.request("GET", "/") |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 507 | >>> r1 = conn.getresponse() |
Georg Brandl | 6911e3c | 2007-09-04 07:15:32 +0000 | [diff] [blame] | 508 | >>> print(r1.status, r1.reason) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 509 | 200 OK |
Senthil Kumaran | ce9b596 | 2011-06-19 16:56:49 -0700 | [diff] [blame] | 510 | >>> data1 = r1.read() # This will return entire content. |
| 511 | >>> # The following example demonstrates reading data in chunks. |
Benjamin Peterson | ac87ed7 | 2015-05-03 12:59:09 -0400 | [diff] [blame] | 512 | >>> conn.request("GET", "/") |
Senthil Kumaran | ce9b596 | 2011-06-19 16:56:49 -0700 | [diff] [blame] | 513 | >>> r1 = conn.getresponse() |
| 514 | >>> while not r1.closed: |
Serhiy Storchaka | dba9039 | 2016-05-10 12:01:23 +0300 | [diff] [blame] | 515 | ... print(r1.read(200)) # 200 bytes |
Benjamin Peterson | ac87ed7 | 2015-05-03 12:59:09 -0400 | [diff] [blame] | 516 | b'<!doctype html>\n<!--[if"... |
Senthil Kumaran | ce9b596 | 2011-06-19 16:56:49 -0700 | [diff] [blame] | 517 | ... |
| 518 | >>> # Example of an invalid request |
Xtreak | f0af4c5 | 2018-12-21 21:04:41 +0530 | [diff] [blame] | 519 | >>> conn = http.client.HTTPSConnection("docs.python.org") |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 520 | >>> conn.request("GET", "/parrot.spam") |
| 521 | >>> r2 = conn.getresponse() |
Georg Brandl | 6911e3c | 2007-09-04 07:15:32 +0000 | [diff] [blame] | 522 | >>> print(r2.status, r2.reason) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 523 | 404 Not Found |
| 524 | >>> data2 = r2.read() |
| 525 | >>> conn.close() |
| 526 | |
Fred Drake | 1587e3d | 2010-05-12 01:36:11 +0000 | [diff] [blame] | 527 | Here is an example session that uses the ``HEAD`` method. Note that the |
| 528 | ``HEAD`` method never returns any data. :: |
Senthil Kumaran | 71fb6c8 | 2010-04-28 17:39:48 +0000 | [diff] [blame] | 529 | |
| 530 | >>> import http.client |
Benjamin Peterson | ac87ed7 | 2015-05-03 12:59:09 -0400 | [diff] [blame] | 531 | >>> conn = http.client.HTTPSConnection("www.python.org") |
| 532 | >>> conn.request("HEAD", "/") |
Senthil Kumaran | 71fb6c8 | 2010-04-28 17:39:48 +0000 | [diff] [blame] | 533 | >>> res = conn.getresponse() |
| 534 | >>> print(res.status, res.reason) |
| 535 | 200 OK |
| 536 | >>> data = res.read() |
| 537 | >>> print(len(data)) |
| 538 | 0 |
| 539 | >>> data == b'' |
| 540 | True |
| 541 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 542 | Here is an example session that shows how to ``POST`` requests:: |
| 543 | |
Senthil Kumaran | aca8fd7 | 2008-06-23 04:41:59 +0000 | [diff] [blame] | 544 | >>> import http.client, urllib.parse |
Senthil Kumaran | 96c84a4 | 2011-07-20 21:56:24 +0800 | [diff] [blame] | 545 | >>> params = urllib.parse.urlencode({'@number': 12524, '@type': 'issue', '@action': 'show'}) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 546 | >>> headers = {"Content-type": "application/x-www-form-urlencoded", |
| 547 | ... "Accept": "text/plain"} |
Senthil Kumaran | 96c84a4 | 2011-07-20 21:56:24 +0800 | [diff] [blame] | 548 | >>> conn = http.client.HTTPConnection("bugs.python.org") |
| 549 | >>> conn.request("POST", "", params, headers) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 550 | >>> response = conn.getresponse() |
Georg Brandl | 6911e3c | 2007-09-04 07:15:32 +0000 | [diff] [blame] | 551 | >>> print(response.status, response.reason) |
Senthil Kumaran | 96c84a4 | 2011-07-20 21:56:24 +0800 | [diff] [blame] | 552 | 302 Found |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 553 | >>> data = response.read() |
Senthil Kumaran | 96c84a4 | 2011-07-20 21:56:24 +0800 | [diff] [blame] | 554 | >>> data |
| 555 | 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] | 556 | >>> conn.close() |
| 557 | |
Senthil Kumaran | b5fe247 | 2013-03-13 13:38:33 -0700 | [diff] [blame] | 558 | Client side ``HTTP PUT`` requests are very similar to ``POST`` requests. The |
| 559 | 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] | 560 | be created via ``PUT`` request. It should be noted that custom HTTP methods |
| 561 | +are also handled in :class:`urllib.request.Request` by sending the appropriate |
| 562 | +method attribute.Here is an example session that shows how to do ``PUT`` |
| 563 | request using http.client:: |
Senthil Kumaran | b5fe247 | 2013-03-13 13:38:33 -0700 | [diff] [blame] | 564 | |
| 565 | >>> # This creates an HTTP message |
| 566 | >>> # with the content of BODY as the enclosed representation |
Senthil Kumaran | 8b4a272 | 2014-04-16 23:33:02 -0400 | [diff] [blame] | 567 | >>> # for the resource http://localhost:8080/file |
Senthil Kumaran | b5fe247 | 2013-03-13 13:38:33 -0700 | [diff] [blame] | 568 | ... |
| 569 | >>> import http.client |
| 570 | >>> BODY = "***filecontents***" |
| 571 | >>> conn = http.client.HTTPConnection("localhost", 8080) |
| 572 | >>> conn.request("PUT", "/file", BODY) |
| 573 | >>> response = conn.getresponse() |
Georg Brandl | d277a56 | 2013-10-06 12:42:18 +0200 | [diff] [blame] | 574 | >>> print(response.status, response.reason) |
Senthil Kumaran | b5fe247 | 2013-03-13 13:38:33 -0700 | [diff] [blame] | 575 | 200, OK |
Jeremy Hylton | 1052f89 | 2009-03-31 14:40:19 +0000 | [diff] [blame] | 576 | |
| 577 | .. _httpmessage-objects: |
| 578 | |
| 579 | HTTPMessage Objects |
| 580 | ------------------- |
| 581 | |
Benjamin Peterson | 605b9d9 | 2009-04-02 00:24:00 +0000 | [diff] [blame] | 582 | An :class:`http.client.HTTPMessage` instance holds the headers from an HTTP |
| 583 | response. It is implemented using the :class:`email.message.Message` class. |
Jeremy Hylton | 1052f89 | 2009-03-31 14:40:19 +0000 | [diff] [blame] | 584 | |
Benjamin Peterson | 605b9d9 | 2009-04-02 00:24:00 +0000 | [diff] [blame] | 585 | .. XXX Define the methods that clients can depend upon between versions. |