Skip to content

API reference

The whole public surface of zttp. It's small on purpose. Everything here is importable straight from zttp (e.g. from zttp import Connection).

Connection

zttp.Connection is a factory. Constructing one returns the subtype for the protocol you picked - an H1Connection (the default), an H2Connection, or an H3Connection - so the surface you get matches the wire. Connection itself isn't a usable instance type and can't be subclassed; only next_event() is common to all three, because the read/write byte surface is transport-specific (HTTP/1.1 and HTTP/2 read receive_data; HTTP/3 reads receive_datagram).

zttp.Connection

A factory for a protocol-specific connection, and the shared read API.

Constructing a Connection returns the subtype for the protocol you pick - H1Connection (the default), H2Connection, or H3Connection - so the send surface matches the wire. Connection itself is not instantiable directly and cannot be subclassed; only next_event() is common to every transport, since the read/write byte surface is transport-specific (HTTP/1.1 and HTTP/2 are byte streams; HTTP/3 rides UDP datagrams).

next_event

next_event()

Return the next parse event, or the NEED_DATA sentinel if more bytes are needed.

zttp.H1Connection

Bases: Connection

An HTTP/1.1 connection: a byte-stream transport with a message-scoped API.

next_event

next_event()

Return the next parse event, or the NEED_DATA sentinel if more bytes are needed.

receive_data

receive_data(data)

Append received bytes to the parse buffer (empty bytes signals EOF).

data_to_send

data_to_send()

Return and clear the bytes queued to send.

start_next_cycle

start_next_cycle()

Reset to read the next message on a kept-alive connection.

send_request

send_request(method, target, version, headers)

Serialize a request head (client role).

send_response

send_response(status, headers=...)

Serialize a response head; the reason phrase is derived from status and the version is 1.1.

send_informational

send_informational(status, headers=...)

Serialize an interim 1xx response; the real response still follows.

send_data

send_data(data)

Serialize a run of body bytes (chunk-framed if the head declared chunked).

end_message

end_message(trailers=...)

End the outgoing message, with optional trailers (chunked bodies only).

should_close

should_close()

Whether the connection must close after this message (Connection: close / HTTP/1.0).

upgrade

upgrade()

The request's Upgrade token if it asked to upgrade (Connection: upgrade), else None.

zttp.H2Connection

Bases: Connection

An HTTP/2 connection: many streams multiplexed over one byte stream.

Attributes:

Name Type Description
send_window int

The connection-level send window in bytes (may go negative after a SETTINGS shrink).

send_window instance-attribute

send_window

next_event

next_event()

Return the next parse event, or the NEED_DATA sentinel if more bytes are needed.

receive_data

receive_data(data)

Append received bytes to the parse buffer (empty bytes signals EOF).

data_to_send

data_to_send()

Return and clear the HTTP/2 frames queued to send.

initiate_connection

initiate_connection()

