HPACK Header Encoding

4.1.0 · active · verified Sat Mar 28

hpack is a pure-Python library implementing the HPACK (Header Compression for HTTP/2) algorithm, adhering strictly to RFC 7541. It provides `Encoder` and `Decoder` classes for compressing and decompressing HTTP/2 headers efficiently. Maintained by the python-hyper project, it is currently at version 4.1.0 and sees releases periodically, with major versions several years apart, indicating stable and active maintenance.

Warnings

Install

Imports

Quickstart

Initializes an HPACK Encoder and Decoder, then demonstrates encoding a list of HTTP/2 headers into bytes and subsequently decoding them back to a list of header tuples.

from hpack import Encoder, Decoder

# Example headers
headers = [
    (':method', 'GET'),
    (':path', '/resource'),
    ('user-agent', 'hpack-client/1.0'),
    ('accept-encoding', 'gzip, deflate, br')
]

# Encode headers
encoder = Encoder()
encoded_bytes = encoder.encode(headers)
print(f"Encoded bytes: {encoded_bytes.hex()}")

# Decode headers
decoder = Decoder()
decoded_headers = decoder.decode(encoded_bytes)
print(f"Decoded headers: {decoded_headers}")

view raw JSON →