| 1 |
Server (:mod:`asyncio`) |
| 2 |
======================= |
| 3 |
|
| 4 |
.. automodule:: websockets.server |
| 5 |
|
| 6 |
Starting a server |
| 7 |
----------------- |
| 8 |
|
| 9 |
.. autofunction:: serve(ws_handler, host=None, port=None, *, create_protocol=None, logger=None, compression="deflate", origins=None, extensions=None, subprotocols=None, extra_headers=None, server_header="Python/x.y.z websockets/X.Y", process_request=None, select_subprotocol=None, ping_interval=20, ping_timeout=20, close_timeout=10, max_size=2 ** 20, max_queue=2 ** 5, read_limit=2 ** 16, write_limit=2 ** 16, **kwds) |
| 10 |
:async: |
| 11 |
|
| 12 |
.. autofunction:: unix_serve(ws_handler, path=None, *, create_protocol=None, logger=None, compression="deflate", origins=None, extensions=None, subprotocols=None, extra_headers=None, server_header="Python/x.y.z websockets/X.Y", process_request=None, select_subprotocol=None, ping_interval=20, ping_timeout=20, close_timeout=10, max_size=2 ** 20, max_queue=2 ** 5, read_limit=2 ** 16, write_limit=2 ** 16, **kwds) |
| 13 |
:async: |
| 14 |
|
| 15 |
Stopping a server |
| 16 |
----------------- |
| 17 |
|
| 18 |
.. autoclass:: WebSocketServer |
| 19 |
|
| 20 |
.. automethod:: close |
| 21 |
|
| 22 |
.. automethod:: wait_closed |
| 23 |
|
| 24 |
.. automethod:: get_loop |
| 25 |
|
| 26 |
.. automethod:: is_serving |
| 27 |
|
| 28 |
.. automethod:: start_serving |
| 29 |
|
| 30 |
.. automethod:: serve_forever |
| 31 |
|
| 32 |
.. autoattribute:: sockets |
| 33 |
|
| 34 |
Using a connection |
| 35 |
------------------ |
| 36 |
|
| 37 |
.. autoclass:: WebSocketServerProtocol(ws_handler, ws_server, *, logger=None, origins=None, extensions=None, subprotocols=None, extra_headers=None, server_header="Python/x.y.z websockets/X.Y", process_request=None, select_subprotocol=None, ping_interval=20, ping_timeout=20, close_timeout=10, max_size=2 ** 20, max_queue=2 ** 5, read_limit=2 ** 16, write_limit=2 ** 16) |
| 38 |
|
| 39 |
.. automethod:: recv |
| 40 |
|
| 41 |
.. automethod:: send |
| 42 |
|
| 43 |
.. automethod:: close |
| 44 |
|
| 45 |
.. automethod:: wait_closed |
| 46 |
|
| 47 |
.. automethod:: ping |
| 48 |
|
| 49 |
.. automethod:: pong |
| 50 |
|
| 51 |
You can customize the opening handshake in a subclass by overriding these methods: |
| 52 |
|
| 53 |
.. automethod:: process_request |
| 54 |
|
| 55 |
.. automethod:: select_subprotocol |
| 56 |
|
| 57 |
WebSocket connection objects also provide these attributes: |
| 58 |
|
| 59 |
.. autoattribute:: id |
| 60 |
|
| 61 |
.. autoattribute:: logger |
| 62 |
|
| 63 |
.. autoproperty:: local_address |
| 64 |
|
| 65 |
.. autoproperty:: remote_address |
| 66 |
|
| 67 |
.. autoproperty:: open |
| 68 |
|
| 69 |
.. autoproperty:: closed |
| 70 |
|
| 71 |
.. autoattribute:: latency |
| 72 |
|
| 73 |
The following attributes are available after the opening handshake, |
| 74 |
once the WebSocket connection is open: |
| 75 |
|
| 76 |
.. autoattribute:: path |
| 77 |
|
| 78 |
.. autoattribute:: request_headers |
| 79 |
|
| 80 |
.. autoattribute:: response_headers |
| 81 |
|
| 82 |
.. autoattribute:: subprotocol |
| 83 |
|
| 84 |
The following attributes are available after the closing handshake, |
| 85 |
once the WebSocket connection is closed: |
| 86 |
|
| 87 |
.. autoproperty:: close_code |
| 88 |
|
| 89 |
.. autoproperty:: close_reason |
| 90 |
|
| 91 |
|
| 92 |
Basic authentication |
| 93 |
-------------------- |
| 94 |
|
| 95 |
.. automodule:: websockets.auth |
| 96 |
|
| 97 |
websockets supports HTTP Basic Authentication according to |
| 98 |
:rfc:`7235` and :rfc:`7617`. |
| 99 |
|
| 100 |
.. autofunction:: basic_auth_protocol_factory |
| 101 |
|
| 102 |
.. autoclass:: BasicAuthWebSocketServerProtocol |
| 103 |
|
| 104 |
.. autoattribute:: realm |
| 105 |
|
| 106 |
.. autoattribute:: username |
| 107 |
|
| 108 |
.. automethod:: check_credentials |
| 109 |
|
| 110 |
Broadcast |
| 111 |
--------- |
| 112 |
|
| 113 |
.. autofunction:: websockets.broadcast |