Skip to main content

peer-protocol

基于 aiohttp 的 WebSocket 双向通信库,支持服务端广播、客户端断线重连。

特性

  • 服务端:WebSocket 服务、多客户端广播、自动清理断线连接
  • 客户端:断线自动重连、TCP 探活、统一生命周期回调
  • 类型提示:connect/disconnect 泛型,便于静态检查

安装

pip install peer-protocol

快速开始

服务端

import asyncio
from peer_protocol import Server

async def main():
    server = Server(host="0.0.0.0", port=8080)

    @server.on_receive
    async def _(payload):
        # 收到任意客户端消息时广播给所有客户端
        await server.broadcast(payload)

    await server.run()

if __name__ == "__main__":
    asyncio.run(main())

客户端

import asyncio
from peer_protocol import Client

async def main():
    # 需包含 /ws 路径
    client = Client(url="http://127.0.0.1:8080/ws")

    @client.on_connect
    async def _(ws):
        await client.send({"type": "hello", "message": "world"})

    @client.on_receive
    async def _(payload):
        print("收到:", payload)
        await client.stop()  # 收到消息后退出

    await client.run()

if __name__ == "__main__":
    asyncio.run(main())

API 概览

Server

参数/方法 说明
Server(host="0.0.0.0", port=8080, heartbeat=120) 创建服务端
await server.start() 启动服务
await server.stop() 停止服务
await server.run() 启动并阻塞直到 stop()
await server.broadcast(payload) 广播 JSON 数据到所有客户端
on_start, on_stop 启动/停止回调
on_connect, on_disconnect 连接/断开回调,参数为 WebSocketResponse
on_send, on_receive 发送/接收消息回调

Client

参数/方法 说明
Client(url, retry_interval=2, retry_timeout=5) 创建客户端
await client.start() 启动并开始重连
await client.stop() 停止
await client.run() 启动并阻塞直到 stop()
await client.send(payload) 发送 JSON 数据
client.connected 是否已连接
on_start, on_stop 启动/停止回调
on_connect, on_disconnect 连接/断开回调,参数为 ClientWebSocketResponse
on_send, on_receive 发送/接收消息回调

运行示例

# 终端 1:启动服务端
python -m examples.server

# 终端 2:启动客户端
python -m examples.client

Metadata

Release files for peer-protocol 0.0.9

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for peer-protocol 0.0.9
File Size Uploaded Provenance
peer_protocol-0.0.9.tar.gz 7.4 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for peer-protocol 0.0.9
File Interpreter ABI Platform
peer_protocol-0.0.9-py3-none-any.whl Python 3 none any Details

Total release size: 16.3 kB

Release files / peer_protocol-0.0.9.tar.gz

Download URL peer_protocol-0.0.9.tar.gz
Size 7.4 kB
Tags Source
SHA-256 checksum
How to use checksums
70b77ed1382322ba08a89dfaffaa83c167d6db758e7f71e051dd2fbe3c0bc064
BLAKE2b-256 checksum
How to use checksums
0c6bdd2030f65efe2f30ac1cb5ea5021408d4aa37c3d6a7a244bba838a8f6d3d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.6

Release files / peer_protocol-0.0.9-py3-none-any.whl

Download URL peer_protocol-0.0.9-py3-none-any.whl
Size 8.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
26c8e4996189c2e61a748d122e1e9eb93b74f6526646e804533e5d2457eb6a20
BLAKE2b-256 checksum
How to use checksums
b8c5295dddcc684635bf61ca47104586c1025019e2516315242b0bac1b676017
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.6

Security for peer-protocol 0.0.9

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.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page