python - Python websockets库V10.0破解客户端代码
问题描述
我正在尝试使用 AWS API 网关设置 Web 套接字应用程序。
我正在使用 Anaconda Python 版本 3.7.3
我创建了在教程中找到的项目:
https
://github.com/ryanmurakami/websockets-on-aws
(这是一个简单的聊天应用程序,它向发送的任何字符串发送随机响应一个网络套接字。)
对于客户端,我下载了 Python websockets 库:
https ://pypi.org/project/websockets/
$ pip install websockets
这安装了最新的 websockets 版本 10.0(2021 年 10 月)。
但是客户端应用程序不起作用。
问题:
客户端能够连接到服务器。
我可以在 AWS 端的 CloudWatch 日志中看到传入消息。
lambda 也产生了响应,该响应再次记录在 CloudWatch 上。
但是响应没有到达我的 Python 客户端。它似乎冻结了。
当我启用调试跟踪时,我可以看到乒乓球在进行:
%% sending keepalive ping
> PING 4f 91 72 32 [binary, 4 bytes]
< PONG 4f 91 72 32 [binary, 4 bytes]
%% received keepalive pong
我找到的解决方案:
我将 websockets 库从 Ver 10.0 降级到 Ver 9.0.2
现在客户端开始接收消息。
我的问题是:最新版本的库中的重大变化是什么?
如何在客户端代码中修复它?
如果有用,这是我的客户端代码:
# An asyncIO web socket client for an AWS API gateway server
# This worked on websocket library Ver 9.0.2, but there was no reply from the server when I upgraded to Ver 10.0
# Debugging shows ping pong is working, but still no response from the server.
import json
import asyncio
import websockets
import logging
logger = logging.getLogger('websockets')
logger.setLevel(logging.DEBUG)
logger.addHandler(logging.StreamHandler())
async def hello():
websocket = await websockets.connect('wss://xxxxxxxxx.execute-api.us-east-2.amazonaws.com/test')
payload = { 'action' : 'myroute', 'question' : 'How do you do?' }
await websocket.send (json.dumps(payload))
logger.debug ('Message sent.')
reply = await websocket.recv()
logger.debug(reply)
asyncio.get_event_loop().run_until_complete(hello())
解决方案
推荐阅读
- javascript - Lottie - 基于网络的交互式动画,带有光栅图像
- sql - SQL:在一列中获取最小值和最大值
- regex - 匹配和排除的正则表达式
- mysql - 每个时段的 MySQL 查询小计,小时从当天下午 2 点到第二天下午 2 点
- python - 用于列表理解 Python 的多个条件语句
- azure - Azure 门户中的“拒绝访问:您无权访问”错误
- c# - DLL 引用在引用的项目中不起作用
- android - React 本机 Android 出现 OutOfMemoryError 的原因
- javascript - Jquery DataTable 复选框
- c# - 您如何正确处理另一个脚本功能?