Skip to content

Commit 8881b4a

Browse files
committed
Add trio client and server.
Also uniformize code & tests with other implementations.
1 parent b0d9376 commit 8881b4a

32 files changed

+3749
-225
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/index.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ Here's an echo server and corresponding client.
7070

7171
.. literalinclude:: ../example/sync/echo.py
7272

73+
.. tab:: trio
74+
75+
.. literalinclude:: ../example/trio/echo.py
76+
7377
.. tab:: asyncio
7478
:new-set:
7579

@@ -79,6 +83,11 @@ Here's an echo server and corresponding client.
7983

8084
.. literalinclude:: ../example/sync/hello.py
8185

86+
.. tab:: trio
87+
88+
.. literalinclude:: ../example/trio/hello.py
89+
90+
8291
Don't worry about the opening and closing handshakes, pings and pongs, or any
8392
other behavior described in the WebSocket specification. websockets takes care
8493
of this under the hood so you can focus on your application!

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/asyncio/client.py

100644100755
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
"""Client example using the asyncio API."""
44

55
import asyncio
6-
76
from websockets.asyncio.client import connect
87

98

0 commit comments

Comments
 (0)