关于 Python 中 websocket-client 库的使用有一些疑惑
資深大佬 : black201w 21
想用 websocket-client 库和这个项目 https://github.com/Chocolatl/qqlight-websocket 写一个 QQ 机器人,我的 python 代码大致如下
import time import websocket import json class QLWebSocket: def __init__(self, address="127.0.0.1", port=2333, path="/"): def on_open(*args): def run(*args): # 发送这段 json,服务器返回现在登录的 QQ 号,返回的 json 如下 # {"id": "1.048596", "result": "114514"} # id 与发送的 id 相同,result 为 QQ 号 self.ws.send('{"id": "1.048596","method": "getLoginAccount"}') thread.start_new_thread(run, ()) ws_link = "ws://%s:%d%s" % (address, port, path) self.ws = websocket.WebSocketApp(ws_link, on_message=self.on_message, on_close=self.on_close) self.ws.on_open = on_open def on_message(self, message): # message 为服务器发送的 json 字符串 print(message) def get_cookies(self): self.ws.send('{"id": "1.16584161","method": "getCookies"}') bot = QLWebSocket() if __name__ == '__main__': bot.run()
现在的问题就是,只要是服务器发送过来的 json 都会由 on_message()函数处理。
我想要的效果是比如在我执行 get_cookies()函数时,能直接通过 get_cookies()返回获取到的 cookie 字符串
所以请问各位大佬应该怎么实现这个效果呢?
大佬有話說 (4)