网站首页 > 技术文章 正文
1: 使用 aiohttp 实现简单回声
aiohttp 提供异步 WebSockets。
适用于 Python 3.x 版本 ≥ 3.5
import asyncio
from aiohttp import ClientSession
async with ClientSession() as session:
async def hello_world():
websocket = await session.ws_connect("wss://echo.websocket.org")
websocket.send_str("Hello, world!")
print("Received:", (await websocket.receive()).data)
await websocket.close()
loop = asyncio.get_event_loop()
loop.run_until_complete(hello_world())
2: 使用 aiohttp 的包装类
aiohttp.ClientSession 可以作为自定义 WebSocket 类的父类。
适用于 Python 3.x 版本 ≥ 3.5
import asyncio
from aiohttp import ClientSession
class EchoWebSocket(ClientSession):
URL = "wss://echo.websocket.org"
def __init__(self):
super().__init__()
self.websocket = None
async def connect(self):
"""Connect to the WebSocket."""
self.websocket = await self.ws_connect(self.URL)
async def send(self, message):
"""Send a message to the WebSocket."""
assert self.websocket is not None, "You must connect first!"
self.websocket.send_str(message)
print("Sent:", message)
async def receive(self):
"""Receive one message from the WebSocket."""
assert self.websocket is not None, "You must connect first!"
return (await self.websocket.receive()).data
async def read(self):
"""Read messages from the WebSocket."""
assert self.websocket is not None, "You must connect first!"
while self.websocket.receive():
message = await self.receive()
print("Received:", message)
if message == "Echo 9!":
break
async def send(websocket):
for n in range(10):
await websocket.send("Echo {}!".format(n))
await asyncio.sleep(1)
loop = asyncio.get_event_loop()
async with EchoWebSocket() as websocket:
loop.run_until_complete(websocket.connect())
tasks = (
send(websocket),
websocket.read()
)
loop.run_until_complete(asyncio.wait(tasks))
loop.close()
3: 使用 Autobahn 作为 WebSocket 工厂
Autobahn 包可用于 Python WebSocket 服务器工厂。
Python Autobahn 包文档
安装通常只需在终端中使用以下命令:
(对于 Linux):
sudo pip install autobahn
(对于 Windows):
python -m pip install autobahn
然后,可以在 Python 脚本中创建一个简单的回声服务器:
from autobahn.asyncio.websocket import WebSocketServerProtocol
class MyServerProtocol(WebSocketServerProtocol):
'''创建服务器协议时,用户定义的类继承自 WebSocketServerProtocol,
需要覆盖 onMessage、onConnect 等事件以实现用户指定的功能,
这些事件定义了服务器的协议,本质上'''
def onMessage(self,payload,isBinary):
'''当服务器收到消息时,会调用 onMessage 方法。
它需要 payload 和布尔值 isBinary 作为参数。payload 是消息的实际内容,
isBinary 是一个标志,用于告知用户 payload 是否包含二进制数据。
在此示例中,payload 将按原样返回给发送者。'''
self.sendMessage(payload,isBinary)
if__name__=='__main__':
try:
importasyncio
except ImportError:
'''Trollius = 0.3 被重命名'''
import trollius as asyncio
from autobahn.asyncio.websocketimportWebSocketServerFactory
factory=WebSocketServerFactory()
'''初始化 WebSocket 工厂,并将协议设置为上述定义的协议
(继承自 autobahn.asyncio.websocket.WebSocketServerProtocol 的类)'''
factory.protocol=MyServerProtocol
'''上述代码可以理解为将 MyServerProtocol 类中描述的
onConnect、onMessage 等方法绑定到服务器,
设置服务器的功能,即协议'''
loop=asyncio.get_event_loop()
coro=loop.create_server(factory,'127.0.0.1',9000)
server=loop.run_until_complete(coro)
'''在无限循环中运行服务器'''
try:
loop.run_forever()
except KeyboardInterrupt:
pass
finally:
server.close()
loop.close()
在此示例中,服务器将在本地主机(127.0.0.1)的 9000 端口上创建。这是监听的 IP 和端口。此信息非常重要,因为使用它,你可以查找计算机的局域网地址,并从调制解调器进行端口转发,通过你拥有的任何路由器将信息转发到计算机。然后,通过使用 Google 查找你的广域网 IP,你可以设计你的网站向你的广域网 IP 发送 WebSocket 消息,端口为 9000(在此示例中)。
重要的是,你需要从调制解调器进行反向端口转发,也就是说,如果你有路由器串联连接到调制解调器,请进入调制解调器的配置设置,从调制解调器向连接的路由器进行端口转发,依此类推,直到连接到计算机的最终路由器将从调制解调器端口 9000(在此示例中)接收到的信息转发到它。
猜你喜欢
- 2025-03-25 使用消息代理作为LLM路由器:集成OpenAI和Claude
- 2025-03-25 Dify工具使用全场景:dify-sandbox沙盒的原理(源码篇·第2期)
- 2025-03-25 虚拟机panic问题排查(虚拟机出现故障,恢复方法)
- 2025-03-25 3分钟简述熔断器使用方法(熔断器的操作流程?)
- 2025-03-25 太实用了!自己动手写软件——SSH、FTP和SQL server的密码破解
- 2025-03-25 最近很火的MCP是什么?一文带你搞懂,附大量MCP开源服务端项目!
- 2025-03-25 数据库调优-连接池优化(数据库连接池配置优化)
- 2025-03-25 小白之Tkinter库读文:其他功能-网络功能(51)
- 2025-03-25 python散装笔记——123: 客户端与服务器之间套接字和消息加解密
- 2025-03-25 【Python备忘单】Python编程的快速参考指南
- 08-06中等生如何学好初二数学函数篇
- 08-06C#构造函数
- 08-06初中数学:一次函数学习要点和方法
- 08-06仓颉编程语言基础-数据类型—结构类型
- 08-06C++实现委托机制
- 08-06初中VS高中三角函数:从"固定镜头"到"360°全景",数学视野升级
- 08-06一文讲透PLC中Static和Temp变量的区别
- 08-06类三剑客:一招修改所有对象!类方法与静态方法的核心区别!
- 1522℃桌面软件开发新体验!用 Blazor Hybrid 打造简洁高效的视频处理工具
- 649℃Dify工具使用全场景:dify-sandbox沙盒的原理(源码篇·第2期)
- 527℃MySQL service启动脚本浅析(r12笔记第59天)
- 492℃服务器异常重启,导致mysql启动失败,问题解决过程记录
- 492℃启用MySQL查询缓存(mysql8.0查询缓存)
- 479℃「赵强老师」MySQL的闪回(赵强iso是哪个大学毕业的)
- 461℃mysql服务怎么启动和关闭?(mysql服务怎么启动和关闭)
- 460℃MySQL server PID file could not be found!失败
- 最近发表
- 标签列表
-
- cmd/c (90)
- c++中::是什么意思 (84)
- 标签用于 (71)
- 主键只能有一个吗 (77)
- c#console.writeline不显示 (95)
- pythoncase语句 (88)
- es6includes (74)
- sqlset (76)
- windowsscripthost (69)
- apt-getinstall-y (100)
- node_modules怎么生成 (87)
- chromepost (71)
- flexdirection (73)
- c++int转char (80)
- mysqlany_value (79)
- static函数和普通函数 (84)
- el-date-picker开始日期早于结束日期 (70)
- asynccallback (71)
- localstorage.removeitem (74)
- vector线程安全吗 (70)
- java (73)
- js数组插入 (83)
- mac安装java (72)
- 查看mysql是否启动 (70)
- 无效的列索引 (74)