首页 > 解决方案 > 如何获得 websocket 连接的输出?

问题描述

如何在 python 中获取 websocket 连接的输出?

我已成功创建到网站的 websocket 连接,但我不知道如何获取代码中最后一个函数的特定输出。这是我的代码:

def on_open(ws):

    authentication = {
        'action': 'authenticate',
        'data': {'key': ''}
        }

    ws.send(json.dumps(authentication))

    listen = {'action': 'listen', 'data': {'streams': ['SomeName']}}

    ws.send(json.dumps(listen))

socket = 'SomeWebsite'

ws = websocket.WebSocketApp(socket, on_open)
ws.run_forever()

我想获取函数“ws.run_forever()”的输出,它是 json 格式的。如何获取这些输出并将它们放入例如字典中?

标签: pythonapiwebsocket

解决方案


推荐阅读