An implementation of the WebSocket Protocol (RFC 6455 & 7692)
Project description
What is websockets?
websockets is a library for building WebSocket servers and clients in Python with a focus on correctness and simplicity.
Built on top of asyncio, Python’s standard asynchronous I/O framework, it provides an elegant coroutine-based API.
Documentation is available on Read the Docs.
Here’s how a client sends and receives messages:
#!/usr/bin/env python
import asyncio
import websockets
async def hello(uri):
async with websockets.connect(uri) as websocket:
await websocket.send("Hello world!")
await websocket.recv()
asyncio.get_event_loop().run_until_complete(
hello('ws://localhost:8765'))
And here’s an echo server:
#!/usr/bin/env python
import asyncio
import websockets
async def echo(websocket, path):
async for message in websocket:
await websocket.send(message)
asyncio.get_event_loop().run_until_complete(
websockets.serve(echo, 'localhost', 8765))
asyncio.get_event_loop().run_forever()
Does that look good?
Why should I use websockets?
The development of websockets is shaped by four principles:
Simplicity: all you need to understand is msg = await ws.recv() and await ws.send(msg); websockets takes care of managing connections so you can focus on your application.
Robustness: websockets is built for production; for example it was the only library to handle backpressure correctly before the issue became widely known in the Python community.
Quality: websockets is heavily tested. Continuous integration fails under 100% branch coverage. Also it passes the industry-standard Autobahn Testsuite.
Performance: memory use is configurable. An extension written in C accelerates expensive operations. It’s pre-compiled for Linux, macOS and Windows and packaged in the wheel format for each system and Python version.
Documentation is a first class concern in the project. Head over to Read the Docs and see for yourself.
Why shouldn’t I use websockets?
If you prefer callbacks over coroutines: websockets was created to provide the best coroutine-based API to manage WebSocket connections in Python. Pick another library for a callback-based API.
If you’re looking for a mixed HTTP / WebSocket library: websockets aims at being an excellent implementation of RFC 6455: The WebSocket Protocol and RFC 7692: Compression Extensions for WebSocket. Its support for HTTP is minimal — just enough for a HTTP health check.
If you want to use Python 2: websockets builds upon asyncio which only works on Python 3. websockets requires Python ≥ 3.6.
What else?
Bug reports, patches and suggestions are welcome!
To report a security vulnerability, please use the Tidelift security contact. Tidelift will coordinate the fix and disclosure.
For anything else, please open an issue or send a pull request.
Participants must uphold the Contributor Covenant code of conduct.
websockets is released under the BSD license.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Hashes for websockets-8.0.2-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 049e694abe33f8a1d99969fee7bfc0ae6761f7fd5f297c58ea933b27dd6805f2 |
|
MD5 | 1b1f99f468c24e4c123b2fbbc80d842c |
|
BLAKE2b-256 | 9c60f96f535f3354cb6ba5e5c7ab128b1c4802a2d040ee7225e3fe51242816c1 |
Hashes for websockets-8.0.2-cp37-cp37m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f5cb2683367e32da6a256b60929a3af9c29c212b5091cf5bace9358d03011bf5 |
|
MD5 | f8792067b3866a51b7c65c62cb240214 |
|
BLAKE2b-256 | 71144a1e84fd8701e05b7e97cb84a833c6a3f18427b5d5cefde7c0d66b44dab8 |
Hashes for websockets-8.0.2-cp37-cp37m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a7affaeffbc5d55681934c16bb6b8fc82bb75b175e7fd4dcca798c938bde8dda |
|
MD5 | c135d083379f0e40253c08d6547e3b12 |
|
BLAKE2b-256 | 49afd01b9d7a6c95d86c8f3c3df7a152f4e3d131fb1fe54facea103a3fd89ed4 |
Hashes for websockets-8.0.2-cp37-cp37m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8c77f7d182a6ea2a9d09c2612059f3ad859a90243e899617137ee3f6b7f2b584 |
|
MD5 | 0cb600d39120c94ceb0fbc17fe540fab |
|
BLAKE2b-256 | 21f13bcde69c268b44026768a4c20bc617e16c8c568ff3a9aa1af04a973bb22b |
Hashes for websockets-8.0.2-cp37-cp37m-macosx_10_6_intel.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 73ce69217e4655783ec72ce11c151053fcbd5b837cc39de7999e19605182e28a |
|
MD5 | abd9ae819e6817bfbf40f71cf358a03c |
|
BLAKE2b-256 | 8b6bdcbafe10a1b889f3d31bef7048dbfb23973d4b56e8fb47c9158c47fa5643 |
Hashes for websockets-8.0.2-cp36-cp36m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c82e286555f839846ef4f0fdd6910769a577952e1e26aa8ee7a6f45f040e3c2b |
|
MD5 | 025f1d61a55f2e154dc49279f2891911 |
|
BLAKE2b-256 | 6abd8555c65b3fca7310b2601d0957b36511c3c3c2e8a93a6d3f90c671b70c6a |
Hashes for websockets-8.0.2-cp36-cp36m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8d7a20a2f97f1e98c765651d9fb9437201a9ccc2c70e94b0270f1c5ef29667a3 |
|
MD5 | f190f72787dfcc1b98be5842725e2b63 |
|
BLAKE2b-256 | 36602301775ccbdc4082a5e44743ca8aed7939ba2cc67c1ac6961f6172c1fb44 |
Hashes for websockets-8.0.2-cp36-cp36m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e9102043a81cdc8b7c8032ff4bce39f6229e4ac39cb2010946c912eeb84e2cb6 |
|
MD5 | 6f407d9bd81e30a7109e74abec9b179b |
|
BLAKE2b-256 | f04bad228451b1c071c5c52616b7d4298ebcfcac5ae8515ede959db19e4cd56d |
Hashes for websockets-8.0.2-cp36-cp36m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 83e63aa73331b9ca21af61df8f115fb5fbcba3f281bee650a4ad16a40cd1ef15 |
|
MD5 | b41b3a31a77fa4e3e17bd040cd4ebc9a |
|
BLAKE2b-256 | a5e5ceb80a4952a24303f559c49ea2cb4ddbd9917c3dc6db456c3ab59f612747 |
Hashes for websockets-8.0.2-cp36-cp36m-macosx_10_6_intel.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e906128532a14b9d264a43eb48f9b3080d53a9bda819ab45bf56b8039dc606ac |
|
MD5 | 2759f1f3055d8a2e6a0dfc12ead9e1f2 |
|
BLAKE2b-256 | cd2d7d1e969854d50e2e4adfc38a7bdfab0d470444d340c4eae3ba176fa4765c |