在 python 中调用 websocket 后无法解析响应——locust
我调用 websocket 调用,该调用返回中间有三角形的响应。我想摆脱那个三角形
响应如下:
{"type":1,"target":"CustomerDatapassCallback","arguments":[{"value":{"name":"PageMarkers","data":"unmarked page, unmarked page, unmarked page, unmarked page, unmarked page, unmarked page","messageId":"9E216B6458F13B9B07631F0BE2637E57"},"chatId":"36416579612304584","timestamp":-62135596800,"messageId":"9E216B6458F13B9B07631F0BE2637E57","connectionId":"l7UdY5uuw_J3jkREP42fGwfebbdf651","traceId":"a2d7f5e8-c202-4218-832b-03f7a5e36528","success":true}]}▲{"type":1,"target":"ChannelInformationCallback","arguments":[{"value":{"channel":0,"device":{"name":"Edge","version":"90.0.818.66","operatingSystem":"Windows 10"}},"chatId":"36416579612304584","timestamp":-62135596800,"messageId":null,"connectionId":"l7UdY5uuw_J3jkREP42fGwfebbdf651","traceId":"a2d7f5e8-c202-4218-832b-03f7a5e36528","success":true}]}
我为解决此问题而创建的代码如下。
def parse_signalr_response(json_str: str) -> List[SignalrResponse]:
以下代码有效,即如果三角形位于末尾,我可以删除该三角形。
if (json_str.endswith('▲')):
json_str = json_str[:(len(json_str) - Constants.sigr_suffix_decode_len)]
elif (json_str.endswith(Constants.sigr_suffix_encoded1)):
json_str = json_str[:(len(json_str)-1)]
elif (json_str.endswith(Constants.sigr_suffix_encoded)):
json_str = json_str[:(len(json_str)-1)]
以下代码不起作用,即如果三角形位于响应中间。那么这段代码不会进入 if 块。
# if (Constants.sigr_suffix_encoded1 in json_str):
if (json_str.__contains__('▲')):
# if (json_str.find('▲')):
# json_str.replace("▲", '')
base_sigr_response: SignalrResponse = JsonHelper.parse_json(json_str, SignalrResponse)
if (base_sigr_response.type == SignalrResponseType.Callback):
return _parse_signalr_callback(json_str)
elif (base_sigr_response.type == SignalrResponseType.Response):
if hasattr(base_sigr_response, 'error'):
return [_parse_signalr_error(json_str)]
else:
return [_parse_signalr_success(json_str)]
return [base_sigr_response]
I invoke the websocket call that returns the response that has triangle in the middle of it. I want to get rid of that triangle
Response is following :
{"type":1,"target":"CustomerDatapassCallback","arguments":[{"value":{"name":"PageMarkers","data":"unmarked page, unmarked page, unmarked page, unmarked page, unmarked page, unmarked page","messageId":"9E216B6458F13B9B07631F0BE2637E57"},"chatId":"36416579612304584","timestamp":-62135596800,"messageId":"9E216B6458F13B9B07631F0BE2637E57","connectionId":"l7UdY5uuw_J3jkREP42fGwfebbdf651","traceId":"a2d7f5e8-c202-4218-832b-03f7a5e36528","success":true}]}▲{"type":1,"target":"ChannelInformationCallback","arguments":[{"value":{"channel":0,"device":{"name":"Edge","version":"90.0.818.66","operatingSystem":"Windows 10"}},"chatId":"36416579612304584","timestamp":-62135596800,"messageId":null,"connectionId":"l7UdY5uuw_J3jkREP42fGwfebbdf651","traceId":"a2d7f5e8-c202-4218-832b-03f7a5e36528","success":true}]}
Code that I have created to overcome this issue is as follows.
def parse_signalr_response(json_str: str) -> List[SignalrResponse]:
Following code works i.e if triangle is at the end I am able to delete that triangle.
if (json_str.endswith('▲')):
json_str = json_str[:(len(json_str) - Constants.sigr_suffix_decode_len)]
elif (json_str.endswith(Constants.sigr_suffix_encoded1)):
json_str = json_str[:(len(json_str)-1)]
elif (json_str.endswith(Constants.sigr_suffix_encoded)):
json_str = json_str[:(len(json_str)-1)]
Following piece of code does not work, i.e if triangle is in the middle of response. then this code does not go in the if block.
# if (Constants.sigr_suffix_encoded1 in json_str):
if (json_str.__contains__('▲')):
# if (json_str.find('▲')):
# json_str.replace("▲", '')
base_sigr_response: SignalrResponse = JsonHelper.parse_json(json_str, SignalrResponse)
if (base_sigr_response.type == SignalrResponseType.Callback):
return _parse_signalr_callback(json_str)
elif (base_sigr_response.type == SignalrResponseType.Response):
if hasattr(base_sigr_response, 'error'):
return [_parse_signalr_error(json_str)]
else:
return [_parse_signalr_success(json_str)]
return [base_sigr_response]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的问题似乎是您在单个响应中收到多个 JSON 对象。
▲
是应分割响应的位置,以便您可以正确解析两个对象。Your problem seems to be that you are receiving multiple JSON objects in a single response.
▲
is where the response should be split so you can properly parse both objects.