h2

A pure-Python, sans-I/O implementation of the HTTP/2 protocol state machine

Library
PyPI
v4.4.1
1,040stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
69/100Good
Development Activity72
Maintenance32
Community84
Maturity60
Momentum28

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
81/100Excellent
Architecture90
Code Quality85
Innovation85
Learning Curve65

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 H2Connection state 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 from receive_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.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search