Skip to main content

既能做客户端也能做服务端

Project description

peer-protocol

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

特性

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

安装

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

peer_protocol-0.0.4.tar.gz (6.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

peer_protocol-0.0.4-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

File details

Details for the file peer_protocol-0.0.4.tar.gz.

File metadata

  • Download URL: peer_protocol-0.0.4.tar.gz
  • Upload date:
  • Size: 6.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.6

File hashes

Hashes for peer_protocol-0.0.4.tar.gz
Algorithm Hash digest
SHA256 d6d6c0aa27f14a625717c91c1740bef0a4aa7010dad5127881e3b423315b6b36
MD5 ae9828fa3775a74df407110f079fdd4f
BLAKE2b-256 cd714b9f14e357e6ad2b366abccb21fbf7e2e6253b3f80f1074721584f60c247

See more details on using hashes here.

File details

Details for the file peer_protocol-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: peer_protocol-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 7.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.6

File hashes

Hashes for peer_protocol-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 bfd5872cfb8f127a4f5e9e302a2c248993d28ad129f32202ef70ec344d1d7991
MD5 57c91c3a5b2e172e90eba59bffd87dd7
BLAKE2b-256 a67de10edec858920ec7a9b0a06c6d63cc0067ec3c253e05087babda7f207ea8

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page