在 python 中调用 websocket 后无法解析响应——locust

发布于 2025-01-09 12:54:39 字数 2039 浏览 3 评论 0原文

我调用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

林空鹿饮溪 2025-01-16 12:54:39

您的问题似乎是您在单个响应中收到多个 JSON 对象。 是应分割响应的位置,以便您可以正确解析两个对象。

import json

test_string = # Your response string
s = test_string.split('▲')
for x in s:
    j = json.loads(x)
    print(j)

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.

import json

test_string = # Your response string
s = test_string.split('▲')
for x in s:
    j = json.loads(x)
    print(j)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文