h2
A pure-Python, sans-I/O implementation of the HTTP/2 protocol state machine
Repository Health
Technical Analysis
h2 implements the full HTTP/2 protocol as a pure state machine with no I/O of its own: you feed it bytes off the wire and send it API calls to build requests, and it hands back a stream of events and bytes to write, leaving the actual socket, async framework, or transport entirely up to the caller. This “sans-I/O” design is why h2 has become the shared HTTP/2 engine underneath multiple unrelated networking stacks, including httpx, hyper, and Twisted’s HTTP/2 support.
Internally, h2 builds on two smaller sibling libraries from the same python-hyper organization: hyperframe for parsing/serializing the raw HTTP/2 frame format, and hpack for the HPACK header-compression codec that HTTP/2 requires. Together the three packages implement HTTP/2 end to end while staying decoupled from any specific networking or async runtime.
What You Get
- A
H2Connectionstate machine implementing the full HTTP/2 spec (streams, flow control, settings negotiation, priority, push, GOAWAY handling) - A sans-I/O design that works identically over blocking sockets, asyncio, Twisted, or any other transport, since h2 never performs I/O itself
- An event-based API (
ConnectionTerminated,RequestReceived,DataReceived,StreamEnded, etc.) returned fromreceive_data()for reacting to protocol activity - Built-in dependencies on hpack for HPACK header compression and hyperframe for HTTP/2 frame parsing, so callers don’t reimplement either
- Used as the underlying HTTP/2 engine inside httpx, hyper, and other Python networking libraries
Common Use Cases
- Implementing HTTP/2 client or server support inside a custom networking library or async framework without adopting a full HTTP stack
- Building an HTTP/2 proxy or gateway that needs fine-grained control over frame-level behavior
- Powering HTTP/2 support in higher-level HTTP client libraries such as httpx, which delegates protocol handling to h2
- Writing protocol-level tests or fuzzers against HTTP/2 behavior using h2’s deterministic, I/O-free state machine
Under The Hood
Architecture - connection.py’s H2Connection is the central state machine, delegating per-stream state to stream.py, flow-control window accounting to windows.py, and settings negotiation to settings.py; incoming bytes flow through frame_buffer.py (which uses hyperframe to parse raw frames) before being dispatched into stream/connection state transitions that produce events.py objects. Tech Stack - Pure Python with two direct sibling dependencies from the same organization, hyperframe (frame serialization) and hpack (HPACK header compression), plus internal typing support via _typing.py; no networking or async library dependency at all, by design. Code Quality - 27 test files exercise the connection state machine, stream lifecycle, flow control, and error conditions, with CI badges for build status and Codecov coverage visible in the README; the codebase has an errors.py/exceptions.py split giving well-defined error types for protocol violations. API Design - The sans-I/O event/action pattern (feed bytes in, get events and bytes-to-send out) is a deliberate, well-documented architectural choice that requires understanding the pattern upfront but then generalizes cleanly across any transport, which is why multiple unrelated projects embed h2 rather than reimplementing HTTP/2.
Used by 2 apps in this directory
Agno
Devops · AI Development · Automation
Build, run, and manage agent platforms with a full production stack — SDK, runtime, and control plane included.
SWIRL
Search · Databases · Data Engineering
Federated AI search and RAG across 100+ enterprise sources—no data extraction, no vector database required.