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