ssl.create_default_context()
ssl.create_default_context() is a Python function in the SSL Module category. Creates a default SSL/TLS context with reasonable security settings. The syntax is ssl.create_default_context(purpose=ssl.Purpose.SERVER_AUTH, *, cafile=None, capath=None, cadata=None). Returns: SSLContext object. A typical example: ctx = ssl.create_default_context()
with socket.create_connection(("example.com", 443)) as sock:
with ctx.wrap_socket(sock, server_hostname="example.com") as ssock:
ssock.sendall(b"GET / HTTP/1.1\r\n\r\n")
data = ssock.recv(4096). A close sibling is ssl.wrap_socket(), which wraps an existing socket for SSL/TLS encryption. A close sibling is ssl.get_server_certificate(), which fetches the SSL certificate from a server and returns it as PEM string. A close sibling is ssl.SSLContext(), which creates an SSL context for configuring SSL/TLS connections. More about this category: SSL/TLS wrapper — wrap_socket, get_server_certificate, create_default_context. Related SSL Module entries: ssl.wrap_socket(), ssl.get_server_certificate(), ssl.SSLContext(). This page is part of the free Python Reference documentation covering 978+ functions, methods, and modules with examples and parameter details. You can try ssl.create_default_context() immediately in the Python playground, look up unfamiliar terms in the glossary, or return to the Python Reference homepage.