{"id":16648,"date":"2025-04-18T23:07:06","date_gmt":"2025-04-18T15:07:06","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/16648.html"},"modified":"2025-04-18T23:07:06","modified_gmt":"2025-04-18T15:07:06","slug":"%e7%b2%be%e9%80%9a%e6%9c%8d%e5%8a%a1%e5%99%a8%e6%8e%a8%e9%80%81%e4%ba%8b%e4%bb%b6%ef%bc%88sse%ef%bc%89%e4%b8%8e-python-%e5%92%8c-go-%e5%ae%9e%e7%8e%b0%e5%ae%9e%e6%97%b6%e6%95%b0%e6%8d%ae%e6%b5%81","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/16648.html","title":{"rendered":"\u7cbe\u901a\u670d\u52a1\u5668\u63a8\u9001\u4e8b\u4ef6\uff08SSE\uff09\u4e0e Python \u548c Go \u5b9e\u73b0\u5b9e\u65f6\u6570\u636e\u6d41 [\u7279\u6b8a\u5b57\u7b26]"},"content":{"rendered":"<p>\u5728\u5f53\u4eca\u7684\u4e92\u52a8\u578b Web \u5e94\u7528\u7a0b\u5e8f\u4e2d&#xff0c;\u5b9e\u65f6\u6570\u636e\u66f4\u65b0\u5728\u63d0\u5347\u7528\u6237\u4f53\u9a8c\u65b9\u9762\u8d77\u7740\u81f3\u5173\u91cd\u8981\u7684\u4f5c\u7528\u3002\u65e0\u8bba\u662f\u5b9e\u65f6\u80a1\u7968\u66f4\u65b0\u3001\u5373\u65f6\u804a\u5929\u6d88\u606f&#xff0c;\u8fd8\u662f\u6d41\u5f0f\u8bc4\u8bba&#xff0c;\u5b9e\u65f6\u6570\u636e\u6d41\u90fd\u662f\u4e0d\u53ef\u6216\u7f3a\u7684\u3002\u5728\u5404\u79cd\u53ef\u7528\u4e8e\u5b9e\u65f6\u901a\u4fe1\u7684\u6280\u672f\u4e2d&#xff0c;\u670d\u52a1\u5668\u63a8\u9001\u4e8b\u4ef6&#xff08;SSE&#xff09;\u4f5c\u4e3a\u4e00\u79cd\u5e7f\u6cdb\u4f7f\u7528\u4e14\u9ad8\u6548\u7684\u89e3\u51b3\u65b9\u6848\u8131\u9896\u800c\u51fa\u3002SSE\u00a0\u5141\u8bb8\u670d\u52a1\u5668\u901a\u8fc7 HTTP \u5411\u5ba2\u6237\u7aef\u63a8\u9001\u5b9e\u65f6\u66f4\u65b0&#xff0c;\u63d0\u4f9b\u4e86\u4e00\u79cd\u8f7b\u91cf\u4e14\u9ad8\u6548\u7684\u65b9\u5f0f\u3002<\/p>\n<h3>\u4e3a\u4ec0\u4e48\u9009\u62e9\u670d\u52a1\u5668\u63a8\u9001\u4e8b\u4ef6&#xff08;SSE&#xff09;&#xff1f; &#x1f914;<\/h3>\n<p>\u670d\u52a1\u5668\u63a8\u9001\u4e8b\u4ef6\u00a0\u662f HTML5 \u89c4\u8303\u7684\u4e00\u90e8\u5206&#xff0c;\u4e13\u95e8\u8bbe\u8ba1\u7528\u4e8e\u5c06\u4e8b\u4ef6\u4ece\u670d\u52a1\u5668\u63a8\u9001\u5230\u5ba2\u6237\u7aef\u3002\u5176\u7b80\u5355\u6027\u3001\u81ea\u52a8\u91cd\u8fde\u548c\u4e8b\u4ef6\u8ffd\u8e2a\u529f\u80fd&#xff0c;\u4f7f\u5176\u975e\u5e38\u9002\u5408\u9700\u8981\u6301\u7eed\u6570\u636e\u6d41\u7684\u573a\u666f\u3002\u5728\u5355\u5411\u6570\u636e\u6d41\u7684\u60c5\u51b5\u4e0b&#xff0c;SSE\u00a0\u5c24\u5176\u8868\u73b0\u51fa\u8272\u3002<\/p>\n<h3>\u6982\u8ff0 &#x1f4da;<\/h3>\n<p>\u670d\u52a1\u5668\u63a8\u9001\u4e8b\u4ef6&#xff08;SSE&#xff09;\u662f\u4e00\u9879\u6280\u672f&#xff0c;\u5141\u8bb8\u670d\u52a1\u5668\u5411\u6d4f\u89c8\u5668\u63a8\u9001\u5b9e\u65f6\u66f4\u65b0\u3002\u5b83\u662f HTML5 \u89c4\u8303\u7684\u4e00\u90e8\u5206&#xff0c;\u4e3b\u8981\u6d89\u53ca&#xff1a;<\/p>\n<li>\u901a\u4fe1\u534f\u8bae&#xff1a;\u4f7f\u7528 HTTP\u3002<\/li>\n<li>EventSource \u5bf9\u8c61&#xff1a;\u5728\u6d4f\u89c8\u5668\u7aef\u7684 JavaScript \u4e2d\u53ef\u7528\u3002<\/li>\n<p>\u867d\u7136 SSE \u548c WebSocket \u90fd\u652f\u6301\u4ece\u670d\u52a1\u5668\u5230\u5ba2\u6237\u7aef\u7684\u5b9e\u65f6\u901a\u4fe1&#xff0c;\u4f46\u5b83\u4eec\u6709\u4e00\u4e9b\u533a\u522b&#xff1a;<\/p>\n<table>\n<tr>SSEWebSocket<\/tr>\n<tbody>\n<tr>\n<td>\u57fa\u4e8e HTTP<\/td>\n<td>\u57fa\u4e8e TCP<\/td>\n<\/tr>\n<tr>\n<td>\u5355\u5411\u901a\u4fe1&#xff08;\u4ece\u670d\u52a1\u5668\u5230\u5ba2\u6237\u7aef&#xff09;<\/td>\n<td>\u53cc\u5411\u901a\u4fe1&#xff08;\u5168\u53cc\u5de5&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>\u8f7b\u91cf\u4e14\u7b80\u5355<\/td>\n<td>\u66f4\u590d\u6742<\/td>\n<\/tr>\n<tr>\n<td>\u5185\u7f6e\u91cd\u8fde\u548c\u6d88\u606f\u8ffd\u8e2a\u529f\u80fd<\/td>\n<td>\u9700\u8981\u624b\u52a8\u5b9e\u73b0\u8fd9\u4e9b\u529f\u80fd<\/td>\n<\/tr>\n<tr>\n<td>\u652f\u6301\u6587\u672c\u6216 Base64 \u7f16\u7801\u5e76\u538b\u7f29\u7684\u4e8c\u8fdb\u5236\u6570\u636e<\/td>\n<td>\u652f\u6301\u5404\u79cd\u6570\u636e\u7c7b\u578b<\/td>\n<\/tr>\n<tr>\n<td>\u652f\u6301\u81ea\u5b9a\u4e49\u4e8b\u4ef6\u7c7b\u578b<\/td>\n<td>\u4e0d\u652f\u6301\u81ea\u5b9a\u4e49\u4e8b\u4ef6\u7c7b\u578b<\/td>\n<\/tr>\n<tr>\n<td>\u9650\u5236\u5728 HTTP\/1.1 \u6216 HTTP\/2 \u8fde\u63a5\u6570\u4e0a<\/td>\n<td>\u8fde\u63a5\u6570\u65e0\u9650\u5236<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>\u670d\u52a1\u5668\u5b9e\u73b0 &#x1f310;<\/h3>\n<h4>\u534f\u8bae\u5b9e\u73b0<\/h4>\n<p>\u57fa\u672c\u4e0a&#xff0c;\u6d4f\u89c8\u5668\u53d1\u8d77\u4e00\u4e2a HTTP \u8bf7\u6c42&#xff0c;\u670d\u52a1\u5668\u8fd4\u56de HTTP \u72b6\u6001\u53ca\u6570\u636e&#xff0c;\u5e76\u5305\u542b\u4ee5\u4e0b\u5934\u4fe1\u606f&#xff1a;<\/p>\n<p>Content-Type: text\/event-stream<br \/>\nCache-Control: no-cache<br \/>\nConnection: keep-alive<\/p>\n<p>SSE \u6307\u5b9a\u4e8b\u4ef6\u6d41\u7684 MIME \u7c7b\u578b\u5fc5\u987b\u4e3a\u00a0text\/event-stream&#xff0c;\u6d4f\u89c8\u5668\u4e0d\u5e94\u7f13\u5b58\u6570\u636e&#xff0c;\u5e76\u4e14\u8fde\u63a5\u5e94\u4fdd\u6301\u6301\u4e45&#xff08;keep-alive&#xff09;\u3002<\/p>\n<h4>\u6d88\u606f\u683c\u5f0f<\/h4>\n<p>\u4e8b\u4ef6\u6d41\u662f UTF-8 \u7f16\u7801\u7684\u6587\u672c&#xff0c;\u6216\u7ecf\u8fc7 Base64 \u7f16\u7801\u5e76\u4f7f\u7528 gzip \u538b\u7f29\u7684\u4e8c\u8fdb\u5236\u6d88\u606f\u3002\u6bcf\u6761\u6d88\u606f\u7531\u4e00\u884c\u6216\u591a\u884c\u5b57\u6bb5\u7ec4\u6210&#xff0c;\u683c\u5f0f\u4e3a\u00a0field-name:\u00a0field-value\u3002\u6bcf\u4e2a\u5b57\u6bb5\u4ee5\u00a0\\\\n\u00a0\u7ed3\u675f\u3002\u4ee5\u5192\u53f7&#xff08;:&#xff09;\u5f00\u5934\u7684\u884c\u662f\u6ce8\u91ca&#xff0c;\u6d4f\u89c8\u5668\u4f1a\u5ffd\u7565\u8fd9\u4e9b\u884c\u3002\u6bcf\u6b21\u63a8\u9001\u53ef\u4ee5\u7531\u591a\u4e2a\u6d88\u606f\u7ec4\u6210&#xff0c;\u6d88\u606f\u4e4b\u95f4\u7528\u4e00\u4e2a\u7a7a\u884c&#xff08;\\\\n\\\\n&#xff09;\u5206\u9694\u3002<\/p>\n<h4>\u5173\u952e\u5b57\u6bb5\u5305\u62ec&#xff1a;<\/h4>\n<ul>\n<li>event&#xff1a;\u6307\u5b9a\u4e8b\u4ef6\u7c7b\u578b\u3002<\/li>\n<li>id&#xff1a;\u4e8b\u4ef6 ID&#xff0c;\u7528\u4e8e\u6d4f\u89c8\u5668\u8ddf\u8e2a\u6700\u540e\u63a5\u6536\u5230\u7684\u4e8b\u4ef6&#xff0c;\u4ee5\u4fbf\u91cd\u8fde\u65f6\u4f7f\u7528\u3002<\/li>\n<li>retry&#xff1a;\u5f53\u8fde\u63a5\u5931\u8d25\u65f6&#xff0c;\u6d4f\u89c8\u5668\u7b49\u5f85\u91cd\u65b0\u8fde\u63a5\u7684\u65f6\u95f4&#xff08;\u4ee5\u6beb\u79d2\u4e3a\u5355\u4f4d&#xff09;\u3002<\/li>\n<li>data&#xff1a;\u6d88\u606f\u6570\u636e\u3002<\/li>\n<\/ul>\n<p>\u793a\u4f8b&#xff1a;Python \u670d\u52a1\u5668\u5b9e\u73b0 SSE<\/p>\n<p>from flask import Flask, Response<\/p>\n<p>app &#061; Flask(__name__)<\/p>\n<p>&#064;app.route(&#039;\/events&#039;)<br \/>\ndef sse_handler():<br \/>\n    def generate():<br \/>\n        paragraph &#061; [<br \/>\n            &#034;Hello, this is an example of a continuous text output.&#034;,<br \/>\n            &#034;It contains multiple sentences, each of which will be sent to the client as an event.&#034;,<br \/>\n            &#034;This is to simulate the functionality of Server-Sent Events (SSE).&#034;,<br \/>\n            &#034;We can use this method to push real-time updates.&#034;,<br \/>\n            &#034;End of sample text, thank you!&#034;,<br \/>\n        ]<\/p>\n<p>        for sentence in paragraph:<br \/>\n            yield f&#034;data: {sentence}\\\\n\\\\n&#034;<br \/>\n            import time<br \/>\n            time.sleep(1)<\/p>\n<p>    return Response(generate(), mimetype&#061;&#039;text\/event-stream&#039;)<\/p>\n<p>if __name__ &#061;&#061; &#039;__main__&#039;:<br \/>\n    app.run(host&#061;&#039;0.0.0.0&#039;, port&#061;8081, debug&#061;True)<\/p>\n<p>\u793a\u4f8b&#xff1a;Go \u670d\u52a1\u5668\u5b9e\u73b0 SSE<\/p>\n<p>package main<\/p>\n<p>import (<br \/>\n    &#034;fmt&#034;<br \/>\n    &#034;log&#034;<br \/>\n    &#034;net\/http&#034;<br \/>\n    &#034;time&#034;<br \/>\n)<\/p>\n<p>func main() {<br \/>\n    http.HandleFunc(&#034;\/events&#034;, sseHandler)<\/p>\n<p>    fmt.Println(&#034;Starting server on :8080&#034;)<br \/>\n    if err :&#061; http.ListenAndServe(&#034;:8080&#034;, nil); err !&#061; nil {<br \/>\n       log.Fatalf(&#034;Server error: %v&#034;, err)<br \/>\n    }<br \/>\n}<\/p>\n<p>func sseHandler(w http.ResponseWriter, r *http.Request) {<br \/>\n    flusher, ok :&#061; w.(http.Flusher)<br \/>\n    if !ok {<br \/>\n       http.Error(w, &#034;Streaming unsupported!&#034;, http.StatusInternalServerError)<br \/>\n       return<br \/>\n    }<\/p>\n<p>    w.Header().Set(&#034;Content-Type&#034;, &#034;text\/event-stream&#034;)<br \/>\n    w.Header().Set(&#034;Cache-Control&#034;, &#034;no-cache&#034;)<br \/>\n    w.Header().Set(&#034;Connection&#034;, &#034;keep-alive&#034;)<\/p>\n<p>    paragraph :&#061; []string{<br \/>\n       &#034;Hello, this is an example of a continuous text output.&#034;,<br \/>\n       &#034;It contains multiple sentences, each of which will be sent to the client as an event.&#034;,<br \/>\n       &#034;This is to simulate the functionality of Server-Sent Events (SSE).&#034;,<br \/>\n       &#034;We can use this method to push real-time updates.&#034;,<br \/>\n       &#034;End of sample text, thank you!&#034;,<br \/>\n    }<\/p>\n<p>    for _, sentence :&#061; range paragraph {<br \/>\n       _, err :&#061; fmt.Fprintf(w, &#034;data: %s\\\\n\\\\n&#034;, sentence)<br \/>\n       if err !&#061; nil {<br \/>\n          return<br \/>\n       }<br \/>\n       flusher.Flush()<br \/>\n       time.Sleep(1 * time.Second) \/\/ Wait 1 second before sending the next piece of text<br \/>\n    }<br \/>\n}<\/p>\n<h3>\u6d4f\u89c8\u5668 API &#x1f5a5;\ufe0f<\/h3>\n<p>\u5728\u5ba2\u6237\u7aef&#xff0c;JavaScript \u7684\u00a0EventSource\u00a0API \u5141\u8bb8\u4f60\u521b\u5efa\u4e00\u4e2a\u00a0EventSource\u00a0\u5bf9\u8c61&#xff0c;\u76d1\u542c\u670d\u52a1\u5668\u53d1\u9001\u7684\u4e8b\u4ef6\u3002\u4e00\u65e6\u8fde\u63a5\u5efa\u7acb&#xff0c;\u670d\u52a1\u5668\u53ef\u4ee5\u901a\u8fc7\u5177\u6709\u00a0text\/event-stream\u00a0\u5185\u5bb9\u7c7b\u578b\u7684 HTTP \u54cd\u5e94\u5411\u6d4f\u89c8\u5668\u53d1\u9001\u4e8b\u4ef6\u6d88\u606f\u3002\u6d4f\u89c8\u5668\u53ef\u4ee5\u901a\u8fc7\u76d1\u542c\u00a0EventSource\u00a0\u5bf9\u8c61\u7684\u00a0onmessage\u3001onopen\u00a0\u548c\u00a0onerror\u00a0\u4e8b\u4ef6\u6765\u5904\u7406\u8fd9\u4e9b\u6d88\u606f\u3002<\/p>\n<p>&lt;!DOCTYPE html&gt;<br \/>\n&lt;html lang&#061;&#034;en&#034;&gt;<br \/>\n&lt;head&gt;<br \/>\n    &lt;meta charset&#061;&#034;UTF-8&#034;&gt;<br \/>\n    &lt;meta name&#061;&#034;viewport&#034; content&#061;&#034;width&#061;device-width, initial-scale&#061;1.0&#034;&gt;<br \/>\n    &lt;title&gt;SSE Example &#x1f31f;&lt;\/title&gt;<br \/>\n&lt;\/head&gt;<br \/>\n&lt;body&gt;<br \/>\n    &lt;h1&gt;Server-Sent Events Example &#x1f680;&lt;\/h1&gt;<br \/>\n    &lt;div id&#061;&#034;messages&#034;&gt;&lt;\/div&gt;<br \/>\n    &lt;script&gt;<br \/>\n        window.onload &#061; function() {<br \/>\n            if (typeof(EventSource) !&#061;&#061; &#034;undefined&#034;) {<br \/>\n                const eventSource &#061; new EventSource(&#039;\/events&#039;);<\/p>\n<p>                eventSource.onmessage &#061; function(event) {<br \/>\n                    const newElement &#061; document.createElement(&#034;p&#034;);<br \/>\n                    newElement.textContent &#061; &#034;Message: &#034; &#043; event.data;<\/p>\n<p>                    document.getElementById(&#034;messages&#034;).appendChild(newElement);<br \/>\n                };<\/p>\n<p>                eventSource.onerror &#061; function(event) {<br \/>\n                    console.error(&#034;Error occurred: &#034;, event);<br \/>\n                    const newElement &#061; document.createElement(&#034;p&#034;);<br \/>\n                    newElement.textContent &#061; &#034;An error occurred while connecting to the event source.&#034;;<br \/>\n                    document.getElementById(&#034;messages&#034;).appendChild(newElement);<br \/>\n                    eventSource.close();<br \/>\n                };<br \/>\n            } else {<br \/>\n                document.getElementById(&#034;messages&#034;).textContent &#061; &#034;Sorry, your browser does not support server-sent events&#8230;&#034;;<br \/>\n            }<br \/>\n        };<br \/>\n    &lt;\/script&gt;<br \/>\n&lt;\/body&gt;<br \/>\n&lt;\/html&gt;<\/p>\n<h3>\u7ed3\u8bba &#x1f3c1;<\/h3>\n<p>SSE \u662f\u4e00\u79cd\u57fa\u4e8e HTTP \u534f\u8bae\u7684\u8f7b\u91cf\u7ea7\u5b9e\u65f6\u901a\u4fe1\u6280\u672f\u3002\u5b83\u5728\u670d\u52a1\u5668\u9a71\u52a8\u7684\u4e8b\u4ef6\u3001\u81ea\u52a8\u91cd\u8fde\u548c\u5411\u5ba2\u6237\u7aef\u63a8\u9001\u66f4\u65b0\u65b9\u9762\u8868\u73b0\u51fa\u8272\u3002\u7136\u800c&#xff0c;SSE \u4e5f\u6709\u4e00\u4e9b\u5c40\u9650\u6027&#xff0c;\u5982\u5355\u5411\u901a\u4fe1\u3001\u8fde\u63a5\u6570\u9650\u5236\u548c\u4ec5\u9650\u4e8e GET \u8bf7\u6c42\u3002\u5b83\u975e\u5e38\u9002\u5408\u50cf\u5b9e\u65f6\u80a1\u7968\u66f4\u65b0\u3001\u65e5\u5fd7\u63a8\u9001\u548c\u804a\u5929\u623f\u95f4\u4e2d\u7684\u5b9e\u65f6\u7528\u6237\u8ba1\u6570\u7b49\u573a\u666f\u3002<\/p>\n<p>\u5bf9\u4e8e\u9700\u8981\u9ad8\u5e76\u53d1\u3001\u9ad8\u541e\u5410\u91cf\u548c\u4f4e\u5ef6\u8fdf\u7684\u573a\u666f&#xff0c;WebSocket \u53ef\u80fd\u662f\u66f4\u597d\u7684\u9009\u62e9\u3002\u76f8\u53cd&#xff0c;SSE \u66f4\u9002\u5408\u7b80\u5355\u3001\u8f7b\u91cf\u7684\u63a8\u9001\u573a\u666f\u3002\u5728\u9009\u62e9\u5b9e\u65f6\u66f4\u65b0\u89e3\u51b3\u65b9\u6848\u65f6&#xff0c;\u8bf7\u6839\u636e\u5e94\u7528\u7a0b\u5e8f\u7684\u5177\u4f53\u9700\u6c42\u548c\u80cc\u666f\u505a\u51fa\u9009\u62e9\u3002<\/p>\n<p>\u901a\u8fc7\u9075\u5faa\u63d0\u4f9b\u7684\u5b9e\u73b0\u7ec6\u8282\u548c\u793a\u4f8b&#xff0c;\u4f60\u5c06\u80fd\u591f\u5c06 SSE \u96c6\u6210\u5230\u4f60\u7684\u9879\u76ee\u4e2d&#xff0c;\u5e76\u6a21\u62df\u7c7b\u4f3c ChatGPT \u7684\u6570\u636e\u6d41&#xff0c;\u63d0\u5347\u7528\u6237\u4f53\u9a8c\u3002 &#x1f680;&#x1f525;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb813\u6b21\uff0c\u70b9\u8d5e5\u6b21\uff0c\u6536\u85cf8\u6b21\u3002\u670d\u52a1\u5668\u63a8\u9001\u4e8b\u4ef6\uff08SSE\uff09\u662f\u4e00\u9879\u6280\u672f\uff0c\u5141\u8bb8\u670d\u52a1\u5668\u5411\u6d4f\u89c8\u5668\u63a8\u9001\u5b9e\u65f6\u66f4\u65b0\u3002\u901a\u4fe1\u534f\u8bae\uff1a\u4f7f\u7528 HTTP\u3002EventSource \u5bf9\u8c61\uff1a\u5728\u6d4f\u89c8\u5668\u7aef\u7684 JavaScript \u4e2d\u53ef\u7528\u3002SSEWebSocket\u57fa\u4e8e HTTP\u57fa\u4e8e TCP\u5355\u5411\u901a\u4fe1\uff08\u4ece\u670d\u52a1\u5668\u5230\u5ba2\u6237\u7aef\uff09\u53cc\u5411\u901a\u4fe1\uff08\u5168\u53cc\u5de5\uff09\u8f7b\u91cf\u4e14\u7b80\u5355\u66f4\u590d\u6742\u5185\u7f6e\u91cd\u8fde\u548c\u6d88\u606f\u8ffd\u8e2a\u529f\u80fd\u9700\u8981\u624b\u52a8\u5b9e\u73b0\u8fd9\u4e9b\u529f\u80fd\u652f\u6301\u6587\u672c\u6216 Base64 \u7f16\u7801\u5e76\u538b\u7f29\u7684\u4e8c\u8fdb\u5236\u6570\u636e\u652f\u6301\u5404\u79cd\u6570\u636e\u7c7b\u578b\u652f\u6301\u81ea\u5b9a\u4e49\u4e8b\u4ef6\u7c7b\u578b\u4e0d\u652f\u6301\u81ea\u5b9a\u4e49\u4e8b\u4ef6\u7c7b\u578b\u9650\u5236\u5728 HTTP\/1.1 \u6216 HTTP\/2 \u8fde\u63a5\u6570\u4e0a\u3002_python sse sevet<\/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":[746,81,43],"topic":[],"class_list":["post-16648","post","type-post","status-publish","format-standard","hentry","category-server","tag-golang","tag-python","tag-43"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u7cbe\u901a\u670d\u52a1\u5668\u63a8\u9001\u4e8b\u4ef6\uff08SSE\uff09\u4e0e Python \u548c Go \u5b9e\u73b0\u5b9e\u65f6\u6570\u636e\u6d41 [\u7279\u6b8a\u5b57\u7b26] - \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\/16648.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u7cbe\u901a\u670d\u52a1\u5668\u63a8\u9001\u4e8b\u4ef6\uff08SSE\uff09\u4e0e Python \u548c Go \u5b9e\u73b0\u5b9e\u65f6\u6570\u636e\u6d41 [\u7279\u6b8a\u5b57\u7b26] - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb813\u6b21\uff0c\u70b9\u8d5e5\u6b21\uff0c\u6536\u85cf8\u6b21\u3002\u670d\u52a1\u5668\u63a8\u9001\u4e8b\u4ef6\uff08SSE\uff09\u662f\u4e00\u9879\u6280\u672f\uff0c\u5141\u8bb8\u670d\u52a1\u5668\u5411\u6d4f\u89c8\u5668\u63a8\u9001\u5b9e\u65f6\u66f4\u65b0\u3002\u901a\u4fe1\u534f\u8bae\uff1a\u4f7f\u7528 HTTP\u3002EventSource \u5bf9\u8c61\uff1a\u5728\u6d4f\u89c8\u5668\u7aef\u7684 JavaScript \u4e2d\u53ef\u7528\u3002SSEWebSocket\u57fa\u4e8e HTTP\u57fa\u4e8e TCP\u5355\u5411\u901a\u4fe1\uff08\u4ece\u670d\u52a1\u5668\u5230\u5ba2\u6237\u7aef\uff09\u53cc\u5411\u901a\u4fe1\uff08\u5168\u53cc\u5de5\uff09\u8f7b\u91cf\u4e14\u7b80\u5355\u66f4\u590d\u6742\u5185\u7f6e\u91cd\u8fde\u548c\u6d88\u606f\u8ffd\u8e2a\u529f\u80fd\u9700\u8981\u624b\u52a8\u5b9e\u73b0\u8fd9\u4e9b\u529f\u80fd\u652f\u6301\u6587\u672c\u6216 Base64 \u7f16\u7801\u5e76\u538b\u7f29\u7684\u4e8c\u8fdb\u5236\u6570\u636e\u652f\u6301\u5404\u79cd\u6570\u636e\u7c7b\u578b\u652f\u6301\u81ea\u5b9a\u4e49\u4e8b\u4ef6\u7c7b\u578b\u4e0d\u652f\u6301\u81ea\u5b9a\u4e49\u4e8b\u4ef6\u7c7b\u578b\u9650\u5236\u5728 HTTP\/1.1 \u6216 HTTP\/2 \u8fde\u63a5\u6570\u4e0a\u3002_python sse sevet\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/16648.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-18T15:07:06+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\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/16648.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/16648.html\",\"name\":\"\u7cbe\u901a\u670d\u52a1\u5668\u63a8\u9001\u4e8b\u4ef6\uff08SSE\uff09\u4e0e Python \u548c Go \u5b9e\u73b0\u5b9e\u65f6\u6570\u636e\u6d41 [\u7279\u6b8a\u5b57\u7b26] - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-04-18T15:07:06+00:00\",\"dateModified\":\"2025-04-18T15:07:06+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/16648.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/16648.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/16648.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u7cbe\u901a\u670d\u52a1\u5668\u63a8\u9001\u4e8b\u4ef6\uff08SSE\uff09\u4e0e Python \u548c Go \u5b9e\u73b0\u5b9e\u65f6\u6570\u636e\u6d41 [\u7279\u6b8a\u5b57\u7b26]\"}]},{\"@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":"\u7cbe\u901a\u670d\u52a1\u5668\u63a8\u9001\u4e8b\u4ef6\uff08SSE\uff09\u4e0e Python \u548c Go \u5b9e\u73b0\u5b9e\u65f6\u6570\u636e\u6d41 [\u7279\u6b8a\u5b57\u7b26] - \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\/16648.html","og_locale":"zh_CN","og_type":"article","og_title":"\u7cbe\u901a\u670d\u52a1\u5668\u63a8\u9001\u4e8b\u4ef6\uff08SSE\uff09\u4e0e Python \u548c Go \u5b9e\u73b0\u5b9e\u65f6\u6570\u636e\u6d41 [\u7279\u6b8a\u5b57\u7b26] - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb813\u6b21\uff0c\u70b9\u8d5e5\u6b21\uff0c\u6536\u85cf8\u6b21\u3002\u670d\u52a1\u5668\u63a8\u9001\u4e8b\u4ef6\uff08SSE\uff09\u662f\u4e00\u9879\u6280\u672f\uff0c\u5141\u8bb8\u670d\u52a1\u5668\u5411\u6d4f\u89c8\u5668\u63a8\u9001\u5b9e\u65f6\u66f4\u65b0\u3002\u901a\u4fe1\u534f\u8bae\uff1a\u4f7f\u7528 HTTP\u3002EventSource \u5bf9\u8c61\uff1a\u5728\u6d4f\u89c8\u5668\u7aef\u7684 JavaScript \u4e2d\u53ef\u7528\u3002SSEWebSocket\u57fa\u4e8e HTTP\u57fa\u4e8e TCP\u5355\u5411\u901a\u4fe1\uff08\u4ece\u670d\u52a1\u5668\u5230\u5ba2\u6237\u7aef\uff09\u53cc\u5411\u901a\u4fe1\uff08\u5168\u53cc\u5de5\uff09\u8f7b\u91cf\u4e14\u7b80\u5355\u66f4\u590d\u6742\u5185\u7f6e\u91cd\u8fde\u548c\u6d88\u606f\u8ffd\u8e2a\u529f\u80fd\u9700\u8981\u624b\u52a8\u5b9e\u73b0\u8fd9\u4e9b\u529f\u80fd\u652f\u6301\u6587\u672c\u6216 Base64 \u7f16\u7801\u5e76\u538b\u7f29\u7684\u4e8c\u8fdb\u5236\u6570\u636e\u652f\u6301\u5404\u79cd\u6570\u636e\u7c7b\u578b\u652f\u6301\u81ea\u5b9a\u4e49\u4e8b\u4ef6\u7c7b\u578b\u4e0d\u652f\u6301\u81ea\u5b9a\u4e49\u4e8b\u4ef6\u7c7b\u578b\u9650\u5236\u5728 HTTP\/1.1 \u6216 HTTP\/2 \u8fde\u63a5\u6570\u4e0a\u3002_python sse sevet","og_url":"https:\/\/www.wsisp.com\/helps\/16648.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-04-18T15:07:06+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"3 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/16648.html","url":"https:\/\/www.wsisp.com\/helps\/16648.html","name":"\u7cbe\u901a\u670d\u52a1\u5668\u63a8\u9001\u4e8b\u4ef6\uff08SSE\uff09\u4e0e Python \u548c Go \u5b9e\u73b0\u5b9e\u65f6\u6570\u636e\u6d41 [\u7279\u6b8a\u5b57\u7b26] - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-04-18T15:07:06+00:00","dateModified":"2025-04-18T15:07:06+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/16648.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/16648.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/16648.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u7cbe\u901a\u670d\u52a1\u5668\u63a8\u9001\u4e8b\u4ef6\uff08SSE\uff09\u4e0e Python \u548c Go \u5b9e\u73b0\u5b9e\u65f6\u6570\u636e\u6d41 [\u7279\u6b8a\u5b57\u7b26]"}]},{"@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\/16648","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=16648"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/16648\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=16648"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=16648"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=16648"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=16648"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}