Skip to main content

An async-native WebTransport stack for Python.

Project description

PyWebTransport Logo

PyWebTransport

An async-native WebTransport stack for Python


PyPI version Python Version License: Apache 2.0 CI Coverage Docs

Features

  • Sans-I/O Architecture: Powered by an ownership-driven Rust state machine decoupled from the I/O runtime.
  • Transport Primitives: Full implementation of bidirectional streams, unidirectional streams, and unreliable datagrams.
  • Structured Concurrency: Deterministic lifecycle management for connections and streams via asynchronous context managers.
  • Zero-Copy I/O: End-to-end support for buffer protocols and memoryview to minimize data copying overhead.
  • Typed Messaging: Integrated transmission of Python objects via pluggable serializers (JSON, MsgPack, Protobuf).
  • Application Framework: Includes ServerApp with routing and middleware, plus a composable client suite for connection resilience and fleet management.

Installation

pip install pywebtransport

Quick Start

Server

import asyncio

from pywebtransport import Event, ServerApp, ServerConfig, WebTransportSession, WebTransportStream
from pywebtransport.types import EventType
from pywebtransport.utils import generate_self_signed_cert

generate_self_signed_cert(hostname="localhost")

app = ServerApp(config=ServerConfig(certfile="localhost.crt", keyfile="localhost.key"))


@app.route(path="/")
async def echo_handler(session: WebTransportSession) -> None:
    async def on_datagram(event: Event) -> None:
        if isinstance(event.data, dict) and (data := event.data.get("data")):
            await session.send_datagram(data=b"ECHO: " + data)

    async def on_stream(event: Event) -> None:
        if isinstance(event.data, dict) and (stream := event.data.get("stream")):
            if isinstance(stream, WebTransportStream):
                asyncio.create_task(handle_stream(stream))

    session.events.on(event_type=EventType.DATAGRAM_RECEIVED, handler=on_datagram)
    session.events.on(event_type=EventType.STREAM_OPENED, handler=on_stream)

    try:
        await session.events.wait_for(event_type=EventType.SESSION_CLOSED)
    finally:
        session.events.off(event_type=EventType.DATAGRAM_RECEIVED, handler=on_datagram)
        session.events.off(event_type=EventType.STREAM_OPENED, handler=on_stream)


async def handle_stream(stream: WebTransportStream) -> None:
    try:
        data = await stream.read_all()
        await stream.write_all(data=b"ECHO: " + data, end_stream=True)
    except Exception:
        pass


if __name__ == "__main__":
    app.run(host="127.0.0.1", port=4433)

Client

import asyncio
import ssl

from pywebtransport import ClientConfig, WebTransportClient
from pywebtransport.types import EventType


async def main() -> None:
    config = ClientConfig(verify_mode=ssl.CERT_NONE)

    async with WebTransportClient(config=config) as client:
        session = await client.connect(url="https://127.0.0.1:4433/")

        await session.send_datagram(data=b"Hello, Datagram!")

        event = await session.events.wait_for(event_type=EventType.DATAGRAM_RECEIVED)
        if isinstance(event.data, dict) and (data := event.data.get("data")):
            print(f"Datagram: {data!r}")

        stream = await session.create_bidirectional_stream()
        await stream.write_all(data=b"Hello, Stream!", end_stream=True)

        response = await stream.read_all()
        print(f"Stream: {response!r}")

        await session.close()


if __name__ == "__main__":
    try:
        asyncio.run(main())
    except KeyboardInterrupt:
        pass

Interoperability

Infrastructure

Endpoints

  • /echo: Bidirectional stream and datagram reflection.
  • /stats: Current session statistics and negotiated parameters.
  • /status: Global server health and aggregate metrics.
  • /webtransport/devious-baton: Devious Baton protocol state transition validation.

Sponsors

License

Distributed under the terms of the Apache License 2.0. See LICENSE for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pywebtransport-0.13.0.tar.gz (775.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pywebtransport-0.13.0-cp312-abi3-manylinux_2_34_x86_64.whl (1.0 MB view details)

Uploaded CPython 3.12+manylinux: glibc 2.34+ x86-64

File details

Details for the file pywebtransport-0.13.0.tar.gz.

File metadata

  • Download URL: pywebtransport-0.13.0.tar.gz
  • Upload date:
  • Size: 775.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for pywebtransport-0.13.0.tar.gz
Algorithm Hash digest
SHA256 a8f40a1655f14214ed176f84b552ebe4fc9a5e900638cc1730409d29252f998c
MD5 a1596e46880b14157efcffceb06495e1
BLAKE2b-256 f4a18948f08cc59ddb9261cb950f285cd30622971ef015faecc83658ed007a48

See more details on using hashes here.

File details

Details for the file pywebtransport-0.13.0-cp312-abi3-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for pywebtransport-0.13.0-cp312-abi3-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 5ae15f7ef41e925840a9a0e5bad50ed6486e40ec432dddcbf6c3713c33f6965e
MD5 3e679a1a36209bd56acf5a40a9901287
BLAKE2b-256 ab779637ea8ba9711aeff2584eedb56e046cd4570e1c1c9ada247425203070ab

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page