Debian Codegraph @

README.rst

raw
1 .. image:: logo/horizontal.svg
2 :width: 480px
3 :alt: websockets
4
5 |licence| |version| |pyversions|
6
7 .. |licence| image:: https://img.shields.io/pypi/l/websockets.svg
8 :target: https://pypi.python.org/pypi/websockets
9
10 .. |version| image:: https://img.shields.io/pypi/v/websockets.svg
11 :target: https://pypi.python.org/pypi/websockets
12
13 .. |pyversions| image:: https://img.shields.io/pypi/pyversions/websockets.svg
14 :target: https://pypi.python.org/pypi/websockets
15
16 What is ``websockets``?
17 -----------------------
18
19 websockets is a library for building WebSocket_ servers and clients in Python
20 with a focus on correctness, simplicity, robustness, and performance.
21
22 .. _WebSocket: https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API
23
24 Built on top of ``asyncio``, Python's standard asynchronous I/O framework, the
25 default implementation provides an elegant coroutine-based API.
26
27 Implementations on top of ``threading`` and ``trio`` are also provided, as well
28 as a Sans-I/O layer for integration in third-party projects.
29
30 `Documentation is available on Read the Docs. <https://websockets.readthedocs.io/>`_
31
32 .. copy-pasted because GitHub doesn't support the include directive
33
34 Here's an echo server with the ``asyncio`` API:
35
36 .. code:: python
37
38 #!/usr/bin/env python
39
40 import asyncio
41 from websockets.asyncio.server import serve
42
43 async def echo(websocket):
44 async for message in websocket:
45 await websocket.send(message)
46
47 async def main():
48 server = await serve(echo, "localhost", 8765)
49 await server.serve_forever()
50
51 asyncio.run(main())
52
53 Here's how a client sends and receives messages with the ``threading`` API:
54
55 .. code:: python
56
57 #!/usr/bin/env python
58
59 from websockets.sync.client import connect
60
61 def hello():
62 with connect("ws://localhost:8765") as websocket:
63 websocket.send("Hello world!")
64 message = websocket.recv()
65 print(f"Received: {message}")
66
67 hello()
68
69
70 Does that look good?
71
72 `Get started with the tutorial! <https://websockets.readthedocs.io/en/stable/intro/index.html>`_
73
74 .. raw:: html
75
76 <hr>
77 <img align="left" height="150" width="150" src="https://raw.githubusercontent.com/python-websockets/websockets/main/logo/tidelift.png">
78 <h3 align="center"><i>websockets for enterprise</i></h3>
79 <p align="center"><i>Available as part of the Tidelift Subscription</i></p>
80 <p align="center"><i>The maintainers of websockets and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. <a href="https://tidelift.com/subscription/pkg/pypi-websockets?utm_source=pypi-websockets&utm_medium=referral&utm_campaign=readme">Learn more.</a></i></p>
81 <hr>
82 <p>(If you contribute to <code>websockets</code> and would like to become an official support provider, <a href="https://fractalideas.com/">let me know</a>.)</p>
83
84 Why should I use ``websockets``?
85 --------------------------------
86
87 The development of ``websockets`` is shaped by four principles:
88
89 1. **Correctness**: ``websockets`` is heavily tested for compliance with
90 :rfc:`6455`. Continuous integration fails under 100% branch coverage.
91
92 2. **Simplicity**: all you need to understand is ``msg = await ws.recv()`` and
93 ``await ws.send(msg)``. ``websockets`` takes care of managing connections
94 so you can focus on your application.
95
96 3. **Robustness**: ``websockets`` is built for production. For example, it was
97 the only library to `handle backpressure correctly`_ before the issue
98 became widely known in the Python community.
99
100 4. **Performance**: memory usage is optimized and configurable. A C extension
101 accelerates expensive operations. It's pre-compiled for Linux, macOS and
102 Windows and packaged in the wheel format for each system and Python version.
103
104 Documentation is a first class concern in the project. Head over to `Read the
105 Docs`_ and see for yourself.
106
107 .. _Read the Docs: https://websockets.readthedocs.io/
108 .. _handle backpressure correctly: https://vorpus.org/blog/some-thoughts-on-asynchronous-api-design-in-a-post-asyncawait-world/#websocket-servers
109
110 Why shouldn't I use ``websockets``?
111 -----------------------------------
112
113 * If you prefer callbacks over coroutines: ``websockets`` was created to
114 provide the best coroutine-based API to manage WebSocket connections in
115 Python. Pick another library for a callback-based API.
116
117 * If you're looking for a mixed HTTP / WebSocket library: ``websockets`` aims
118 at being an excellent implementation of :rfc:`6455`: The WebSocket Protocol
119 and :rfc:`7692`: Compression Extensions for WebSocket. Its support for HTTP
120 is minimal — just enough for an HTTP health check.
121
122 If you want to do both in the same server, look at HTTP + WebSocket servers
123 that build on top of ``websockets`` to support WebSocket connections, like
124 uvicorn_ or Sanic_.
125
126 .. _uvicorn: https://www.uvicorn.org/
127 .. _Sanic: https://sanic.dev/en/
128
129 What else?
130 ----------
131
132 Bug reports, patches and suggestions are welcome!
133
134 To report a security vulnerability, please use the `Tidelift security
135 contact`_. Tidelift will coordinate the fix and disclosure.
136
137 .. _Tidelift security contact: https://tidelift.com/security
138
139 For anything else, please open an issue_ or send a `pull request`_.
140
141 .. _issue: https://github.com/python-websockets/websockets/issues/new
142 .. _pull request: https://github.com/python-websockets/websockets/compare/
143
144 Participants must uphold the `Contributor Covenant code of conduct`_.
145
146 .. _Contributor Covenant code of conduct: https://github.com/python-websockets/websockets/blob/main/CODE_OF_CONDUCT.md
147
148 ``websockets`` is released under the `BSD license`_.
149
150 .. _BSD license: https://github.com/python-websockets/websockets/blob/main/LICENSE