{"id":2830,"date":"2025-03-16T12:19:00","date_gmt":"2025-03-16T04:19:00","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=2830"},"modified":"2025-03-16T12:19:00","modified_gmt":"2025-03-16T04:19:00","slug":"python3-%e7%bd%91%e7%bb%9c%e7%bc%96%e7%a8%8b7%ef%bc%9awebsocket-%e7%bc%96%e7%a8%8b%ef%bc%88%e5%8f%8c%e5%90%91%e5%ae%9e%e6%97%b6%e9%80%9a%e4%bf%a1%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/03\/16\/python3-%e7%bd%91%e7%bb%9c%e7%bc%96%e7%a8%8b7%ef%bc%9awebsocket-%e7%bc%96%e7%a8%8b%ef%bc%88%e5%8f%8c%e5%90%91%e5%ae%9e%e6%97%b6%e9%80%9a%e4%bf%a1%ef%bc%89\/","title":{"rendered":"Python3 \u7f51\u7edc\u7f16\u7a0b7\uff1aWebSocket \u7f16\u7a0b\uff08\u53cc\u5411\u5b9e\u65f6\u901a\u4fe1\uff09"},"content":{"rendered":"\n<p>\u5728\u4f20\u7edf HTTP \u8bf7\u6c42-\u54cd\u5e94\u6a21\u5f0f\u4e0b\uff0c\u5ba2\u6237\u7aef\u9700\u8981\u4e0d\u65ad\u8f6e\u8be2\u670d\u52a1\u5668\u4ee5\u83b7\u53d6\u6700\u65b0\u6570\u636e\u3002\u800c <strong>WebSocket<\/strong> \u5141\u8bb8\u670d\u52a1\u5668\u548c\u5ba2\u6237\u7aef\u5efa\u7acb<strong>\u5168\u53cc\u5de5\u901a\u4fe1<\/strong>\uff0c\u9002\u7528\u4e8e<strong>\u5b9e\u65f6\u804a\u5929\u3001\u5728\u7ebf\u6e38\u620f\u3001\u80a1\u7968\u884c\u60c5\u7b49\u573a\u666f<\/strong>\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>7.1 WebSocket \u57fa\u7840<\/strong><\/h2>\n\n\n\n<p>Python \u63d0\u4f9b\u4e86 <code>websockets<\/code> \u5e93\uff0c\u652f\u6301 WebSocket \u901a\u4fe1\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>7.1.1 \u5b89\u88c5 <code>websockets<\/code><\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>pip install websockets<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>7.1.2 WebSocket \u901a\u4fe1\u6d41\u7a0b<\/strong><\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u5ba2\u6237\u7aef\u53d1\u9001\u8fde\u63a5\u8bf7\u6c42<\/strong><\/li>\n\n\n\n<li><strong>\u670d\u52a1\u5668\u54cd\u5e94\u8fde\u63a5<\/strong><\/li>\n\n\n\n<li><strong>\u53cc\u5411\u901a\u4fe1<\/strong><\/li>\n\n\n\n<li><strong>\u4efb\u610f\u4e00\u65b9\u53ef\u4e3b\u52a8\u65ad\u5f00\u8fde\u63a5<\/strong><\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>7.2 WebSocket \u670d\u52a1\u5668<\/strong><\/h2>\n\n\n\n<p>\u6211\u4eec\u4f7f\u7528 <code>websockets<\/code> \u5e93\u521b\u5efa\u4e00\u4e2a WebSocket \u670d\u52a1\u5668\uff0c\u76d1\u542c <code>0.0.0.0:8765<\/code> \u7aef\u53e3\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import asyncio\nimport websockets\n\nasync def websocket_server(websocket, path):\n    print(\"\u5ba2\u6237\u7aef\u5df2\u8fde\u63a5\")\n    try:\n        async for message in websocket:\n            print(f\"\u6536\u5230\u6d88\u606f: {message}\")\n            await websocket.send(f\"\u670d\u52a1\u5668\u6536\u5230: {message}\")\n    except websockets.exceptions.ConnectionClosed:\n        print(\"\u5ba2\u6237\u7aef\u65ad\u5f00\u8fde\u63a5\")\n\nstart_server = websockets.serve(websocket_server, \"0.0.0.0\", 8765)\n\nasyncio.run(start_server)<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>7.3 WebSocket \u5ba2\u6237\u7aef<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>import asyncio\nimport websockets\n\nasync def websocket_client():\n    async with websockets.connect(\"ws:\/\/127.0.0.1:8765\") as websocket:\n        await websocket.send(\"Hello WebSocket Server!\")\n        response = await websocket.recv()\n        print(f\"\u6536\u5230\u670d\u52a1\u5668\u54cd\u5e94: {response}\")\n\nasyncio.run(websocket_client())<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>7.4 WebSocket \u591a\u5ba2\u6237\u7aef\u652f\u6301<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>7.4.1 \u670d\u52a1\u5668\u652f\u6301\u591a\u4e2a\u5ba2\u6237\u7aef<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>import asyncio\nimport websockets\n\nclients = set()\n\nasync def websocket_handler(websocket, path):\n    clients.add(websocket)\n    try:\n        async for message in websocket:\n            print(f\"\u6536\u5230\u6d88\u606f: {message}\")\n            for client in clients:\n                if client != websocket:\n                    await client.send(f\"\u5e7f\u64ad\u6d88\u606f: {message}\")\n    except websockets.exceptions.ConnectionClosed:\n        pass\n    finally:\n        clients.remove(websocket)\n\nstart_server = websockets.serve(websocket_handler, \"0.0.0.0\", 8765)\n\nasyncio.run(start_server)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>7.4.2 \u5ba2\u6237\u7aef\u8fde\u63a5\u591a\u4e2a WebSocket<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>import asyncio\nimport websockets\n\nasync def websocket_client(name):\n    async with websockets.connect(\"ws:\/\/127.0.0.1:8765\") as websocket:\n        await websocket.send(f\"{name} \u8fde\u63a5\u6210\u529f\")\n        while True:\n            response = await websocket.recv()\n            print(f\"{name} \u6536\u5230\u6d88\u606f: {response}\")\n\nasync def main():\n    await asyncio.gather(websocket_client(\"\u5ba2\u6237\u7aef1\"), websocket_client(\"\u5ba2\u6237\u7aef2\"))\n\nasyncio.run(main())<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>7.5 WebSocket \u4e0e\u6d4f\u89c8\u5668\u901a\u4fe1<\/strong><\/h2>\n\n\n\n<p>\u53ef\u4ee5\u5728<strong>\u524d\u7aef HTML \u9875\u9762<\/strong>\u4e2d\u4f7f\u7528 WebSocket \u8fde\u63a5\u670d\u52a1\u5668\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>7.5.1 \u524d\u7aef WebSocket \u4ee3\u7801<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;!DOCTYPE html&gt;\n&lt;html lang=\"zh\"&gt;\n&lt;head&gt;\n    &lt;meta charset=\"UTF-8\"&gt;\n    &lt;title&gt;WebSocket \u5ba2\u6237\u7aef&lt;\/title&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n    &lt;h2&gt;WebSocket \u8fde\u63a5&lt;\/h2&gt;\n    &lt;button onclick=\"sendMessage()\"&gt;\u53d1\u9001\u6d88\u606f&lt;\/button&gt;\n    &lt;script&gt;\n        let ws = new WebSocket(\"ws:\/\/localhost:8765\");\n\n        ws.onopen = function () {\n            console.log(\"\u8fde\u63a5\u6210\u529f\uff01\");\n        };\n\n        ws.onmessage = function (event) {\n            console.log(\"\u6536\u5230\u6d88\u606f: \" + event.data);\n        };\n\n        function sendMessage() {\n            ws.send(\"Hello from Web!\");\n        }\n    &lt;\/script&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>\u4e0b\u7bc7\u6587\u7ae0\u6211\u4eec\u5c06\u4ecb\u7ecd <code>Twisted<\/code>\uff0c\u7528\u4e8e\u66f4\u9ad8\u7ea7\u7684\u7f51\u7edc\u7f16\u7a0b\uff0c\u5982 Telnet \u670d\u52a1\u5668\uff01<\/strong> <\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u4f20\u7edf HTTP \u8bf7\u6c42-\u54cd\u5e94\u6a21\u5f0f\u4e0b\uff0c\u5ba2\u6237\u7aef\u9700\u8981\u4e0d\u65ad\u8f6e\u8be2\u670d\u52a1\u5668\u4ee5\u83b7\u53d6\u6700\u65b0\u6570\u636e\u3002\u800c WebSocket \u5141\u8bb8\u670d\u52a1\u5668 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2831,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[79],"tags":[],"class_list":["post-2830","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-3-"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2830","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=2830"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2830\/revisions"}],"predecessor-version":[{"id":2832,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2830\/revisions\/2832"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/2831"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=2830"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=2830"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=2830"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}