一个简单的websocket服务

参考: https://pypi.org/project/websockets/

python 复制代码
#!/usr/bin/env python3
import asyncio
from websockets.server import serve

async def echo(websocket):
    async for message in websocket:
        await websocket.send(message)

async def main():
    async with serve(echo, "127.0.0.1", 8765):
        await asyncio.Future()  # run forever

asyncio.run(main())

获取ip的方法

python 复制代码
def getIp(websocket):
    header = websocket.request_headers
    remote_host = header.get(REMOTE_HOST_KEY)
    if remote_host is not None:
        return remote_host
    return websocket.remote_address[0]

nginx配置

复制代码
location /asyncio {
    add_header Access-Control-Allow-Origin *;
    proxy_pass http://127.0.0.1:8765$request_uri;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header REMOTE-HOST $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

如果机器上有stl证书,上面这样配置,访问 wss://${域名}/asyncio 即可访问到上面部署的echo服务。

相关推荐
CPETW3 小时前
RS-232 Sniffer 嗅探器 ---- UNI-T电子负载通讯协议抓取-C
网络
liulilittle4 小时前
TCP UCP 卡尔曼滤波器
网络·网络协议·tcp/ip·通信
GOTXX5 小时前
SenseNova U1 实战体验:API 调用 + OpenClaw 接入全流程
服务器·网络·人工智能·语言模型
liulilittle5 小时前
TCP UCP:基于卡尔曼滤波的BBR增强型拥塞控制算法
linux·网络·c++·tcp/ip·算法·c·通讯
fortydusk6 小时前
3DMark v2.32.8426 专业授权版|专业显卡跑分工具
网络
清欢渡---6 小时前
三次握手四次挥手(对话场景)
运维·服务器·网络·hcia
XiYang-DING6 小时前
【Java EE】IP协议
网络·tcp/ip·java-ee
tom02186 小时前
2026年5月23日软考中级网络工程师考题答案整理
网络·网络工程师·软考·答案·试题·2026年
韦胖漫谈IT7 小时前
提示词注入- 大语言模型 OWASP TOP 10系列
网络·人工智能·语言模型·大模型安全·owasp
顾凌陵8 小时前
SQL注入漏洞进阶篇
网络·sql