websockets
websockets
在 github 中的描述如下,简而言之,websockets
是基于asyncio
实现websocket
。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.
websockets
的优点是:
- 简单:使用
websocket
只需要了解msg = await ws.recv()
和await ws.send(msg)
;websockets
负责管理连接,以便可以专注于应用程序。 - 健壮:
websockets
为生产而构建。 - 质量:
websockets
经过严格测试。 - 性能:内存使用是可配置的。用 C 编写的扩展加速了昂贵的操作。它针对 Linux、macOS 和 Windows 进行了预编译,并针对每个系统和 Python 版本进行 package 。
websockets
不适用于以下场景:
- 如果项目使用回调函数而不是协程。
websockets
创建是为了提供基于协程的 API 来管理 Python 中的 WebSocket 连接。 - 如果项目需要在 http/websocket 场景下使用,
websockets
并不是很适用,websockets
主要