Skip to content

Commit cfd1dfa

Browse files
committed
Add trio client and server.
1 parent b5ad2ae commit cfd1dfa

32 files changed

+3804
-240
lines changed

docs/conf.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
intersphinx_mapping = {
8686
"python": ("https://docs.python.org/3", None),
8787
"sesame": ("https://django-sesame.readthedocs.io/en/stable/", None),
88+
"trio": ("https://trio.readthedocs.io/en/stable/", None),
8889
"werkzeug": ("https://werkzeug.palletsprojects.com/en/stable/", None),
8990
}
9091

docs/project/changelog.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ Backwards-incompatible changes
4343
New features
4444
............
4545

46+
.. admonition:: websockets 16.0 introduces a :mod:`trio` implementation.
47+
:class: important
48+
49+
It is an alternative to the :mod:`asyncio` implementation.
50+
51+
See :func:`websockets.trio.client.connect` and
52+
:func:`websockets.trio.server.serve` for details.
53+
4654
* Validated compatibility with Python 3.14.
4755

4856
Improvements

docs/reference/asyncio/server.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ Running a server
4646

4747
.. automethod:: serve_forever
4848

49-
.. autoattribute:: sockets
49+
.. autoproperty:: sockets
5050

5151
Using a connection
5252
------------------

docs/reference/features.rst

Lines changed: 129 additions & 128 deletions
Large diffs are not rendered by default.

docs/reference/index.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,17 @@ This alternative implementation can be a good choice for clients.
3737
sync/server
3838
sync/client
3939

40+
:mod:`trio`
41+
------------
42+
43+
This is another option for servers that handle many clients concurrently.
44+
45+
.. toctree::
46+
:titlesonly:
47+
48+
trio/server
49+
trio/client
50+
4051
`Sans-I/O`_
4152
-----------
4253

docs/reference/trio/client.rst

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
Client (:mod:`trio`)
2+
=======================
3+
4+
.. automodule:: websockets.trio.client
5+
6+
Opening a connection
7+
--------------------
8+
9+
.. autofunction:: connect
10+
:async:
11+
12+
.. autofunction:: process_exception
13+
14+
Using a connection
15+
------------------
16+
17+
.. autoclass:: ClientConnection
18+
19+
.. automethod:: __aiter__
20+
21+
.. automethod:: recv
22+
23+
.. automethod:: recv_streaming
24+
25+
.. automethod:: send
26+
27+
.. automethod:: aclose
28+
29+
.. automethod:: wait_closed
30+
31+
.. automethod:: ping
32+
33+
.. automethod:: pong
34+
35+
WebSocket connection objects also provide these attributes:
36+
37+
.. autoattribute:: id
38+
39+
.. autoattribute:: logger
40+
41+
.. autoproperty:: local_address
42+
43+
.. autoproperty:: remote_address
44+
45+
.. autoattribute:: latency
46+
47+
.. autoproperty:: state
48+
49+
The following attributes are available after the opening handshake,
50+
once the WebSocket connection is open:
51+
52+
.. autoattribute:: request
53+
54+
.. autoattribute:: response
55+
56+
.. autoproperty:: subprotocol
57+
58+
The following attributes are available after the closing handshake,
59+
once the WebSocket connection is closed:
60+
61+
.. autoproperty:: close_code
62+
63+
.. autoproperty:: close_reason

