dwsocket
dwsocket 为 Django WSGI 应用提供 WebSocket 支持,是
duanhongyi/dwebsocket 0.5.12 的维护分支。
首个公开版本重点修复新式 Django middleware 兼容和断线检测问题,同时保留原有 API。
分发名与导入名不同:安装命令是
pip install dwsocket,Python 代码仍使用import dwebsocket,middleware 路径也保持dwebsocket.middleware.WebSocketMiddleware。
支持范围
- Python 3.9+
- Django 3.2.x
- Django WSGI 应用
- 默认后端,以及上游提供的 uWSGI 后端
- 默认后端已在 Gunicorn + gevent 单 worker 环境验证
本项目不是 Django Channels 的替代品,目前不提供 ASGI 支持。
安装
pip install dwsocket
从原 dwebsocket 包迁移时,请先卸载原包,避免两个发行包覆盖同一导入目录:
pip uninstall dwebsocket
pip install dwsocket
不要同时安装 dwebsocket 和 dwsocket。现有视图、import 和配置路径无需修改。
快速开始
accept_websocket 允许同一个视图处理普通 HTTP 和 WebSocket 请求;
require_websocket 只接受 WebSocket 请求。
from dwebsocket import require_websocket
@require_websocket
def echo(request):
for message in request.websocket:
if message is not None:
request.websocket.send(message)
装饰器路径不需要额外启用全局 middleware。
可选 middleware
如果希望请求对象在所有 URL 上都具备 WebSocket 属性,可以加入:
MIDDLEWARE = [
"dwebsocket.middleware.WebSocketMiddleware",
# 其他 middleware……
]
默认情况下,普通视图仍不会接受 WebSocket 连接;视图需要使用上述装饰器。 如确需允许所有视图接受 WebSocket,可设置:
WEBSOCKET_ACCEPT_ALL = True
请求与 WebSocket API
视图中的请求对象提供:
request.is_websocket():判断当前请求是否为有效 WebSocket 请求。request.websocket:WebSocket 对象;普通 HTTP 请求下为None。
常用方法:
wait(timeout=None):等待一条消息;超时或连接关闭时返回None。read(fallback=None):非阻塞读取;没有新消息时返回fallback,不能据此判断断线。has_messages():是否存在待读取消息。count_messages():待读取消息数量。send(message):发送一条消息;连接已关闭或发送过程中断开时抛出ConnectionError。is_closed():判断连接是否已关闭。close(code=None, reason=None):关闭连接。
持续推送的视图应主动检查连接状态,并处理发送时的断线:
from dwebsocket import require_websocket
@require_websocket
def push(request):
while not request.websocket.is_closed():
try:
request.websocket.send("message")
except ConnectionError:
break
实际应用中通常还应在循环内加入等待或事件驱动逻辑,避免忙循环。
后端配置
默认后端适用于 Django 开发服务器及基于 WSGI 的 Gunicorn/gevent 部署。
使用 uWSGI 后端时:
WEBSOCKET_FACTORY_CLASS = "dwebsocket.backends.uwsgi.factory.uWsgiWebSocketFactory"
uWSGI 需要启用其 WebSocket 和异步相关能力,具体参数请参考 uWSGI 文档。
0.6.0 的主要变化
- 将 middleware 改为新式实例协议,兼容 Django 3.2 的
MIDDLEWARE和decorator_from_middleware路径。 - middleware 检测同时支持
MIDDLEWARE与旧式MIDDLEWARE_CLASSES。 - 发送前后检查连接状态,断线时向调用方抛出
ConnectionError,避免推送循环空转。 - 保留
dwebsocket导入名和现有视图 API,降低迁移成本。
项目与许可
- 项目仓库:https://github.com/wuchhao/dwsocket
- 上游项目:https://github.com/duanhongyi/dwebsocket
- 问题反馈:https://github.com/wuchhao/dwsocket/issues
dwsocket 的新修改采用 BSD 3-Clause;上游 dwebsocket 采用 BSD 2-Clause。
上游还说明其开发基于 django-websocket。
完整版权与许可文本见 LICENSE。
Metadata
Release files for dwsocket 0.6.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| dwsocket-0.6.0.tar.gz | 14.4 kB | Without provenance | Details for dwsocket-0.6.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| dwsocket-0.6.0-py3-none-any.whl | Python 3 | none | any | Details for dwsocket-0.6.0-py3-none-any.whl |
Total release size: 29.2 kB
Release files / dwsocket-0.6.0.tar.gz
| Download URL | dwsocket-0.6.0.tar.gz |
|---|---|
| Size | 14.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
86ceaf557ae8ec4a3bdbee8b7bcb3d5c15d6182b4c4f8fb4846c76a7fb554e56
|
|
BLAKE2b-256 checksum How to use checksums |
e0cfb71a51fee9594b9ea0334d3942a30b70fe581bbf91ae33031ecad43ad8da
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.9.13
|
Release files / dwsocket-0.6.0-py3-none-any.whl
| Download URL | dwsocket-0.6.0-py3-none-any.whl |
|---|---|
| Size | 14.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
eb8ed40728ca369176f2f8b67f66e058c6a1fe5cd3f3f3964dffc0d8ab83c9cb
|
|
BLAKE2b-256 checksum How to use checksums |
f73fe42364c41d877bbc5ece58eece927f50e6d06960a7fd10016c14205e28bf
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.9.13
|
Security for dwsocket 0.6.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.