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