Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Library for building WebSocket servers and clients in Python

License

NotificationsYou must be signed in to change notification settings

python-websockets/websockets

websockets

licenceversionpyversionstestsdocsopenssf

What iswebsockets?

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!


websockets for enterprise

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.)

Why should I usewebsockets?

The development ofwebsockets is shaped by four principles:

  1. Correctness:websockets is heavily tested for compliance withRFC 6455. Continuous integration fails under 100% branch coverage.
  2. Simplicity: all you need to understand ismsg = await ws.recv() andawait ws.send(msg).websockets takes care of managing connectionsso you can focus on your application.
  3. Robustness:websockets is built for production. For example, it wasthe only library tohandle backpressure correctly before the issuebecame widely known in the Python community.
  4. 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.

Why shouldn't I usewebsockets?

  • If you prefer callbacks over coroutines:websockets was 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:websockets aimsat 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 ofwebsockets to support WebSocket connections, likeuvicorn orSanic.

What else?

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.


[8]ページ先頭

©2009-2025 Movatter.jp