Skip to main content

A high-performance, async-native WebTransport implementation for Python.

Project description

PyWebTransport

PyPI version Python License: MIT Build Status Coverage

A high-performance, async-native WebTransport implementation for Python.

Features

  • Full WebTransport protocol implementation (bidirectional streams, unidirectional streams, datagrams)
  • High-performance async client and server implementations
  • Production-ready connection pooling and load balancing
  • Comprehensive monitoring and debugging capabilities
  • Type-safe API with complete type annotations
  • Extensive test coverage (unit, integration, end-to-end)

Installation

pip install pywebtransport

For development installation:

git clone https://github.com/lemonsterfy/pywebtransport.git
cd pywebtransport
pip install -e ".[dev]"

Quick Start

Server

# server.py
import asyncio

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

generate_self_signed_cert("localhost")

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


async def handle_datagrams(session: WebTransportSession) -> None:
    try:
        while True:
            data = await session.datagrams.receive()
            await session.datagrams.send(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(b"ECHO: " + data)
    except (ConnectionError, SessionError, asyncio.CancelledError):
        pass


@app.route("/")
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.create(verify_mode=ssl.CERT_NONE)

    async with WebTransportClient.create(config=config) as client:
        async with await client.connect("https://127.0.0.1:4433/") as session:
            print("Connection established. Testing datagrams...")
            await session.datagrams.send(b"Hello, Datagram!")
            response = await session.datagrams.receive()
            print(f"Datagram echo: {response!r}\n")

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


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

Documentation

Requirements

  • Python 3.8+
  • asyncio support
  • TLS 1.3

Dependencies:

  • aioquic >= 1.2.0
  • cryptography >= 45.0.4
  • typing-extensions >= 4.14.0 (Python < 3.10)

Contributing

Contributions are welcome! Please read our Contributing Guide for details.

Development Setup:

git clone https://github.com/lemonsterfy/pywebtransport.git
cd pywebtransport
pip install -e ".[dev]"
pre-commit install
pytest

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.1.1.tar.gz (90.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.1.1-py3-none-any.whl (119.6 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for pywebtransport-0.1.1.tar.gz
Algorithm Hash digest
SHA256 76bfff1328263c4115c6cc44bfd6498b8dbf48a6e0b7b2c870c2779f38ef6f59
MD5 3f9267a01f472a6bbe99c3f743022837
BLAKE2b-256 3209523e2e2666c911c7b377f811c1008bea1b078731202471232d01d24ec2b8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pywebtransport-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f47c2d4349ca9063f2095cced6707836a7a430efd8cdff12b38e98c57c080517
MD5 5eaefde3f25ae09dcdbba0c43c2e79af
BLAKE2b-256 70e185cea0a29c9b0fec8b405b1ab7cc4081a153c6d0288a5215953f9f437c5f

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