hpack

A pure-Python implementation of the HPACK header compression algorithm used by HTTP/2

Library
PyPI
v4.2.0
81stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
42/100Fair
Development Activity36
Maintenance12
Community48
Maturity60
Momentum12

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
79/100Good
Architecture82
Code Quality84
Innovation70
Learning Curve80

hpack implements HPACK, the header-compression scheme HTTP/2 requires to shrink the overhead of sending largely repetitive HTTP headers on every request. It provides an Encoder and Decoder pair that maintain the dynamic table and Huffman coding state HPACK depends on, translating between Python header lists and the compact binary representation that goes on the wire.

As one of the three python-hyper sibling libraries (alongside hyperframe and h2), hpack is a low-level, single-purpose building block: h2 depends on it directly to compress and decompress headers as part of implementing the full HTTP/2 protocol, and it can be used standalone by anything else that needs to speak HPACK.

What You Get

  • An Encoder class that compresses a list of header name/value pairs into HPACK’s binary wire format, including dynamic table management
  • A Decoder class that reverses the process, turning received HPACK-encoded bytes back into header lists
  • A full Huffman coding table implementation (huffman.py, huffman_constants.py, huffman_table.py) matching the HTTP/2 spec’s static Huffman code
  • Dedicated exceptions.py types for HPACK-specific error conditions (decoding errors, table size violations)
  • Used directly by h2 as its HPACK codec, so any project consuming h2 already depends on hpack transitively

Common Use Cases

  • Implementing HTTP/2 header compression/decompression as part of a custom HTTP/2 client or server (typically via h2 rather than hpack directly)
  • Building tooling that inspects or manipulates raw HTTP/2 traffic and needs to decode HPACK-compressed header blocks
  • Writing HPACK conformance tests or fuzzers against the encoder/decoder pair in isolation from the rest of HTTP/2
  • Any protocol implementation outside HTTP/2 that chooses to reuse HPACK’s compression scheme for header-like data

Under The Hood

Architecture - hpack.py exposes the public Encoder/Decoder classes, table.py implements the dynamic header table with its eviction and size-accounting rules, struct.py handles HPACK’s variable-length integer encoding, and the huffman*.py modules implement the static Huffman code tables and encode/decode logic as a self-contained unit. Tech Stack - Pure Python with no external runtime dependencies, keeping it trivially embeddable in any project (h2 included) without pulling in additional transitive dependencies. Code Quality - 9 test files cover the encoder, decoder, dynamic table behavior, and Huffman coding paths, with CI and Codecov badges tracked in the README; the small, focused module layout (8 files total) keeps each concern isolated and easy to audit. API Design - The Encoder().encode(headers) / Decoder().decode(data) pair is a minimal, purpose-built API with almost no configuration surface beyond table size, matching HPACK’s narrow scope as a header-compression codec rather than a general-purpose library.

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