pylsqpack

0.3.24 · active · verified Fri Apr 10

pylsqpack is a Python wrapper for the `ls-qpack` C library, providing Python `Decoder` and `Encoder` objects for reading and writing HTTP/3 headers compressed with QPACK. It enables applications to handle the header compression and decompression necessary for HTTP/3. It is currently at version 0.3.24 and is maintained as part of the `aiortc` project. [1, 4]

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize an `Encoder` and `Decoder`, encode a list of HTTP/3 headers, and then decode the resulting header block. Note the use of bytes for header names and values, and the `max_table_capacity` and `blocked_streams` parameters for the `Decoder` (and implicitly for `Encoder` settings which would be exchanged via HTTP/3 SETTINGS frames). [2, 7]

from pylsqpack import Encoder, Decoder

# Example HTTP/3 headers
headers = [
    (b':method', b'GET'),
    (b':scheme', b'https'),
    (b':path', b'/resource'),
    (b':authority', b'example.org'),
    (b'user-agent', b'pylsqpack-example/1.0')
]

# Encoder initialization
# The exact values for max_table_capacity and blocked_streams depend on QPACK settings
e = Encoder()

# Encode headers for stream ID 0
# The encode method returns a tuple: (encoder_stream_data, encoded_header_block)
encoder_stream_data, encoded_header_block = e.encode(0, headers)

print("Original Headers:", headers)
print("Encoded Header Block:", encoded_header_block)

# Decoder initialization
# max_table_capacity and blocked_streams must match peer's settings
d = Decoder(max_table_capacity=4096, blocked_streams=100)

# Feed encoder stream data to the decoder, if any
# (In simple encode/decode, this might be empty, but important for full QPACK interaction)
if encoder_stream_data:
    unblocked_streams = d.feed_encoder_stream(encoder_stream_data)
    # For actual HTTP/3, you'd then resume decoding for unblocked_streams

# Decode the header block for stream ID 0
try:
    control_data, decoded_headers = d.decode(0, encoded_header_block)
    print("Decoded Headers:", decoded_headers)
except Exception as e:
    print(f"Decoding failed: {e}")

view raw JSON →