{"id":24741,"date":"2025-04-19T10:42:13","date_gmt":"2025-04-19T02:42:13","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/24741.html"},"modified":"2025-04-19T10:42:13","modified_gmt":"2025-04-19T02:42:13","slug":"%e4%bd%bf%e7%94%a8-python-%e5%ae%9e%e7%8e%b0-websocket-%e6%9c%8d%e5%8a%a1%e5%99%a8%e4%b8%8e%e5%ae%a2%e6%88%b7%e7%ab%af%e9%80%9a%e4%bf%a1","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/24741.html","title":{"rendered":"\u4f7f\u7528 Python \u5b9e\u73b0 WebSocket \u670d\u52a1\u5668\u4e0e\u5ba2\u6237\u7aef\u901a\u4fe1"},"content":{"rendered":"<h4>\u7b80\u4ecb<\/h4>\n<p>WebSocket \u662f\u4e00\u79cd\u57fa\u4e8e TCP \u534f\u8bae\u7684\u901a\u4fe1\u534f\u8bae\uff0c\u80fd\u591f\u5728\u5ba2\u6237\u7aef\u4e0e\u670d\u52a1\u5668\u4e4b\u95f4\u8fdb\u884c\u5168\u53cc\u5de5\uff08\u53cc\u5411\uff09\u901a\u4fe1\u3002\u76f8\u6bd4\u4f20\u7edf\u7684 HTTP \u534f\u8bae\uff0cWebSocket \u53ef\u4ee5\u5b9e\u73b0\u5b9e\u65f6\u6570\u636e\u7684\u4f20\u8f93\uff0c\u5c24\u5176\u9002\u5408\u9700\u8981\u5b9e\u65f6\u4ea4\u4e92\u7684\u5e94\u7528\u573a\u666f\uff0c\u5982\u5728\u7ebf\u6e38\u620f\u3001\u5b9e\u65f6\u804a\u5929\u3001\u91d1\u878d\u4ea4\u6613\u7b49\u3002<\/p>\n<p>\u6211\u901a\u8fc7 Python \u5b9e\u73b0\u4e00\u4e2a\u7b80\u5355\u7684 WebSocket \u670d\u52a1\u5668\uff0c\u5e76\u4f7f\u5176\u4e0e\u5ba2\u6237\u7aef\u8fdb\u884c\u901a\u4fe1\u3002\u6211\u4eec\u5c06\u521b\u5efa\u4e24\u4e2a Python \u6587\u4ef6\uff1awebsocket.py \u548c main.py\uff0cwebsocket.py \u8d1f\u8d23\u5b9e\u73b0 WebSocket \u670d\u52a1\u5668\u7684\u529f\u80fd\uff0cmain.py \u8d1f\u8d23\u542f\u52a8\u548c\u7ba1\u7406\u670d\u52a1\u5668\uff0c\u4ee5\u53ca\u5b9a\u65f6\u5411\u5ba2\u6237\u7aef\u53d1\u9001\u6d88\u606f\u3002<\/p>\n<h4>\u5b89\u88c5 WebSocket \u5e93<\/h4>\n<p>\u5728\u5f00\u59cb\u4e4b\u524d\uff0c\u9700\u8981\u5b89\u88c5 websockets \u5e93\uff0c\u5b83\u662f Python \u4e2d\u975e\u5e38\u6d41\u884c\u7684 WebSocket \u5b9e\u73b0\u3002<\/p>\n<p>pip install websockets<\/p>\n<h4>websocket.py \u2014 WebSocket \u670d\u52a1\u5668\u5b9e\u73b0<\/h4>\n<p>\u5728 websocket.py \u6587\u4ef6\u4e2d\uff0c\u6211\u5b9a\u4e49\u4e86\u4e00\u4e2a WebSocketServer \u7c7b\uff0c\u5305\u542b\u4e86 WebSocket \u670d\u52a1\u5668\u7684\u4e3b\u8981\u903b\u8f91\u3002<\/p>\n<h5>\u4ee3\u7801\u89e3\u6790<\/h5>\n<p><span class=\"token keyword\">import<\/span> asyncio<br \/>\n<span class=\"token keyword\">import<\/span> websockets<\/p>\n<p><span class=\"token keyword\">class<\/span> <span class=\"token class-name\">WebSocketServer<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    <span class=\"token keyword\">def<\/span> <span class=\"token function\">__init__<\/span><span class=\"token punctuation\">(<\/span>self<span class=\"token punctuation\">,<\/span> host<span class=\"token operator\">=<\/span><span class=\"token string\">\\&#8221;localhost\\&#8221;<\/span><span class=\"token punctuation\">,<\/span> port<span class=\"token operator\">=<\/span><span class=\"token number\">8765<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n        self<span class=\"token punctuation\">.<\/span>host <span class=\"token operator\">=<\/span> host<br \/>\n        self<span class=\"token punctuation\">.<\/span>port <span class=\"token operator\">=<\/span> port<br \/>\n        self<span class=\"token punctuation\">.<\/span>clients <span class=\"token operator\">=<\/span> <span class=\"token builtin\">set<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u5b58\u50a8\u6240\u6709\u8fde\u63a5\u7684\u5ba2\u6237\u7aef<\/span><\/p>\n<p>\u9996\u5148\uff0c\u6211\u521b\u5efa\u4e86\u4e00\u4e2a WebSocketServer \u7c7b\uff0c\u5b83\u7684\u6784\u9020\u65b9\u6cd5\u521d\u59cb\u5316\u4e86\u670d\u52a1\u5668\u7684\u4e3b\u673a\u5730\u5740 host \u548c\u7aef\u53e3\u53f7 port\uff0c\u540c\u65f6\u7ef4\u62a4\u4e86\u4e00\u4e2a\u5ba2\u6237\u7aef\u96c6\u5408 clients \u6765\u5b58\u50a8\u5f53\u524d\u8fde\u63a5\u7684 WebSocket \u5ba2\u6237\u7aef\u3002<\/p>\n<p>    <span class=\"token keyword\">async<\/span> <span class=\"token keyword\">def<\/span> <span class=\"token function\">handle_client<\/span><span class=\"token punctuation\">(<\/span>self<span class=\"token punctuation\">,<\/span> websocket<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n        <span class=\"token comment\"># \u65b0\u7684\u5ba2\u6237\u7aef\u8fde\u63a5<\/span><br \/>\n        self<span class=\"token punctuation\">.<\/span>clients<span class=\"token punctuation\">.<\/span>add<span class=\"token punctuation\">(<\/span>websocket<span class=\"token punctuation\">)<\/span><br \/>\n        <span class=\"token keyword\">try<\/span><span class=\"token punctuation\">:<\/span><br \/>\n            <span class=\"token keyword\">async<\/span> <span class=\"token keyword\">for<\/span> message <span class=\"token keyword\">in<\/span> websocket<span class=\"token punctuation\">:<\/span><br \/>\n                <span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string-interpolation\"><span class=\"token string\">f\\&#8221;\u6536\u5230\u6d88\u606f: <\/span><span class=\"token interpolation\"><span class=\"token punctuation\">{<br \/>\n     <\/span>message<span class=\"token punctuation\">}<\/span><\/span><span class=\"token string\">\\&#8221;<\/span><\/span><span class=\"token punctuation\">)<\/span><br \/>\n                <span class=\"token comment\"># \u56de\u663e\u6d88\u606f\u7ed9\u5ba2\u6237\u7aef<\/span><br \/>\n                <span class=\"token keyword\">await<\/span> websocket<span class=\"token punctuation\">.<\/span>send<span class=\"token punctuation\">(<\/span><span class=\"token string-interpolation\"><span class=\"token string\">f\\&#8221;\u670d\u52a1\u5668\u5df2\u6536\u5230: <\/span><span class=\"token interpolation\"><span class=\"token punctuation\">{<br \/>\n     <\/span>message<span class=\"token punctuation\">}<\/span><\/span><span class=\"token string\">\\&#8221;<\/span><\/span><span class=\"token punctuation\">)<\/span><br \/>\n        <span class=\"token keyword\">except<\/span> websockets<span class=\"token punctuation\">.<\/span>ConnectionClosed <span class=\"token keyword\">as<\/span> e<span class=\"token punctuation\">:<\/span><br \/>\n            <span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string-interpolation\"><span class=\"token string\">f\\&#8221;\u5ba2\u6237\u7aef\u65ad\u5f00\u8fde\u63a5: <\/span><span class=\"token interpolation\"><span class=\"token punctuation\">{<br \/>\n     <\/span>e<span class=\"token punctuation\">}<\/span><\/span><span class=\"token string\">\\&#8221;<\/span><\/span><span class=\"token punctuation\">)<\/span><br \/>\n        <span class=\"token keyword\">finally<\/span><span class=\"token punctuation\">:<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb3.4k\u6b21\uff0c\u70b9\u8d5e10\u6b21\uff0c\u6536\u85cf11\u6b21\u3002\u4f7f\u7528 Python \u5b9e\u73b0\u4e00\u4e2a\u7b80\u5355\u7684 WebSocket \u670d\u52a1\u5668\uff0c\u652f\u6301\u5ba2\u6237\u7aef\u8fde\u63a5\u3001\u6d88\u606f\u56de\u663e\u548c\u5b9a\u65f6\u5411\u5ba2\u6237\u7aef\u53d1\u9001\u6d88\u606f\u3002_python websocket<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[81,947],"topic":[],"class_list":["post-24741","post","type-post","status-publish","format-standard","hentry","category-server","tag-python","tag-websocket"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u4f7f\u7528 Python \u5b9e\u73b0 WebSocket \u670d\u52a1\u5668\u4e0e\u5ba2\u6237\u7aef\u901a\u4fe1 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/24741.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u4f7f\u7528 Python \u5b9e\u73b0 WebSocket \u670d\u52a1\u5668\u4e0e\u5ba2\u6237\u7aef\u901a\u4fe1 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb3.4k\u6b21\uff0c\u70b9\u8d5e10\u6b21\uff0c\u6536\u85cf11\u6b21\u3002\u4f7f\u7528 Python \u5b9e\u73b0\u4e00\u4e2a\u7b80\u5355\u7684 WebSocket \u670d\u52a1\u5668\uff0c\u652f\u6301\u5ba2\u6237\u7aef\u8fde\u63a5\u3001\u6d88\u606f\u56de\u663e\u548c\u5b9a\u65f6\u5411\u5ba2\u6237\u7aef\u53d1\u9001\u6d88\u606f\u3002_python websocket\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/24741.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-04-19T02:42:13+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/24741.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/24741.html\",\"name\":\"\u4f7f\u7528 Python \u5b9e\u73b0 WebSocket \u670d\u52a1\u5668\u4e0e\u5ba2\u6237\u7aef\u901a\u4fe1 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-04-19T02:42:13+00:00\",\"dateModified\":\"2025-04-19T02:42:13+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/24741.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/24741.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/24741.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u4f7f\u7528 Python \u5b9e\u73b0 WebSocket \u670d\u52a1\u5668\u4e0e\u5ba2\u6237\u7aef\u901a\u4fe1\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u4f7f\u7528 Python \u5b9e\u73b0 WebSocket \u670d\u52a1\u5668\u4e0e\u5ba2\u6237\u7aef\u901a\u4fe1 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/24741.html","og_locale":"zh_CN","og_type":"article","og_title":"\u4f7f\u7528 Python \u5b9e\u73b0 WebSocket \u670d\u52a1\u5668\u4e0e\u5ba2\u6237\u7aef\u901a\u4fe1 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb3.4k\u6b21\uff0c\u70b9\u8d5e10\u6b21\uff0c\u6536\u85cf11\u6b21\u3002\u4f7f\u7528 Python \u5b9e\u73b0\u4e00\u4e2a\u7b80\u5355\u7684 WebSocket \u670d\u52a1\u5668\uff0c\u652f\u6301\u5ba2\u6237\u7aef\u8fde\u63a5\u3001\u6d88\u606f\u56de\u663e\u548c\u5b9a\u65f6\u5411\u5ba2\u6237\u7aef\u53d1\u9001\u6d88\u606f\u3002_python websocket","og_url":"https:\/\/www.wsisp.com\/helps\/24741.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-04-19T02:42:13+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/24741.html","url":"https:\/\/www.wsisp.com\/helps\/24741.html","name":"\u4f7f\u7528 Python \u5b9e\u73b0 WebSocket \u670d\u52a1\u5668\u4e0e\u5ba2\u6237\u7aef\u901a\u4fe1 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-04-19T02:42:13+00:00","dateModified":"2025-04-19T02:42:13+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/24741.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/24741.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/24741.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u4f7f\u7528 Python \u5b9e\u73b0 WebSocket \u670d\u52a1\u5668\u4e0e\u5ba2\u6237\u7aef\u901a\u4fe1"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/24741","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=24741"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/24741\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=24741"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=24741"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=24741"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=24741"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}