python模拟发送websocket

python模拟发送websocket请求的模块

pip3 install websocket-client 

基于 websocket-client 的示例代码

from websocket import WebSocketApp


def on_open(ws):
    print("on_open", ws)

def on_message(ws, message):
    # 回调函数,直接接收到xx的弹幕信息
    print("on_message", message)

def on_error(ws, message):
    print("on_error", ws, message)

def on_close(ws, **args, **kwargs):
    pass

def run():
    # 向xx发送web请求链接
    ws = WebSocketApp(
        url = "xxx",
        header={},
        cookie="",
        on_open=on_open,
        on_message=on_message,
        on_error=on_error,
        on_close=on_close
    )
    ws.run_forever()

if __name__ == '__main__':
    run()
posted @ 2024-11-10 14:50  流年中渲染了微笑  阅读(14)  评论(0编辑  收藏  举报