| 1 |
websockets |
| 2 |
========== |
| 3 |
|
| 4 |
|licence| |version| |pyversions| |
| 5 |
|
| 6 |
.. |version| image:: https://img.shields.io/pypi/v/websockets.svg |
| 7 |
:target: https://pypi.python.org/pypi/websockets |
| 8 |
|
| 9 |
.. |pyversions| image:: https://img.shields.io/pypi/pyversions/websockets.svg |
| 10 |
:target: https://pypi.python.org/pypi/websockets |
| 11 |
|
| 12 |
.. |licence| image:: https://img.shields.io/pypi/l/websockets.svg |
| 13 |
:target: https://pypi.python.org/pypi/websockets |
| 14 |
|
| 15 |
websockets is a library for building WebSocket_ servers and clients in Python |
| 16 |
with a focus on correctness, simplicity, robustness, and performance. |
| 17 |
|
| 18 |
.. _WebSocket: https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API |
| 19 |
|
| 20 |
It supports several network I/O and control flow paradigms. |
| 21 |
|
| 22 |
1. The default implementation builds upon :mod:`asyncio`, Python's built-in |
| 23 |
asynchronous I/O library. It provides an elegant coroutine-based API. It's |
| 24 |
ideal for servers that handle many client connections. |
| 25 |
|
| 26 |
2. The :mod:`threading` implementation is a good alternative for clients, |
| 27 |
especially if you aren't familiar with :mod:`asyncio`. It may also be used |
| 28 |
for servers that don't handle too many client connections. |
| 29 |
|
| 30 |
3. The :mod:`trio` implementation is functionally equivalent to the |
| 31 |
:mod:`asyncio` implementation and a great choice if you appreciate the |
| 32 |
simpler design or stronger correctness guarantees of Trio_. |
| 33 |
|
| 34 |
4. The `Sans-I/O`_ implementation is designed for integrating in third-party |
| 35 |
libraries, typically application servers, in addition being used internally |
| 36 |
by websockets. |
| 37 |
|
| 38 |
.. _Trio: https://trio.readthedocs.io/ |
| 39 |
|
| 40 |
.. _Sans-I/O: https://sans-io.readthedocs.io/ |
| 41 |
|
| 42 |
Refer to the :doc:`feature support matrices <reference/features>` for the full |
| 43 |
list of features provided by each implementation. |
| 44 |
|
| 45 |
.. admonition:: The :mod:`asyncio` implementation was rewritten. |
| 46 |
:class: tip |
| 47 |
|
| 48 |
The new implementation in ``websockets.asyncio`` builds upon the Sans-I/O |
| 49 |
implementation. It adds features that were impossible to provide in the |
| 50 |
original design. It was introduced in version 13.0. |
| 51 |
|
| 52 |
The historical implementation in ``websockets.legacy`` traces its roots to |
| 53 |
early versions of websockets. While it's stable and robust, it was deprecated |
| 54 |
in version 14.0 and it will be removed by 2030. |
| 55 |
|
| 56 |
The new implementation provides the same features as the historical |
| 57 |
implementation, and then some. If you're using the historical implementation, |
| 58 |
you should :doc:`upgrade to the new implementation <howto/upgrade>`. |
| 59 |
|
| 60 |
Here's an echo server and corresponding client. |
| 61 |
|
| 62 |
.. tab:: asyncio |
| 63 |
|
| 64 |
.. literalinclude:: ../example/asyncio/echo.py |
| 65 |
|
| 66 |
.. tab:: threading |
| 67 |
|
| 68 |
.. literalinclude:: ../example/sync/echo.py |
| 69 |
|
| 70 |
.. tab:: trio |
| 71 |
|
| 72 |
.. literalinclude:: ../example/trio/echo.py |
| 73 |
|
| 74 |
.. tab:: asyncio |
| 75 |
:new-set: |
| 76 |
|
| 77 |
.. literalinclude:: ../example/asyncio/hello.py |
| 78 |
|
| 79 |
.. tab:: threading |
| 80 |
|
| 81 |
.. literalinclude:: ../example/sync/hello.py |
| 82 |
|
| 83 |
.. tab:: trio |
| 84 |
|
| 85 |
.. literalinclude:: ../example/trio/hello.py |
| 86 |
|
| 87 |
Don't worry about the opening and closing handshakes, pings and pongs, or any |
| 88 |
other behavior described in the WebSocket specification. websockets takes care |
| 89 |
of this under the hood so you can focus on your application! |
| 90 |
|
| 91 |
Also, websockets provides an interactive client: |
| 92 |
|
| 93 |
.. code-block:: console |
| 94 |
|
| 95 |
$ websockets ws://localhost:8765/ |
| 96 |
Connected to ws://localhost:8765/. |
| 97 |
> Hello world! |
| 98 |
< Hello world! |
| 99 |
Connection closed: 1000 (OK). |
| 100 |
|
| 101 |
Do you like it? :doc:`Let's dive in! <intro/index>` |
| 102 |
|
| 103 |
.. toctree:: |
| 104 |
:hidden: |
| 105 |
|
| 106 |
intro/index |
| 107 |
howto/index |
| 108 |
deploy/index |
| 109 |
faq/index |
| 110 |
reference/index |
| 111 |
topics/index |
| 112 |
project/index |