{"id":91404,"date":"2020-05-02T19:00:04","date_gmt":"2020-05-02T11:00:04","guid":{"rendered":"http:\/\/4563.org\/?p=91404"},"modified":"2020-05-02T19:00:04","modified_gmt":"2020-05-02T11:00:04","slug":"%e5%85%b3%e4%ba%8e-python-%e4%b8%ad-websocket-client-%e5%ba%93%e7%9a%84%e4%bd%bf%e7%94%a8%e6%9c%89%e4%b8%80%e4%ba%9b%e7%96%91%e6%83%91","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=91404","title":{"rendered":"\u5173\u4e8e Python \u4e2d websocket-client \u5e93\u7684\u4f7f\u7528\u6709\u4e00\u4e9b\u7591\u60d1"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u5173\u4e8e Python \u4e2d websocket-client \u5e93\u7684\u4f7f\u7528\u6709\u4e00\u4e9b\u7591\u60d1               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : black201w <\/span>  <span><i><\/i> 21<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u60f3\u7528 websocket-client \u5e93\u548c\u8fd9\u4e2a\u9879\u76ee <code>https:\/\/github.com\/Chocolatl\/qqlight-websocket<\/code> \u5199\u4e00\u4e2a QQ \u673a\u5668\u4eba\uff0c\u6211\u7684 python \u4ee3\u7801\u5927\u81f4\u5982\u4e0b<\/p>\n<pre><code>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):                           # \u53d1\u9001\u8fd9\u6bb5 json\uff0c\u670d\u52a1\u5668\u8fd4\u56de\u73b0\u5728\u767b\u5f55\u7684 QQ \u53f7\uff0c\u8fd4\u56de\u7684 json \u5982\u4e0b                 # {\"id\": \"1.048596\", \"result\": \"114514\"}                 # id \u4e0e\u53d1\u9001\u7684 id \u76f8\u540c\uff0cresult \u4e3a QQ \u53f7                                  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 \u4e3a\u670d\u52a1\u5668\u53d1\u9001\u7684 json \u5b57\u7b26\u4e32      print(message)              def get_cookies(self):      self.ws.send('{\"id\": \"1.16584161\",\"method\": \"getCookies\"}')                   bot = QLWebSocket()   if __name__ == '__main__':     bot.run() <\/code><\/pre>\n<p>\u73b0\u5728\u7684\u95ee\u9898\u5c31\u662f\uff0c\u53ea\u8981\u662f\u670d\u52a1\u5668\u53d1\u9001\u8fc7\u6765\u7684 json \u90fd\u4f1a\u7531 on_message()\u51fd\u6570\u5904\u7406\u3002<\/p>\n<p>\u6211\u60f3\u8981\u7684\u6548\u679c\u662f\u6bd4\u5982\u5728\u6211\u6267\u884c get_cookies()\u51fd\u6570\u65f6\uff0c\u80fd\u76f4\u63a5\u901a\u8fc7 get_cookies()\u8fd4\u56de\u83b7\u53d6\u5230\u7684 cookie \u5b57\u7b26\u4e32<\/p>\n<p>\u6240\u4ee5\u8bf7\u95ee\u5404\u4f4d\u5927\u4f6c\u5e94\u8be5\u600e\u4e48\u5b9e\u73b0\u8fd9\u4e2a\u6548\u679c\u5462\uff1f<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>4<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"1248221\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : black201w <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6551\u547d\u554a= =                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1248222\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : bnm965321 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6ca1\u770b\u61c2\u4f60\u5728\u8bf4\u5565\uff0c\u4f60\u5728\u54ea\u91cc\u8c03\u7528 get_cookies()\uff0c\u4e3a\u4ec0\u4e48\u4e0d\u80fd\u83b7\u53d6\u5b83\u7684\u8fd4\u56de\u503c\uff1f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1248223\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : black201w <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @bnm965321 \u6bd4\u5982\u8bf4\u6211\u5c31\u5728 main \u91cc\u8c03\u7528 bot.get_cookies()\uff0c\u670d\u52a1\u5668\u8fd4\u56de\u7ed9\u6211 json \u662f\u7ed9 on_message()\u5904\u7406\u4e86\uff0c\u4f46\u6211\u60f3\u8ba9\u5b83\u6210\u4e3a get_cookies()\u7684\u8fd4\u56de\u503c                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1248224\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : bnm965321 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4f60\u8981\u628a\u670d\u52a1\u5668\u7ed9\u4f60\u7684 cookies json \u5148\u7f13\u5b58\uff0cwebsocket \u662f\u63a8\u9001\u6a21\u578b\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u5173\u4e8e Python \u4e2d webso&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/91404"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=91404"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/91404\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=91404"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=91404"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=91404"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}