Skip to main content

trio-websockets

rtd pypi-v pypi-pyversions pypi-l pypi-wheel

What is trio-websockets?

trio-websockets is a library for building WebSocket servers and clients in Python, based on trio, an asynchronous I/O framework.

Currently a work in progress. The status is:

  • The client-side works.

  • The server-side does not.

History of the library

The code is originally forked from aaugustin’s websockets library for asyncio, with the following changes:

  • Rip out all asyncio things, replace with trio.

  • Rip out the websocket protocol code, replace with wsproto.

What remains of the original websockets library itself?

  • Most of the remaining code seems to be additional error checking around connection state. Rather than a “trio.BrokenStreamError”, you will receive a ConnectionClosed exception when trying to write on a closed connection.

  • The same/very similar interface to websockets, which might be slightly more user-friendly than a raw wsproto connection (say, exposing attributes like .subprotocol, which wsproto passes along during the ConnectionEstablished event).

TODO

  • Port the server-side.

  • Make the examples run.

  • Make the tests run.

  • Support for curio.

  • Cleanup documentation and readme.

  • Experiment with a different architecture, using reader/writer tasks.

How to use it

Here’s a client that says “Hello world!”:

#!/usr/bin/env python

import trio
import trio_websockets

async def hello(uri):
    async with trio_websockets.connect(uri) as websocket:
        await websocket.send("Hello world!")

trio.run(hello, 'ws://localhost:8765')

And here’s an echo server (for Python ≥ 3.6):

#!/usr/bin/env python

import trio
import trio_websockets

async def echo(websocket, path):
    async for message in websocket:
        await websocket.send(message)

trio.run(trio_websockets.serve, echo, 'localhost', 8765)

Does that look good? Start here.

What else?

Bug reports, patches and suggestions welcome! Just open an issue or send a pull request.

trio-websockets is released under the BSD license.

Metadata

Release files for trio-websockets 0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for trio-websockets 0.2
File Size Uploaded
trio-websockets-0.2.tar.gz 34.7 kB Details

Release files / trio-websockets-0.2.tar.gz

Download URL trio-websockets-0.2.tar.gz
Size 34.7 kB
Tags Source
SHA-256 checksum
How to use checksums
8107e7dc42581c4c01c189a3f58b789f208dbce647e44d6f6caff343e2bd5081
BLAKE2b-256 checksum
How to use checksums
a08831c24c23c9be0c4a4a17c55a8369e8ee37390b8608bf0e0262131d38e792
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via Python-urllib/3.7

Release history Release notifications | RSS feed

This release

0.2 This release

1 release file

0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page