| 1 |
Server (:mod:`trio`) |
| 2 |
======================= |
| 3 |
|
| 4 |
.. admonition:: The :mod:`trio` API is experimental. |
| 5 |
:class: caution |
| 6 |
|
| 7 |
Please provide feedback in GitHub issues about the API, especially if you |
| 8 |
can propose a more intuitive or convenient way to start and stop a server. |
| 9 |
|
| 10 |
.. automodule:: websockets.trio.server |
| 11 |
|
| 12 |
Creating a server |
| 13 |
----------------- |
| 14 |
|
| 15 |
.. autofunction:: serve |
| 16 |
|
| 17 |
.. admonition:: ``unix_serve`` is not available in the Trio implementation. |
| 18 |
:class: note |
| 19 |
|
| 20 |
This is because Trio `does not provide`_ ``open_unix_listeners`` yet. |
| 21 |
Instead, you can create Trio listeners using Unix domain sockets then |
| 22 |
call :func:`serve` with a ``listeners`` arguments. |
| 23 |
|
| 24 |
.. _does not provide: https://github.com/python-trio/trio/issues/279 |
| 25 |
|
| 26 |
Routing connections |
| 27 |
------------------- |
| 28 |
|
| 29 |
.. automodule:: websockets.trio.router |
| 30 |
|
| 31 |
.. autofunction:: route |
| 32 |
|
| 33 |
.. admonition:: ``unix_route`` is not available in the Trio implementation. |
| 34 |
:class: note |
| 35 |
|
| 36 |
This is because ``unix_serve`` isn't available either, as explained above. |
| 37 |
|
| 38 |
.. autoclass:: Router |
| 39 |
|
| 40 |
.. currentmodule:: websockets.trio.server |
| 41 |
|
| 42 |
Running a server |
| 43 |
---------------- |
| 44 |
|
| 45 |
.. autoclass:: Server |
| 46 |
|
| 47 |
.. autoattribute:: connections |
| 48 |
|
| 49 |
.. automethod:: aclose |
| 50 |
|
| 51 |
.. autoattribute:: listeners |
| 52 |
|
| 53 |
Using a connection |
| 54 |
------------------ |
| 55 |
|
| 56 |
.. autoclass:: ServerConnection |
| 57 |
|
| 58 |
.. automethod:: respond |
| 59 |
|
| 60 |
.. automethod:: __aiter__ |
| 61 |
|
| 62 |
.. automethod:: recv |
| 63 |
|
| 64 |
.. automethod:: recv_streaming |
| 65 |
|
| 66 |
.. automethod:: send |
| 67 |
|
| 68 |
.. automethod:: aclose |
| 69 |
|
| 70 |
.. automethod:: wait_closed |
| 71 |
|
| 72 |
.. automethod:: ping |
| 73 |
|
| 74 |
.. automethod:: pong |
| 75 |
|
| 76 |
WebSocket connection objects also provide these attributes: |
| 77 |
|
| 78 |
.. autoattribute:: id |
| 79 |
|
| 80 |
.. autoattribute:: logger |
| 81 |
|
| 82 |
.. autoproperty:: local_address |
| 83 |
|
| 84 |
.. autoproperty:: remote_address |
| 85 |
|
| 86 |
.. autoattribute:: latency |
| 87 |
|
| 88 |
.. autoproperty:: state |
| 89 |
|
| 90 |
The following attributes are available after the opening handshake, |
| 91 |
once the WebSocket connection is open: |
| 92 |
|
| 93 |
.. autoattribute:: request |
| 94 |
|
| 95 |
.. autoattribute:: response |
| 96 |
|
| 97 |
.. autoproperty:: subprotocol |
| 98 |
|
| 99 |
The following attributes are available after the closing handshake, |
| 100 |
once the WebSocket connection is closed: |
| 101 |
|
| 102 |
.. autoproperty:: close_code |
| 103 |
|
| 104 |
.. autoproperty:: close_reason |
| 105 |
|
| 106 |
Broadcast |
| 107 |
--------- |
| 108 |
|
| 109 |
.. autofunction:: broadcast |
| 110 |
|
| 111 |
HTTP Basic Authentication |
| 112 |
------------------------- |
| 113 |
|
| 114 |
websockets supports HTTP Basic Authentication according to |
| 115 |
:rfc:`7235` and :rfc:`7617`. |
| 116 |
|
| 117 |
.. autofunction:: basic_auth |