Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork577
Library for building WebSocket servers and clients in Python
License
python-websockets/websockets
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
websockets is a library for buildingWebSocket servers and clients in Pythonwith a focus on correctness, simplicity, robustness, and performance.
Built on top ofasyncio, Python's standard asynchronous I/O framework, thedefault implementation provides an elegant coroutine-based API.
An implementation on top ofthreading and a Sans-I/O implementation are alsoavailable.
Documentation is available on Read the Docs.
Here's an echo server with theasyncio API:
#!/usr/bin/env pythonimportasynciofromwebsockets.asyncio.serverimportserveasyncdefecho(websocket):asyncformessageinwebsocket:awaitwebsocket.send(message)asyncdefmain():asyncwithserve(echo,"localhost",8765)asserver:awaitserver.serve_forever()asyncio.run(main())
Here's how a client sends and receives messages with thethreading API:
#!/usr/bin/env pythonfromwebsockets.sync.clientimportconnectdefhello():withconnect("ws://localhost:8765")aswebsocket:websocket.send("Hello world!")message=websocket.recv()print(f"Received:{message}")hello()
Does that look good?
Get started with the tutorial!
Available as part of the Tidelift Subscription
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.Learn more.
(If you contribute towebsockets and would like to become an official support provider,let me know.)
The development ofwebsockets is shaped by four principles:
- Correctness:
websocketsis heavily tested for compliance withRFC 6455. Continuous integration fails under 100% branch coverage. - Simplicity: all you need to understand is
msg = await ws.recv()andawait ws.send(msg).websocketstakes care of managing connectionsso you can focus on your application. - Robustness:
websocketsis built for production. For example, it wasthe only library tohandle backpressure correctly before the issuebecame widely known in the Python community. - Performance: memory usage is optimized and configurable. A C extensionaccelerates expensive operations. It's pre-compiled for Linux, macOS andWindows and packaged in the wheel format for each system and Python version.
Documentation is a first class concern in the project. Head over toRead theDocs and see for yourself.
If you prefer callbacks over coroutines:
websocketswas created toprovide the best coroutine-based API to manage WebSocket connections inPython. Pick another library for a callback-based API.If you're looking for a mixed HTTP / WebSocket library:
websocketsaimsat being an excellent implementation ofRFC 6455: The WebSocket ProtocolandRFC 7692: Compression Extensions for WebSocket. Its support for HTTPis minimal — just enough for an HTTP health check.If you want to do both in the same server, look at HTTP + WebSocket serversthat build on top of
websocketsto support WebSocket connections, likeuvicorn orSanic.
Bug reports, patches and suggestions are welcome!
To report a security vulnerability, please use theTidelift securitycontact. Tidelift will coordinate the fix and disclosure.
For anything else, please open anissue or send apull request.
Participants must uphold theContributor Covenant code of conduct.
websockets is released under theBSD license.
About
Library for building WebSocket servers and clients in Python
Topics
Resources
License
Code of conduct
Security policy
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
