易于使用的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..."}
        ]
    }
)

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java Rest DSL路由无法启动路由,因为同一端点不允许有多个使用者   jvm有没有像JConsole或VisualVM这样的工具可以告诉我“类、对象、引用变量在java中存储在哪里?”   java为什么我的列表中的所有元素看起来都一样?   java运行时。运行shell脚本的exec无法打开文件   JPopupMenu的JMenuItem的java热键   会话在Java中被覆盖   如何在java中去除字符串中的尖锐重音?   java Mockito监视一个接口,模拟它的默认方法,获取NullPointerException   javascript如何在jquery中禁用同一时间选择   将数组列表附加到现有CSV文件,但它会附加并清除存储在Java文件中的以前的数据   java从我的jar中访问pdf文件   java Sonar issue参数必须为非Null,但标记为可为Null   java Salesforce流式API:在网络故障之后和重新订阅之前获取事件   在Java 1.8.0_65上运行Play2.0应用程序时出现playframework错误   java为什么字母的ASCII由方法自动转换。toCharArray()?   java如何知道JDialog是否关闭?   java向服务器发送POST请求,服务器的响应为null,启动   java如何设置JTable中特定单元格的值?   ImagePlus中的java保存问题