---
source: https://iinventedlight.com/library/notes/python-ssl.md
title: "Python ssl - wrap sockets with SSLContext and create_default_context"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "PSF-2.0 (Python docs); PSF-2.0 (python/cpython (GitHub)); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "Summary by iinventedlight (AI-assisted). Sources: Python docs, ssl - TLS/SSL wrapper for socket objects; python/cpython (GitHub), Lib/ssl.py. PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-18
status: published
approved_by: "Cole Withington, 2026-11-20 13:14 PT: \"Publish the full batch and rewrite the 6 live notes\""
checked_on: 2026-11-18
as_of: 2026-11-18
confidence: medium
untested: "Not reproduced (2026-11-18). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-python: ssl"
tags: [python, ssl, tls, certificates, stdlib]
client: Librarian
sources:
  - id: S1
    title: "ssl - TLS/SSL wrapper for socket objects"
    publisher: Python docs
    url: https://docs.python.org/3/library/ssl.html
    accessed: 2026-11-18
    license: "PSF-2.0"
  - id: S2
    title: "Lib/ssl.py"
    publisher: python/cpython (GitHub)
    url: https://github.com/python/cpython/blob/main/Lib/ssl.py
    accessed: 2026-11-18
    license: "PSF-2.0"
---
# Python ssl - wrap sockets with SSLContext and create_default_context
**Answer:** `ssl.create_default_context()` returns a new SSLContext object with default settings. This module provides some more Pythonic support for SSL on top of OpenSSL. Use `wrap_socket` on an existing Python socket.

## Examples

```python
import socket, ssl

hostname = "example.com"
context = ssl.create_default_context()
with socket.create_connection((hostname, 443), timeout=5) as raw:
    with context.wrap_socket(raw, server_hostname=hostname) as tls:
        print(tls.version(), tls.selected_alpn_protocol())
        tls.sendall(b"HEAD / HTTP/1.1\r\nHost: example.com\r\n\r\n")
        print(tls.recv(1024))
```

## Rules

- The installed version of OpenSSL may cause variations in behavior.

## See also

- [python-socket](https://iinventedlight.com/library/notes/python-socket/)

## Claims

- id: C1
  claim: create_default_context returns an SSLContext with default settings.
  source: S1
  evidence: "Return a new SSLContext object with default settings"

- id: C2
  claim: The module provides Pythonic support for SSL.
  source: S2
  evidence: "This module provides some more Pythonic support for SSL"

- id: C3
  claim: OpenSSL versions can change TLS behavior.
  source: S1
  evidence: "The installed version of OpenSSL may also cause variations in behavior"

## Sources

- [S1] ssl - TLS/SSL wrapper for socket objects. Python docs. https://docs.python.org/3/library/ssl.html . License: PSF-2.0. Accessed 2026-11-18.

- [S2] Lib/ssl.py. python/cpython (GitHub). https://github.com/python/cpython/blob/main/Lib/ssl.py . License: PSF-2.0. Accessed 2026-11-18.

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Python docs, ssl - TLS/SSL wrapper for socket objects; python/cpython (GitHub), Lib/ssl.py. PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation. Text was condensed and changed from the originals.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
