20.10. http.client
— HTTP protocol client¶
Source code: Lib/http/client.py
This module defines classes which implement the client side of the HTTP and
HTTPS protocols. It is normally not used directly — the module
urllib.request
uses it to handle URLs that use HTTP and HTTPS.
Note
HTTPS support is only available if Python was compiled with SSL support
(through the ssl
module).
The module provides the following classes:
-
class
http.client.
HTTPConnection
(host, port=None[, strict[, timeout[, source_address]]])¶ An
HTTPConnection
instance represents one transaction with an HTTP server. It should be instantiated passing it a host and optional port number. If no port number is passed, the port is extracted from the host string if it has the formhost:port
, else the default HTTP port (80) is used. If the optional timeout parameter is given, blocking operations (like connection attempts) will timeout after that many seconds (if it is not given, the global default timeout setting is used). The optional source_address parameter may be a tuple of a (host, port) to use as the source address the HTTP connection is made from.For example, the following calls all create instances that connect to the server at the same host and port:
>>> h1 = http.client.HTTPConnection('www.cwi.nl') >>> h2 = http.client.HTTPConnection('www.cwi.nl:80') >>> h3 = http.client.HTTPConnection('www.cwi.nl', 80) >>> h3 = http.client.HTTPConnection('www.cwi.nl', 80, timeout=10)
Changed in version 3.2:
Changed in version 3.2: source_address was added.
Changed in version 3.2:
Changed in version 3.2: The strict parameter is deprecated. HTTP 0.9-style “Simple Responses” are not supported anymore.
-
class
http.client.
HTTPSConnection
(host, port=None, key_file=None, cert_file=None, [strict, [timeout, [source_address, ]]]*, context=None, check_hostname=None)¶ A subclass of
HTTPConnection
that uses SSL for communication with secure servers. Default port is443
. If context is specified, it must be assl.SSLContext
instance describing the various SSL options. If context is specified and has averify_mode
of eitherCERT_OPTIONAL
orCERT_REQUIRED
, then by default host is matched against the host name(s) allowed by the server’s certificate. If you want to change that behaviour, you can explicitly set check_hostname to False.key_file and cert_file are deprecated, please use
ssl.SSLContext.load_cert_chain()
instead.If you access arbitrary hosts on the Internet, it is recommended to require certificate checking and feed the context with a set of trusted CA certificates:
context = ssl.SSLContext(ssl.PROTOCOL_TLSv1) context.verify_mode = ssl.CERT_REQUIRED context.load_verify_locations('/etc/pki/tls/certs/ca-bundle.crt') h = client.HTTPSConnection('svn.python.org', 443, context=context)
Changed in version 3.2:
Changed in version 3.2: source_address, context and check_hostname were added.
Changed in version 3.2:
Changed in version 3.2: This class now supports HTTPS virtual hosts if possible (that is,
if ssl.HAS_SNI
is true).
Changed in version 3.2:
Changed in version 3.2: The strict parameter is deprecated. HTTP 0.9-style “Simple Responses” are not supported anymore.
-
class
http.client.
HTTPResponse
(sock, debuglevel=0, [strict, ]method=None, url=None)¶ Class whose instances are returned upon successful connection. Not instantiated directly by user.
Changed in version 3.2:
Changed in version 3.2: The strict parameter is deprecated. HTTP 0.9-style “Simple Responses” are not supported anymore.