Skip to content

Asyncio API

Use the top-level imports shown below.

connect

connect async

connect(host, port, *, configuration=None, create_protocol=QuicConnectionProtocol, session_ticket_handler=None, stream_handler=None, wait_connected=True, local_port=0)

Connect to a QUIC server at the given host and port.

:meth:connect() returns an awaitable. Awaiting it yields a :class:~qh3.asyncio.QuicConnectionProtocol which can be used to create streams.

:func:connect also accepts the following optional arguments:

  • configuration is a :class:~qh3.quic.configuration.QuicConfiguration configuration object.
  • create_protocol allows customizing the :class:~asyncio.Protocol that manages the connection. It should be a callable or class accepting the same arguments as :class:~qh3.asyncio.QuicConnectionProtocol and returning an instance of :class:~qh3.asyncio.QuicConnectionProtocol or a subclass.
  • session_ticket_handler is a callback which is invoked by the TLS engine when a new session ticket is received.
  • stream_handler is a callback which is invoked whenever a stream is created. It must accept two arguments: a :class:asyncio.StreamReader and a :class:asyncio.StreamWriter.
  • local_port is the UDP port number that this client wants to bind.

serve

serve async

serve(host, port, *, configuration, create_protocol=QuicConnectionProtocol, session_ticket_fetcher=None, session_ticket_handler=None, retry=False, stream_handler=None)

Start a QUIC server at the given host and port.

:func:serve requires a :class:~qh3.quic.configuration.QuicConfiguration containing TLS certificate and private key as the configuration argument.

:func:serve also accepts the following optional arguments:

  • create_protocol allows customizing the :class:~asyncio.Protocol that manages the connection. It should be a callable or class accepting the same arguments as :class:~qh3.asyncio.QuicConnectionProtocol and returning an instance of :class:~qh3.asyncio.QuicConnectionProtocol or a subclass.
  • session_ticket_fetcher is a callback which is invoked by the TLS engine when a session ticket is presented by the peer. It should return the session ticket with the specified ID or None if it is not found.
  • session_ticket_handler is a callback which is invoked by the TLS engine when a new session ticket is issued. It should store the session ticket for future lookup.
  • retry specifies whether client addresses should be validated prior to the cryptographic handshake using a retry packet.
  • stream_handler is a callback which is invoked whenever a stream is created. It must accept two arguments: a :class:asyncio.StreamReader and a :class:asyncio.StreamWriter.

The returned server handle supports close(). Its concrete implementation is not a public construction or subclassing API.

QuicConnectionProtocol

QuicConnectionProtocol

Bases: DatagramProtocol

change_connection_id

change_connection_id()

Change the connection ID used to communicate with the peer.

The previous connection ID will be retired.

close

close()

Close the connection.

connect

connect(addr)

Initiate the TLS handshake.

This method can only be called for clients and a single time.

create_stream async

create_stream(is_unidirectional=False)

Create a QUIC stream and return a pair of (reader, writer) objects.

The returned reader and writer objects are instances of :class:asyncio.StreamReader and :class:asyncio.StreamWriter classes.

ping async

ping()

Ping the peer and wait for the response.

quic_event_received

quic_event_received(event)

Called when a QUIC event is received.

Reimplement this in your subclass to handle the events.

request_key_update

request_key_update()

Request an update of the encryption keys.

transmit

transmit()

Send pending datagrams to the peer and arm the timer if needed.

wait_closed async

wait_closed()

Wait for the connection to be closed.

wait_connected async

wait_connected()

Wait for the TLS handshake to complete.