Python Glossary Русский

all Python 3urllib.requestterm

urllib.request.HTTPSHandler

protocol handlers

The handler for https:// URLs; it is in the standard set only when Python is built with the ssl module.

Syntax

urllib.request.HTTPSHandler(debuglevel=0, context=None)

The server certificate is verified by default, and CERTIFICATE_VERIFY_FAILED means a problem with the system certificates (on macOS Install Certificates was often never run), not a reason to switch verification off. Pass your own CA through context=ssl.create_default_context(cafile=...); a context with verify_mode=CERT_NONE invites server impersonation.

Examples

import ssl, urllib.request
ctx = ssl.create_default_context()
print(ctx.check_hostname, ctx.verify_mode == ssl.CERT_REQUIRED)  # → True True
opener = urllib.request.build_opener(urllib.request.HTTPSHandler(context=ctx))
print(any(type(h) is urllib.request.HTTPSHandler for h in opener.handlers))  # → True

See also

Open in the full glossarydocs.python.orgReport an inaccuracy