首页 > 解决方案 > 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())

标签: pythonamazon-web-serviceswebsocketapi-gateway

解决方案


推荐阅读