docs/reference/trio/common.rst

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
:orphan:
2+
3+
Both sides (:mod:`trio`)
4+
===========================
5+
6+
.. automodule:: websockets.trio.connection
7+
8+
.. autoclass:: Connection
9+
10+
.. automethod:: __aiter__
11+
12+
.. automethod:: recv
13+
14+
.. automethod:: recv_streaming
15+
16+
.. automethod:: send
17+
18+
.. automethod:: aclose
19+
20+
.. automethod:: wait_closed
21+
22+
.. automethod:: ping
23+
24+
.. automethod:: pong
25+
26+
WebSocket connection objects also provide these attributes:
27+
28+
.. autoattribute:: id
29+
30+
.. autoattribute:: logger
31+
32+
.. autoproperty:: local_address
33+
34+
.. autoproperty:: remote_address
35+
36+
.. autoattribute:: latency
37+
38+
.. autoproperty:: state
39+
40+
The following attributes are available after the opening handshake,
41+
once the WebSocket connection is open:
42+
43+
.. autoattribute:: request
44+
45+
.. autoattribute:: response
46+
47+
.. autoproperty:: subprotocol
48+
49+
The following attributes are available after the closing handshake,
50+
once the WebSocket connection is closed:
51+
52+
.. autoproperty:: close_code
53+
54+
.. autoproperty:: close_reason

docs/reference/trio/server.rst

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
Server (:mod:`trio`)
2+
=======================
3+
4+
.. automodule:: websockets.trio.server
5+
6+
Creating a server
7+
-----------------
8+
9+
.. autofunction:: serve
10+
:async:
11+
12+
.. currentmodule:: websockets.trio.server
13+
14+
Running a server
15+
----------------
16+
17+
.. autoclass:: Server
18+
19+
.. autoattribute:: connections
20+
21+
.. automethod:: aclose
22+
23+
.. autoattribute:: listeners
24+
25+
Using a connection
26+
------------------
27+
28+
.. autoclass:: ServerConnection
29+
30+
.. automethod:: __aiter__
31+
32+
.. automethod:: recv
33+
34+
.. automethod:: recv_streaming
35+
36+
.. automethod:: send
37+
38+
.. automethod:: aclose
39+
40+
.. automethod:: wait_closed
41+
42+
.. automethod:: ping
43+
44+
.. automethod:: pong
45+
46+
.. automethod:: respond
47+
48+
WebSocket connection objects also provide these attributes:
49+
50+
.. autoattribute:: id
51+
52+
.. autoattribute:: logger
53+
54+
.. autoproperty:: local_address
55+
56+
.. autoproperty:: remote_address
57+
58+
.. autoattribute:: latency
59+
60+
.. autoproperty:: state
61+
62+
The following attributes are available after the opening handshake,
63+
once the WebSocket connection is open:
64+
65+
.. autoattribute:: request
66+
67+
.. autoattribute:: response
68+
69+
.. autoproperty:: subprotocol
70+
71+
The following attributes are available after the closing handshake,
72+
once the WebSocket connection is closed:
73+
74+
.. autoproperty:: close_code
75+
76+
.. autoproperty:: close_reason
77+
78+
HTTP Basic Authentication
79+
-------------------------
80+
81+
websockets supports HTTP Basic Authentication according to
82+
:rfc:`7235` and :rfc:`7617`.
83+
84+
.. autofunction:: basic_auth

example/trio/client.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/env python
2+
3+
"""Client example using the trio API."""
4+
5+
import logging
6+
logging.basicConfig(level=logging.DEBUG)
7+
8+
import trio
9+
10+
from websockets.trio.client import connect
11+
12+
13+
async def hello():
14+
async with connect("ws://localhost:8765") as websocket:
15+
name = input("What's your name? ")
16+
17+
await websocket.send(name)
18+
print(f">>> {name}")
19+
20+
greeting = await websocket.recv()
21+
print(f"<<< {greeting}")
22+
23+
24+
if __name__ == "__main__":
25+
trio.run(hello)

example/trio/echo.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/usr/bin/env python
2+
3+
"""Echo server using the trio API."""
4+
5+
import trio
6+
from websockets.trio.server import serve
7+
8+
9+
async def echo(websocket):
10+
async for message in websocket:
11+
await websocket.send(message)
12+
13+
14+
if __name__ == "__main__":
15+
trio.run(serve, echo, 8765)

0 commit comments

Comments
 (0)