Keep TLS Peer Verification Enabled
Core idea
TLS protects traffic and can authenticate peers. For HTTPS clients use ssl.create_default_context(), which enables certificate-chain and hostname verification for server authentication. Pass the intended hostname when wrapping a connected socket. A verified chain alone is insufficient if the expected identity is not checked. These examples inspect configuration only; they do not perform a handshake.
import sslctx = ssl.create_default_context()print(ctx.check_hostname)print(ctx.verify_mode == ssl.CERT_REQUIRED)The default client context requires a trusted certificate and hostname matching.
Configure a protocol floor
import sslctx = ssl.create_default_context()ctx.minimum_version = ssl.TLSVersion.TLSv1_2print(ctx.minimum_version.name)print(ctx.check_hostname, ctx.verify_mode == ssl.CERT_REQUIRED)Setting a minimum protocol version does not turn off identity checks.
Common mistake
Do not bypass certificate or hostname errors to make a connection work. Diagnose hostname, trust roots, clock and certificate configuration; fail closed. Trust store availability varies by environment.
Practice
Inspect a fresh context and write down which settings enforce identity. Describe a trusted local certificate fixture for a future integration test.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
TLS and certificate verification
Check your understanding of tls and certificate verification.
What is printed?
import sslprint(ssl.create_default_context().check_hostname)What is printed?
import sslprint(ssl.create_default_context().verify_mode.name)What should a hostname mismatch trigger?
Did a context inspection prove a remote certificate valid?