| 1 |
websockets |
| 2 |
========== |
| 3 |
|
| 4 |
|licence| |version| |pyversions| |tests| |docs| |openssf| |
| 5 |
|
| 6 |
.. |licence| image:: https://img.shields.io/pypi/l/websockets.svg |
| 7 |
:target: https://pypi.python.org/pypi/websockets |
| 8 |
|
| 9 |
.. |version| image:: https://img.shields.io/pypi/v/websockets.svg |
| 10 |
:target: https://pypi.python.org/pypi/websockets |
| 11 |
|
| 12 |
.. |pyversions| image:: https://img.shields.io/pypi/pyversions/websockets.svg |
| 13 |
:target: https://pypi.python.org/pypi/websockets |
| 14 |
|
| 15 |
.. |tests| image:: https://img.shields.io/github/checks-status/python-websockets/websockets/main?label=tests |
| 16 |
:target: https://github.com/python-websockets/websockets/actions/workflows/tests.yml |
| 17 |
|
| 18 |
.. |docs| image:: https://img.shields.io/readthedocs/websockets.svg |
| 19 |
:target: https://websockets.readthedocs.io/ |
| 20 |
|
| 21 |
.. |openssf| image:: https://bestpractices.coreinfrastructure.org/projects/6475/badge |
| 22 |
:target: https://bestpractices.coreinfrastructure.org/projects/6475 |
| 23 |
|
| 24 |
websockets is a library for building WebSocket_ servers and clients in Python |
| 25 |
with a focus on correctness, simplicity, robustness, and performance. |
| 26 |
|
| 27 |
.. _WebSocket: https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API |
| 28 |
|
| 29 |
It supports several network I/O and control flow paradigms: |
| 30 |
|
| 31 |
1. The default implementation builds upon :mod:`asyncio`, Python's standard |
| 32 |
asynchronous I/O framework. It provides an elegant coroutine-based API. It's |
| 33 |
ideal for servers that handle many clients concurrently. |
| 34 |
2. The :mod:`threading` implementation is a good alternative for clients, |
| 35 |
especially if you aren't familiar with :mod:`asyncio`. It may also be used |
| 36 |
for servers that don't need to serve many clients. |
| 37 |
3. The `Sans-I/O`_ implementation is designed for integrating in third-party |
| 38 |
libraries, typically application servers, in addition being used internally |
| 39 |
by websockets. |
| 40 |
|
| 41 |
.. _Sans-I/O: https://sans-io.readthedocs.io/ |
| 42 |
|
| 43 |
Here's an echo server with the :mod:`asyncio` API: |
| 44 |
|
| 45 |
.. literalinclude:: ../example/echo.py |
| 46 |
|
| 47 |
Here's how a client sends and receives messages with the :mod:`threading` API: |
| 48 |
|
| 49 |
.. literalinclude:: ../example/hello.py |
| 50 |
|
| 51 |
Don't worry about the opening and closing handshakes, pings and pongs, or any |
| 52 |
other behavior described in the WebSocket specification. websockets takes care |
| 53 |
of this under the hood so you can focus on your application! |
| 54 |
|
| 55 |
Also, websockets provides an interactive client: |
| 56 |
|
| 57 |
.. code-block:: console |
| 58 |
|
| 59 |
$ python -m websockets ws://localhost:8765/ |
| 60 |
Connected to ws://localhost:8765/. |
| 61 |
> Hello world! |
| 62 |
< Hello world! |
| 63 |
Connection closed: 1000 (OK). |
| 64 |
|
| 65 |
Do you like it? :doc:`Let's dive in! <intro/index>` |
| 66 |
|
| 67 |
.. toctree:: |
| 68 |
:hidden: |
| 69 |
|
| 70 |
intro/index |
| 71 |
howto/index |
| 72 |
faq/index |
| 73 |
reference/index |
| 74 |
topics/index |
| 75 |
project/index |