Skip to main content

The canonical, async-native WebTransport stack for Python.

Project description

PyWebTransport

PyPI version Python Versions License: MIT Build Status Coverage Docs

The canonical, async-native WebTransport stack for Python.

Features

  • Full Async Support: Built from the ground up on asyncio for high-performance, non-blocking I/O.
  • High-Level Frameworks: Includes a ServerApp with routing and middleware, and a versatile WebTransportClient with helpers for fleet management, auto-reconnection, and browser-like navigation.
  • Advanced Messaging: Built-in managers for Pub/Sub and RPC (JSON-RPC 2.0 compliant), plus pluggable serializers (JSON, MsgPack, Protobuf) for structured data.
  • Complete Protocol Implementation: Full support for bidirectional and unidirectional streams, as well as unreliable datagrams.
  • Lifecycle and Resource Management: Robust, async context-managed components for handling connections, sessions, streams, monitoring, pooling, and resource management.
  • Event-Driven Architecture: A powerful EventEmitter system for decoupled, asynchronous communication between components.
  • Type-Safe and Tested: A fully type-annotated API with extensive test coverage (unit, integration, E2E) to ensure reliability and maintainability.

Installation

pip install pywebtransport

For more detailed instructions, including virtual environments and platform-specific notes, see the Installation Guide.

Quick Start

Server

# server.py
import asyncio

from pywebtransport import (
    ConnectionError,
    ServerApp,
    ServerConfig,
    SessionError,
    WebTransportSession,
    WebTransportStream,
)
from pywebtransport.utils import generate_self_signed_cert

generate_self_signed_cert(hostname="localhost")

app = ServerApp(
    config=ServerConfig(
        certfile="localhost.crt",
        keyfile="localhost.key",
        initial_max_data=1024 * 1024,
        initial_max_streams_bidi=10,
    )
)


async def handle_datagrams(session: WebTransportSession) -> None:
    try:
        datagram_transport = await session.create_datagram_transport()
        while True:
            data = await datagram_transport.receive()
            await datagram_transport.send(data=b"ECHO: " + data)
    except (ConnectionError, SessionError, asyncio.CancelledError):
        pass


async def handle_streams(session: WebTransportSession) -> None:
    try:
        async for stream in session.incoming_streams():
            if isinstance(stream, WebTransportStream):
                data = await stream.read_all()
                await stream.write_all(data=b"ECHO: " + data)
    except (ConnectionError, SessionError, asyncio.CancelledError):
        pass


@app.route(path="/")
async def echo_handler(session: WebTransportSession) -> None:
    datagram_task = asyncio.create_task(handle_datagrams(session))
    stream_task = asyncio.create_task(handle_streams(session))
    try:
        await session.wait_closed()
    finally:
        datagram_task.cancel()
        stream_task.cancel()


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

Client

# client.py
import asyncio
import ssl

from pywebtransport import ClientConfig, WebTransportClient


async def main() -> None:
    config = ClientConfig(
        verify_mode=ssl.CERT_NONE,
        initial_max_data=1024 * 1024,
        initial_max_streams_bidi=10,
    )

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

        print("Connection established. Testing datagrams...")
        datagram_transport = await session.create_datagram_transport()
        await datagram_transport.send(data=b"Hello, Datagram!")
        response = await datagram_transport.receive()
        print(f"Datagram echo: {response!r}\n")

        print("Testing streams...")
        stream = await session.create_bidirectional_stream()
        await stream.write_all(data=b"Hello, Stream!")
        response = await stream.read_all()
        print(f"Stream echo: {response!r}")

        await session.close()


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

Documentation

Requirements

  • Python 3.11+
  • asyncio support
  • TLS 1.3

Dependencies:

  • aioquic >= 1.2.0
  • cryptography >= 45.0.4

Contributing

Contributions are welcome! Please read our Contributing Guide for details on the development setup, testing, and pull request process.

Development Setup:

git clone https://github.com/lemonsterfy/pywebtransport.git
cd pywebtransport
pip install -r dev-requirements.txt
pip install -e .
tox

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgments

Support

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.8.0.tar.gz (112.9 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.8.0-py3-none-any.whl (140.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pywebtransport-0.8.0.tar.gz
  • Upload date:
  • Size: 112.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.28.1

File hashes

Hashes for pywebtransport-0.8.0.tar.gz
Algorithm Hash digest
SHA256 5589130044e0536b5bfdf4da75f2d510a00ba9019c0dfaea56307eefc27a893e
MD5 eb1fb2ab48292dd2c8ee5350bc896018
BLAKE2b-256 25c95fb51bcfeb2e2b81b91c4c8fccc5f936c17926514962d5688e8dea53ee0e

See more details on using hashes here.

File details

Details for the file pywebtransport-0.8.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pywebtransport-0.8.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ca89308621a6c443e3bb92baf3f34649bcc4f740dd3c655f6db6b918882a894f
MD5 fc1f0f5bd1a702b6649e81072350077f
BLAKE2b-256 11468649bc66b5499c96e36ea0e63980c19d7be1bd0441ed76364df9ce5bda6e

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