all Python 3http.clientterm
http.client.HTTPSConnection
connections
An HTTP connection over TLS: the same as HTTPConnection, but the default port is 443 and traffic is encrypted. TLS settings come through the context parameter.
Syntax
conn = http.client.HTTPSConnection(host, port=None, *, timeout=..., context=None)
Since 3.4.3 the certificate and the host name are verified by default, and a self-signed certificate raises ssl.SSLCertVerificationError; disabling the check instead of adding your CA through ssl.create_default_context(cafile=...) is dangerous. The key_file, cert_file and check_hostname parameters were removed in 3.12: old code gets TypeError, and a client certificate is now loaded with context.load_cert_chain(). As with HTTPConnection, the constructor does not touch the network; the TLS handshake happens on the first request().
Examples
import http.client
import ssl
conn = http.client.HTTPSConnection('example.com', timeout=5)
print(conn.port, isinstance(conn, http.client.HTTPConnection)) # → 443 True
ctx = ssl.create_default_context()
print(ctx.verify_mode == ssl.CERT_REQUIRED, ctx.check_hostname) # → True True
conn = http.client.HTTPSConnection('example.com', context=ctx)
print(conn.sock) # → None — рукопожатие будет при первом request()
# Python 3.12+
import http.client
try:
http.client.HTTPSConnection('example.com', check_hostname=False)
except TypeError as e:
print('check_hostname' in str(e)) # → True — параметр удалён, настраивайте context
See also
Open in the full glossarydocs.python.orgReport an inaccuracy