Skip to content

Commit 635cb5d

Browse files
committed
[WIP] Add trio client and server.
1 parent a71e5ba commit 635cb5d

26 files changed

+3458
-205
lines changed

Makefile

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@ tests:
1818
python -m unittest
1919

2020
coverage:
21-
coverage run --source src/websockets,tests -m unittest
21+
coverage run --source src/websockets,tests -m unittest tests/trio/test_client.py
2222
coverage html
23-
coverage report --show-missing --fail-under=100
2423

2524
maxi_cov:
2625
python tests/maxi_cov.py

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/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: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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:: unix_connect
13+
.. :async:
14+
15+
.. .. autofunction:: process_exception
16+
17+
.. Using a connection
18+
.. ------------------
19+
20+
.. .. autoclass:: ClientConnection
21+
22+
.. .. automethod:: __aiter__
23+
24+
.. .. automethod:: recv
25+
26+
.. .. automethod:: recv_streaming
27+
28+
.. .. automethod:: send
29+
30+
.. .. automethod:: close
31+
32+
.. .. automethod:: wait_closed
33+
34+
.. .. automethod:: ping
35+
36+
.. .. automethod:: pong
37+
38+
.. WebSocket connection objects also provide these attributes:
39+
40+
.. .. autoattribute:: id
41+
42+
.. .. autoattribute:: logger
43+
44+
.. .. autoproperty:: local_address
45+
46+
.. .. autoproperty:: remote_address
47+
48+
.. .. autoattribute:: latency
49+
50+
.. .. autoproperty:: state
51+
52+
.. The following attributes are available after the opening handshake,
53+
.. once the WebSocket connection is open:
54+
55+
.. .. autoattribute:: request
56+
57+
.. .. autoattribute:: response
58+
59+
.. .. autoproperty:: subprotocol
60+
61+
.. The following attributes are available after the closing handshake,
62+
.. once the WebSocket connection is closed:
63+
64+
.. .. autoproperty:: close_code
65+
66+
.. .. autoproperty:: close_reason

docs/reference/trio/common.rst

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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:: receive
15+
16+
.. automethod:: recv_streaming
17+
18+
.. automethod:: send
19+
20+
.. automethod:: close
21+
22+
.. automethod:: aclose
23+
24+
.. automethod:: wait_closed
25+
26+
.. automethod:: ping
27+
28+
.. automethod:: pong
29+
30+
WebSocket connection objects also provide these attributes:
31+
32+
.. autoattribute:: id
33+
34+
.. autoattribute:: logger
35+
36+
.. autoproperty:: local_address
37+
38+
.. autoproperty:: remote_address
39+
40+
.. autoattribute:: latency
41+
42+
.. autoproperty:: state
43+
44+
The following attributes are available after the opening handshake,
45+
once the WebSocket connection is open:
46+
47+
.. autoattribute:: request
48+
49+
.. autoattribute:: response
50+
51+
.. autoproperty:: subprotocol
52+
53+
The following attributes are available after the closing handshake,
54+
once the WebSocket connection is closed:
55+
56+
.. autoproperty:: close_code
57+
58+
.. autoproperty:: close_reason

docs/reference/trio/server.rst

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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+
.. .. autofunction:: unix_serve
13+
.. :async:
14+
15+
.. Routing connections
16+
.. -------------------
17+
18+
.. .. automodule:: websockets.trio.router
19+
20+
.. .. autofunction:: route
21+
.. :async:
22+
23+
.. .. autofunction:: unix_route
24+
.. :async:
25+
26+
.. .. autoclass:: Router
27+
28+
.. .. currentmodule:: websockets.trio.server
29+
30+
.. Running a server
31+
.. ----------------
32+
33+
.. .. autoclass:: Server
34+
35+
.. .. autoattribute:: connections
36+
37+
.. .. automethod:: close
38+
39+
.. .. automethod:: wait_closed
40+
41+
.. .. automethod:: get_loop
42+
43+
.. .. automethod:: is_serving
44+
45+
.. .. automethod:: start_serving
46+
47+
.. .. automethod:: serve_forever
48+
49+
.. .. autoattribute:: sockets
50+
51+
.. Using a connection
52+
.. ------------------
53+
54+
.. .. autoclass:: ServerConnection
55+
56+
.. .. automethod:: __aiter__
57+
58+
.. .. automethod:: recv
59+
60+
.. .. automethod:: recv_streaming
61+
62+
.. .. automethod:: send
63+
64+
.. .. automethod:: close
65+
66+
.. .. automethod:: wait_closed
67+
68+
.. .. automethod:: ping
69+
70+
.. .. automethod:: pong
71+
72+
.. .. automethod:: respond
73+
74+
.. WebSocket connection objects also provide these attributes:
75+
76+
.. .. autoattribute:: id
77+
78+
.. .. autoattribute:: logger
79+
80+
.. .. autoproperty:: local_address
81+
82+
.. .. autoproperty:: remote_address
83+
84+
.. .. autoattribute:: latency
85+
86+
.. .. autoproperty:: state
87+
88+
.. The following attributes are available after the opening handshake,
89+
.. once the WebSocket connection is open:
90+
91+
.. .. autoattribute:: request
92+
93+
.. .. autoattribute:: response
94+
95+
.. .. autoproperty:: subprotocol
96+
97+
.. The following attributes are available after the closing handshake,
98+
.. once the WebSocket connection is closed:
99+
100+
.. .. autoproperty:: close_code
101+
102+
.. .. autoproperty:: close_reason
103+
104+
.. HTTP Basic Authentication
105+
.. -------------------------
106+
107+
.. websockets supports HTTP Basic Authentication according to
108+
.. :rfc:`7235` and :rfc:`7617`.
109+
110+
.. .. autofunction:: basic_auth

example/trio/client.py

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