易于使用的aiohttp websockets应用程序。
aiows的Python项目详细描述
aiohttp websockets服务器
基于aiohttp框架的易于使用的websocket服务器。这个工具可以让你 以便在短时间内轻松构建基于websockets的应用程序。
要求
- Python3.6+
aiows不需要其他任何东西。这是一个非常简单和轻量级的应用程序 这可以使您能够与客户机实时通信您的服务器。
aiows支持三种类型的包:
- json
- 文本
- 字节
这让你可以建立任何你想要的。例如-视频聊天,多人游戏, 聊天,通知…
安装
pip install aiows
安装后-您可以开始开发应用程序。前端示例 sockets用法可以在demo/simple socket.js上看到。
用法
基本理解
aiows不支持websockets双向通信。意思是你不会 能够通过套接字将数据包发送到服务器。为了安全和稳定 原因。aiows没有双向通信,而是有http api,它允许您 在ws订阅者之间共享包。
http api是从全局网络打开的,因此出于安全原因,可以设置发布密码 它将过滤任何未经授权的请求。
aiows
$ aiows --help
usage: aiows [-h] [--pwd PWD] [--usock USOCK] [--host HOST] [--port PORT]
[--reuse-addr REUSE_ADDR] [--reuse-port REUSE_PORT]
[--url-prefix URL_PREFIX]
AIOHttp WebSocket server
optional arguments:
-h, --help show this help message and exit
--pwd PWD Password to be able to publish messages.
--usock USOCK UNIX Socket file for aiows server
--host HOST Server host
--port PORT Server port
--reuse-addr REUSE_ADDR
Reuse host
--reuse-port REUSE_PORT
Reuse port
--url-prefix URL_PREFIX
API Endpoints prefix
http api端点
/频道/{频道名称}/
这是WebSocket终结点。允许订阅频道更新。
post/频道/{频道名称}/
允许将包共享到通道。
Request Headers:
- Package-Type(Text/bytes/json) - which type of package will be sent.
Query Params:
- pwd(str) - publishing password (by default: None)
Request body:
- any - publishing message
Responses:
- 403 - Wrong password
- 400 - Failed to read request body
- 201 - Published
示例
import requests
requests.post(
url='http://127.0.0.1/channel/test-channel/?pwd=some-secure-pwd',
headers={'package-type': 'text'},
data='Hello world'
)
post/broadcast/
允许在多个通道之间批量共享包。
Query Params:
- pwd(str) - publishing password (by default: None)
Request body:
- json(str) - publishing channels and messages as key=>value. Example:
{
"room:1": [{"text": "Hi all"}],
"user:22": [{"json": {"notification": "You've got new friend"}}],
"video:stream": [{"bytes": "\x031\x032\x033..."}, {"bytes": "\x031\x032\x033..."}]
}
Responses:
- 403 - Wrong password
- 400 - Failed to read request body
- 201 - Published
示例
import requests
requests.post(
url='http://127.0.0.1/broadcast/?pwd=some-secure-pwd',
json={
"test-channel": [
{"text": "Hello world"}
],
"call:stream-channel": [
{"bytes": "\x031\x032\x033..."},
{"bytes": "\x031\x032\x033..."},
{"bytes": "\x031\x032\x033..."},
{"bytes": "\x031\x032\x033..."},
{"bytes": "\x031\x032\x033..."},
{"bytes": "\x031\x032\x033..."}
]
}
)