websocket协议的实现(rfc 6455&7692)
websockets的Python项目详细描述
什么是websockets?
websockets是用于在中构建websocketservers和clients的库 着重于正确性和简单性的python。
构建在python的标准异步i/o框架asyncio之上,它 提供了一个优雅的基于协程的api。
Documentation is available on Read the Docs.
以下是客户端发送和接收消息的方式:
#!/usr/bin/env pythonimportasyncioimportwebsocketsasyncdefhello(uri):asyncwithwebsockets.connect(uri)aswebsocket:awaitwebsocket.send("Hello world!")awaitwebsocket.recv()asyncio.get_event_loop().run_until_complete(hello('ws://localhost:8765'))
还有一个echo服务器:
#!/usr/bin/env pythonimportasyncioimportwebsocketsasyncdefecho(websocket,path):asyncformessageinwebsocket:awaitwebsocket.send(message)asyncio.get_event_loop().run_until_complete(websockets.serve(echo,'localhost',8765))asyncio.get_event_loop().run_forever()
看起来不错吧?
为什么要使用websockets?
websockets的发展由四个原则决定:
- 简单性:您只需要了解msg = await ws.recv()和 await ws.send(msg);websockets负责管理连接 所以你可以专注于你的申请。
- 健壮性:websockets是为生产而构建的;例如 唯一一个在发行前handle backpressure correctly的库 在python社区中广为人知。
- 质量:websockets经过严格测试。持续集成失败 低于100%分行覆盖率。它还通过了行业标准Autobahn Testsuite。
- 性能:内存使用是可配置的。用c写的分机 加速昂贵的运营。它是为Linux、MacOS和 windows,并以wheel格式为每个系统和python版本打包。
文档是项目中的头等大事。去Read the Docs看看你自己。
为什么不使用websockets?
还有什么?
欢迎提供错误报告、补丁和建议!
若要报告安全漏洞,请使用Tidelift security contact。Tidelift将协调修复和披露。
如需其他信息,请打开issue或发送pull request。
参与者必须坚持Contributor Covenant code of conduct。
websockets在BSD license下释放。