Emit the connection preface (client preface + SETTINGS, or the server's SETTINGS) now.

send_request

send_request(method, target, version, headers)

Open a request stream and return its Stream (client role).

initiate_upgrade_connection

initiate_upgrade_connection(method, target, headers, settings_header=...)

Seed an h2c-upgraded connection: replay the parsed HTTP/1.1 request as stream 1.

stream

stream(stream_id)

Return the Stream handle for stream_id - the send surface for that stream.

close

close(error_code=..., last_stream_id=...)

Send GOAWAY to shut the connection down.

has_pending_send

has_pending_send()

Whether any stream still has body bytes (or a FIN) parked for the send window.

zttp.H3Connection

Bases: Connection

An HTTP/3 connection: the same streams over a from-scratch QUIC transport.

Fed UDP datagrams with receive_datagram rather than a byte stream, and data_to_send() returns a datagram per element (QUIC datagram boundaries are semantic). Responses go through a Stream handle, exactly as on HTTP/2. Server credentials default to an ephemeral local identity; now is the integrator's monotonic clock, in the unit later fed to handle_timeout.

next_event

next_event()

Return the next parse event, or the NEED_DATA sentinel if more bytes are needed.

data_to_send

data_to_send()

Return and clear the pending outgoing UDP datagrams, one per list element.

receive_datagram

receive_datagram(datagram, now=..., peer_address=...)

Feed one received UDP datagram. peer_address is an opaque key for path validation.

data_to_send_with_addresses

data_to_send_with_addresses()

Like data_to_send, but as (datagram, peer_address) pairs.

challenge_path

challenge_path(peer_address, data)

Queue a QUIC PATH_CHALLENGE to a peer address (data must be 8 unpredictable bytes).

use_peer_connection_id

use_peer_connection_id(sequence_number)

Switch outgoing packets to a peer-issued NEW_CONNECTION_ID sequence.

issue_connection_id

issue_connection_id(sequence_number, connection_id, stateless_reset_token, retire_prior_to=...)

Queue a QUIC NEW_CONNECTION_ID for a local connection ID.

request_key_update

request_key_update()

Advance the QUIC 1-RTT send keys; the next packet carries the new key phase.

initiate_connection

initiate_connection()

Open the control stream and send SETTINGS now, rather than lazily on the first response.

send_request

send_request(method, target, version, headers)

Open a request stream and return its Stream (client role).

send_session_ticket

send_session_ticket(ticket, lifetime=..., age_add=..., nonce=..., extensions=..., max_early_data_size=...)

Queue a TLS NewSessionTicket on a confirmed server connection; returns its PSK when available.

send_new_token

send_new_token(token)

Queue a QUIC NEW_TOKEN address-validation token (server role).

session_tickets

session_tickets()

The TLS session tickets received from the peer, as SessionTickets.

validation_tokens

validation_tokens()

The NEW_TOKEN address-validation tokens received, for reuse on a future connection.

shutdown

shutdown(stream_id)

Begin a graceful shutdown by sending GOAWAY (RFC 9114 5.2).

close

close(app=..., error_code=..., reason=...)

Send a QUIC CONNECTION_CLOSE. app=True sends an HTTP/3 application close.

stream

stream(stream_id)

Return the Stream handle for stream_id (the request's stream_id).

next_timeout

next_timeout()

The next idle/loss/PTO deadline (same clock as now), or None if no timer is armed.

handle_timeout

handle_timeout(now)

Fire the timer at time now: close on idle timeout, or re-queue loss probes.

is_closed

is_closed()

Whether the connection has closed (peer CONNECTION_CLOSE or idle timeout).

idle_timed_out

idle_timed_out()

Whether the connection was silently closed by the idle timeout (RFC 9000 10.1).

close_info

close_info()

The peer's CONNECTION_CLOSE as a CloseInfo, or None.

peer_settings

peer_settings()

The peer's HTTP/3 SETTINGS as a dict, or None until its SETTINGS frame arrives.

goaway_received

goaway_received()

The id of a GOAWAY received from the peer (RFC 9114 5.2), or None.

A Stream is the per-stream send handle on HTTP/2 and HTTP/3 connections; see HTTP/2 and HTTP/3.

zttp.Stream

A borrowed, re-validated handle to one stream - the single send surface for HTTP/2 and HTTP/3. Obtained via conn.stream(stream_id).

Attributes:

Name Type Description
stream_id int

The stream this handle sends on.

send_window int | None

The stream's remaining send window in bytes, or None.

pending_bytes int | None

Body bytes parked waiting for flow-control credit, or None.

stream_id instance-attribute

stream_id

send_window instance-attribute

send_window

pending_bytes instance-attribute

pending_bytes

send_response

send_response(status, headers=..., end_stream=...)

Send a response head on this stream. Set end_stream for a bodyless response.

send_informational

send_informational(status, headers=...)

Send an interim 1xx response; the real response still follows.

send_data

send_data(data)

Send a run of body bytes, subject to the peer's flow-control window.

end_message

end_message(trailers=...)

End the message on this stream, with optional trailers.

reset

reset(error_code=...)

Abruptly cancel this stream (RST_STREAM / STOP_SENDING).

Roles and protocols

A Connection's role and protocol are fixed at construction:

  • zttp.SERVER: you receive requests, send responses.
  • zttp.CLIENT: you send requests, receive responses.
  • zttp.HTTP1 (default): one message at a time; you send on the connection.
  • zttp.HTTP2: multiplexed streams; you send on a Stream.
  • zttp.HTTP3: the same streams over QUIC; you feed UDP datagrams with receive_datagram (see HTTP/3).

For HTTP/3, a server's TLS identity is passed as credentials=TlsCredentials(...) and a resumption secret as resumption=SessionResumption(...) - typed value objects, so the same-typed bytes pairs can't be transposed (omit credentials and the server uses an ephemeral local identity). zttp supplies the QUIC transport defaults internally; the remaining constructor fields (transport_params, connection_id, random, ephemeral_seed, ...) are advanced overrides for deterministic tests and interoperability work.

HTTP/3 value objects

Passed to the HTTP/3 constructor; both are keyword-only frozen dataclasses so the same-typed bytes fields can't be swapped.

zttp.TlsCredentials dataclass

An HTTP/3 server's TLS identity: the certificate and its private key.

certificate instance-attribute

certificate

private_key instance-attribute

private_key

__init__

__init__(*, certificate, private_key)

zttp.SessionResumption dataclass

A TLS-PSK resumption secret: the ticket identity and its pre-shared key.

identity instance-attribute

identity

psk instance-attribute

psk

__init__

__init__(*, identity, psk)

Events

next_event returns one of these. On HTTP/2 and HTTP/3 each also carries a .stream_id.

zttp.Request

A parsed request head: the request line and all headers.

Yielded by next_event() on a server connection once the head is complete, before any body Data. Every value is raw bytes, exactly as received - zttp does not percent-decode the target.

Attributes:

Name Type Description
method bytes

The request method, e.g. b"GET".

target bytes

The raw request-target, e.g. b"/path?q=1".

path bytes

target up to the first ? (not percent-decoded).

query bytes

target after the first ?, or b"" (not percent-decoded).

http_version bytes

The version, e.g. b"1.1" (b"2" / b"3" on HTTP/2 and HTTP/3).

headers list[tuple[bytes, bytes]]

The header fields as (name, value) byte pairs, in received order.

stream_id int

The stream the request arrived on (0 on HTTP/1.1).

expect_continue bool

Whether the client sent Expect: 100-continue.

method instance-attribute

method

target instance-attribute

target

path instance-attribute

path

query instance-attribute

query

http_version instance-attribute

http_version

headers instance-attribute

headers

stream_id instance-attribute

stream_id

expect_continue instance-attribute

expect_continue

zttp.Response

A parsed response head: the status line and all headers.

Yielded by next_event() on a client connection once the head is complete, before any body Data.

Attributes:

Name Type Description
status_code int

The status code, e.g. 200.

reason bytes

The reason phrase, e.g. b"OK" (empty on HTTP/2 and HTTP/3).

http_version bytes

The version, e.g. b"1.1".

headers list[tuple[bytes, bytes]]

The header fields as (name, value) byte pairs, in received order.

stream_id int

The stream the response arrived on (0 on HTTP/1.1).

status_code instance-attribute

status_code

reason instance-attribute

reason

http_version instance-attribute

http_version

headers instance-attribute

headers

stream_id instance-attribute

stream_id

zttp.Data

A run of decoded body bytes.

One or more Data events arrive between the head and EndOfMessage; chunked and content-length bodies both surface the same way, already decoded.

Attributes:

Name Type Description
data bytes

The body bytes, copied out of the parse buffer (safe to keep).

stream_id int

The stream the body belongs to (0 on HTTP/1.1).

data instance-attribute

data

stream_id instance-attribute

stream_id

zttp.EndOfMessage

The end of a message, with any trailers.

Attributes:

Name Type Description
trailers list[tuple[bytes, bytes]]

Trailer fields as (name, value) byte pairs, or [].

stream_id int

The stream that finished (0 on HTTP/1.1).

trailers instance-attribute

trailers

stream_id instance-attribute

stream_id

HTTP/2 control events

An HTTP/2 connection also surfaces the protocol's control frames. zttp acts on the ones that matter on its own; they're here when you want visibility.

zttp.Settings

An HTTP/2 SETTINGS frame from the peer. zttp acks it for you.

Attributes:

Name Type Description
params list[tuple[int, int]]

The settings as (identifier, value) integer pairs; see zttp.H2Settings for the identifier names.

params instance-attribute

params

zttp.WindowUpdate

An HTTP/2 WINDOW_UPDATE: the peer granted more flow-control credit.

Attributes:

Name Type Description
stream_id int

The stream credited, or 0 for the whole connection.

increment int

The number of bytes added to the send window.

stream_id instance-attribute

stream_id

increment instance-attribute

increment

zttp.Ping

An HTTP/2 PING. zttp replies to a non-ack ping for you.

Attributes:

Name Type Description
ack bool

Whether this is an ack of a ping zttp sent.

data bytes

The 8 opaque payload bytes.

ack instance-attribute

ack

data instance-attribute

data

zttp.RstStream

An HTTP/2 RST_STREAM: the peer abruptly cancelled a stream.

Attributes:

Name Type Description
stream_id int

The cancelled stream.

error_code int

The RFC 9113 error code the peer sent.

stream_id instance-attribute

stream_id

error_code instance-attribute

error_code

zttp.GoAway

An HTTP/2 GOAWAY: the peer is shutting the connection down.

Attributes:

Name Type Description
last_stream_id int

The highest stream the peer will still process.

error_code int

The RFC 9113 error code.

debug bytes

Optional opaque debug data, or b"".

last_stream_id instance-attribute

last_stream_id

error_code instance-attribute

error_code

debug instance-attribute

debug

The integer ids in a Settings event's params have names (RFC 9113 6.5.2):

zttp.H2Settings

Bases: IntEnum

The HTTP/2 SETTINGS parameter identifiers (RFC 9113 6.5.2).

Names the integer ids carried in a Settings event's params, so they can be read without magic numbers.

HEADER_TABLE_SIZE class-attribute instance-attribute

HEADER_TABLE_SIZE = 1

ENABLE_PUSH class-attribute instance-attribute

ENABLE_PUSH = 2

MAX_CONCURRENT_STREAMS class-attribute instance-attribute

MAX_CONCURRENT_STREAMS = 3

INITIAL_WINDOW_SIZE class-attribute instance-attribute

INITIAL_WINDOW_SIZE = 4

MAX_FRAME_SIZE class-attribute instance-attribute

MAX_FRAME_SIZE = 5

MAX_HEADER_LIST_SIZE class-attribute instance-attribute

MAX_HEADER_LIST_SIZE = 6

HTTP/3 results

Returned by an H3Connection's introspection methods.

zttp.SessionTicket dataclass

A TLS session ticket received from the peer (RFC 8446 4.6.1).

lifetime instance-attribute

lifetime

age_add instance-attribute

age_add

nonce instance-attribute

nonce

ticket instance-attribute

ticket

extensions instance-attribute

extensions

max_early_data_size instance-attribute

max_early_data_size

psk instance-attribute

psk

__init__

__init__(lifetime, age_add, nonce, ticket, extensions, max_early_data_size, psk)

zttp.CloseInfo dataclass

A QUIC CONNECTION_CLOSE received from the peer (RFC 9000 19.19).

error_code instance-attribute

error_code

reason instance-attribute

reason

is_application instance-attribute

is_application

__init__

__init__(error_code, reason, is_application)

Sentinels

Value Meaning
zttp.NEED_DATA No complete event yet; feed more bytes. Compare with is.
zttp.CONNECTION_CLOSED The peer closed the connection. Compare with is.

Exceptions

zttp.ProtocolError

Bases: Exception

Base class for the two protocol errors. Catch this to handle both.

zttp.RemoteProtocolError

Bases: ProtocolError

The peer sent something malformed. Raised from next_event().

zttp.LocalProtocolError

Bases: ProtocolError

You used the send API in a way that cannot produce a valid message.