{"id":1083332,"date":"2025-01-08T12:57:01","date_gmt":"2025-01-08T04:57:01","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1083332.html"},"modified":"2025-01-08T12:57:03","modified_gmt":"2025-01-08T04:57:03","slug":"%e5%89%8d%e7%ab%af%e5%a6%82%e4%bd%95%e5%b0%86%e6%95%b0%e6%8d%ae%e4%bc%a0%e7%bb%99python-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1083332.html","title":{"rendered":"\u524d\u7aef\u5982\u4f55\u5c06\u6570\u636e\u4f20\u7ed9python"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24193935\/1803988e-c09d-412f-8dda-f9371f4f5fb4.webp\" alt=\"\u524d\u7aef\u5982\u4f55\u5c06\u6570\u636e\u4f20\u7ed9python\" \/><\/p>\n<p><p> <strong>\u524d\u7aef\u5c06\u6570\u636e\u4f20\u7ed9Python\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u5e38\u89c1\u65b9\u6cd5\uff1a\u4f7f\u7528HTTP\u8bf7\u6c42\u3001WebSocket\u3001\u8868\u5355\u63d0\u4ea4\u3001AJAX\u8bf7\u6c42\u3002<\/strong>\u5728\u8fd9\u4e9b\u65b9\u6cd5\u4e2d\uff0cHTTP\u8bf7\u6c42\u548cAJAX\u8bf7\u6c42\u662f\u6700\u5e38\u7528\u7684\uff0c\u56e0\u4e3a\u5b83\u4eec\u6613\u4e8e\u5b9e\u73b0\u4e14\u5e7f\u6cdb\u652f\u6301\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\uff0c\u5373\u4f7f\u7528HTTP\u8bf7\u6c42\u6765\u4f20\u9012\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528HTTP\u8bf7\u6c42<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528HTTP\u8bf7\u6c42\u662f\u524d\u7aef\u4e0e\u540e\u7aef\u8fdb\u884c\u6570\u636e\u4ea4\u4e92\u7684\u6700\u5e38\u89c1\u65b9\u5f0f\u4e4b\u4e00\u3002\u524d\u7aef\u53ef\u4ee5\u901a\u8fc7\u53d1\u9001HTTP\u8bf7\u6c42\uff08\u5982GET\u3001POST\u3001PUT\u3001DELETE\u7b49\uff09\u5c06\u6570\u636e\u4f20\u9012\u7ed9\u540e\u7aef\u7684Python\u7a0b\u5e8f\u3002\u4ee5\u4e0b\u662f\u5177\u4f53\u6b65\u9aa4\u548c\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><h4>1. \u8bbe\u7f6ePython\u540e\u7aef<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5728Python\u540e\u7aef\u8bbe\u7f6e\u4e00\u4e2aAPI\u63a5\u53e3\u6765\u63a5\u6536\u6570\u636e\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Flask\u6846\u67b6\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002Flask\u662f\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u7684Web\u6846\u67b6\uff0c\u975e\u5e38\u9002\u5408\u5feb\u901f\u6784\u5efa\u548c\u90e8\u7f72Web\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from flask import Flask, request, jsonify<\/p>\n<p>app = Flask(__name__)<\/p>\n<p>@app.route(&#39;\/data&#39;, methods=[&#39;POST&#39;])<\/p>\n<p>def receive_data():<\/p>\n<p>    data = request.json<\/p>\n<p>    print(data)<\/p>\n<p>    return jsonify({&quot;status&quot;: &quot;success&quot;, &quot;data_received&quot;: data}), 200<\/p>\n<p>if __name__ == &#39;__m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n__&#39;:<\/p>\n<p>    app.run(debug=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u524d\u7aef\u53d1\u9001HTTP\u8bf7\u6c42<\/h4>\n<\/p>\n<p><p>\u5728\u524d\u7aef\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528JavaScript\u7684<code>fetch<\/code> API\u6216\u5176\u4ed6\u7c7b\u4f3c\u7684\u5e93\uff08\u5982Axios\uff09\u6765\u53d1\u9001HTTP\u8bf7\u6c42\u3002\u8fd9\u91cc\u6211\u4eec\u4f7f\u7528<code>fetch<\/code> API\u4f5c\u4e3a\u793a\u4f8b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-html\">&lt;!DOCTYPE html&gt;<\/p>\n<p>&lt;html lang=&quot;en&quot;&gt;<\/p>\n<p>&lt;head&gt;<\/p>\n<p>    &lt;meta charset=&quot;UTF-8&quot;&gt;<\/p>\n<p>    &lt;title&gt;Send Data to Python&lt;\/title&gt;<\/p>\n<p>&lt;\/head&gt;<\/p>\n<p>&lt;body&gt;<\/p>\n<p>    &lt;button id=&quot;sendDataButton&quot;&gt;Send Data&lt;\/button&gt;<\/p>\n<p>    &lt;script&gt;<\/p>\n<p>        document.getElementById(&#39;sendDataButton&#39;).addEventListener(&#39;click&#39;, () =&gt; {<\/p>\n<p>            const data = {<\/p>\n<p>                name: &quot;John Doe&quot;,<\/p>\n<p>                age: 30,<\/p>\n<p>                email: &quot;john.doe@example.com&quot;<\/p>\n<p>            };<\/p>\n<p>            fetch(&#39;http:\/\/127.0.0.1:5000\/data&#39;, {<\/p>\n<p>                method: &#39;POST&#39;,<\/p>\n<p>                headers: {<\/p>\n<p>                    &#39;Content-Type&#39;: &#39;application\/json&#39;<\/p>\n<p>                },<\/p>\n<p>                body: JSON.stringify(data)<\/p>\n<p>            })<\/p>\n<p>            .then(response =&gt; response.json())<\/p>\n<p>            .then(data =&gt; {<\/p>\n<p>                console.log(&#39;Success:&#39;, data);<\/p>\n<p>            })<\/p>\n<p>            .catch((error) =&gt; {<\/p>\n<p>                console.error(&#39;Error:&#39;, error);<\/p>\n<p>            });<\/p>\n<p>        });<\/p>\n<p>    &lt;\/script&gt;<\/p>\n<p>&lt;\/body&gt;<\/p>\n<p>&lt;\/html&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u5f53\u7528\u6237\u70b9\u51fb\u6309\u94ae\u65f6\uff0cJavaScript\u4f1a\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u7528\u6237\u4fe1\u606f\u7684\u5bf9\u8c61\uff0c\u5e76\u4f7f\u7528<code>fetch<\/code> API\u5c06\u5176\u4f5c\u4e3aJSON\u53d1\u9001\u5230Python\u540e\u7aef\u7684<code>\/data<\/code>\u7aef\u70b9\u3002Python\u540e\u7aef\u63a5\u6536\u5230\u6570\u636e\u540e\uff0c\u4f1a\u5c06\u5176\u6253\u5370\u51fa\u6765\u5e76\u8fd4\u56de\u4e00\u4e2a\u6210\u529f\u7684\u54cd\u5e94\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528WebSocket<\/h3>\n<\/p>\n<p><p>WebSocket\u662f\u4e00\u79cd\u5168\u53cc\u5de5\u901a\u4fe1\u534f\u8bae\uff0c\u5141\u8bb8\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\u4e4b\u95f4\u5b9e\u65f6\u53cc\u5411\u901a\u4fe1\u3002\u76f8\u6bd4HTTP\u8bf7\u6c42\uff0cWebSocket\u66f4\u9002\u5408\u9700\u8981\u5b9e\u65f6\u66f4\u65b0\u6570\u636e\u7684\u5e94\u7528\u573a\u666f\uff0c\u5982\u5728\u7ebf\u804a\u5929\u3001\u6e38\u620f\u7b49\u3002<\/p>\n<\/p>\n<p><h4>1. \u8bbe\u7f6ePython\u540e\u7aef<\/h4>\n<\/p>\n<p><p>\u5728Python\u540e\u7aef\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Flask-SocketIO\u6269\u5c55\u6765\u5b9e\u73b0WebSocket\u901a\u4fe1\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from flask import Flask, render_template<\/p>\n<p>from flask_socketio import SocketIO, send<\/p>\n<p>app = Flask(__name__)<\/p>\n<p>app.config[&#39;SECRET_KEY&#39;] = &#39;secret!&#39;<\/p>\n<p>socketio = SocketIO(app)<\/p>\n<p>@socketio.on(&#39;message&#39;)<\/p>\n<p>def handle_message(msg):<\/p>\n<p>    print(&#39;Received message: &#39; + msg)<\/p>\n<p>    send(&#39;Message received: &#39; + msg)<\/p>\n<p>if __name__ == &#39;__main__&#39;:<\/p>\n<p>    socketio.run(app, debug=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u524d\u7aef\u4f7f\u7528WebSocket<\/h4>\n<\/p>\n<p><p>\u5728\u524d\u7aef\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528JavaScript\u7684<code>WebSocket<\/code>\u5bf9\u8c61\u6765\u5efa\u7acb\u548c\u7ba1\u7406WebSocket\u8fde\u63a5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-html\">&lt;!DOCTYPE html&gt;<\/p>\n<p>&lt;html lang=&quot;en&quot;&gt;<\/p>\n<p>&lt;head&gt;<\/p>\n<p>    &lt;meta charset=&quot;UTF-8&quot;&gt;<\/p>\n<p>    &lt;title&gt;WebSocket Example&lt;\/title&gt;<\/p>\n<p>&lt;\/head&gt;<\/p>\n<p>&lt;body&gt;<\/p>\n<p>    &lt;button id=&quot;sendWebSocketDataButton&quot;&gt;Send WebSocket Data&lt;\/button&gt;<\/p>\n<p>    &lt;script&gt;<\/p>\n<p>        const socket = new WebSocket(&#39;ws:\/\/127.0.0.1:5000&#39;);<\/p>\n<p>        socket.onopen = function(event) {<\/p>\n<p>            console.log(&#39;WebSocket is open now.&#39;);<\/p>\n<p>        };<\/p>\n<p>        socket.onmessage = function(event) {<\/p>\n<p>            console.log(&#39;Received message from server:&#39;, event.data);<\/p>\n<p>        };<\/p>\n<p>        document.getElementById(&#39;sendWebSocketDataButton&#39;).addEventListener(&#39;click&#39;, () =&gt; {<\/p>\n<p>            const data = {<\/p>\n<p>                name: &quot;Jane Doe&quot;,<\/p>\n<p>                age: 28,<\/p>\n<p>                email: &quot;jane.doe@example.com&quot;<\/p>\n<p>            };<\/p>\n<p>            socket.send(JSON.stringify(data));<\/p>\n<p>        });<\/p>\n<p>    &lt;\/script&gt;<\/p>\n<p>&lt;\/body&gt;<\/p>\n<p>&lt;\/html&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u5f53\u7528\u6237\u70b9\u51fb\u6309\u94ae\u65f6\uff0cJavaScript\u4f1a\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u7528\u6237\u4fe1\u606f\u7684\u5bf9\u8c61\uff0c\u5e76\u901a\u8fc7WebSocket\u8fde\u63a5\u5c06\u5176\u53d1\u9001\u5230Python\u540e\u7aef\u3002Python\u540e\u7aef\u63a5\u6536\u5230\u6d88\u606f\u540e\uff0c\u4f1a\u6253\u5370\u51fa\u6765\u5e76\u5c06\u54cd\u5e94\u53d1\u9001\u56de\u524d\u7aef\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u8868\u5355\u63d0\u4ea4<\/h3>\n<\/p>\n<p><p>\u8868\u5355\u63d0\u4ea4\u662f\u6700\u4f20\u7edf\u7684\u65b9\u5f0f\u4e4b\u4e00\uff0c\u9002\u7528\u4e8e\u7b80\u5355\u7684\u6570\u636e\u4f20\u8f93\u9700\u6c42\u3002\u524d\u7aef\u901a\u8fc7\u8868\u5355\u5c06\u6570\u636e\u63d0\u4ea4\u7ed9\u540e\u7aef\uff0c\u540e\u7aef\u5904\u7406\u540e\u8fd4\u56de\u54cd\u5e94\u3002<\/p>\n<\/p>\n<p><h4>1. \u8bbe\u7f6ePython\u540e\u7aef<\/h4>\n<\/p>\n<p><p>\u5728Python\u540e\u7aef\uff0c\u6211\u4eec\u53ef\u4ee5\u8bbe\u7f6e\u4e00\u4e2a\u8def\u7531\u6765\u63a5\u6536\u8868\u5355\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from flask import Flask, request, render_template<\/p>\n<p>app = Flask(__name__)<\/p>\n<p>@app.route(&#39;\/&#39;)<\/p>\n<p>def form():<\/p>\n<p>    return render_template(&#39;form.html&#39;)<\/p>\n<p>@app.route(&#39;\/submit&#39;, methods=[&#39;POST&#39;])<\/p>\n<p>def submit():<\/p>\n<p>    name = request.form[&#39;name&#39;]<\/p>\n<p>    age = request.form[&#39;age&#39;]<\/p>\n<p>    email = request.form[&#39;email&#39;]<\/p>\n<p>    print(f&quot;Name: {name}, Age: {age}, Email: {email}&quot;)<\/p>\n<p>    return f&quot;Received: Name={name}, Age={age}, Email={email}&quot;<\/p>\n<p>if __name__ == &#39;__main__&#39;:<\/p>\n<p>    app.run(debug=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u524d\u7aef\u8868\u5355\u63d0\u4ea4<\/h4>\n<\/p>\n<p><p>\u5728\u524d\u7aef\uff0c\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2aHTML\u8868\u5355\u6765\u63d0\u4ea4\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-html\">&lt;!DOCTYPE html&gt;<\/p>\n<p>&lt;html lang=&quot;en&quot;&gt;<\/p>\n<p>&lt;head&gt;<\/p>\n<p>    &lt;meta charset=&quot;UTF-8&quot;&gt;<\/p>\n<p>    &lt;title&gt;Form Example&lt;\/title&gt;<\/p>\n<p>&lt;\/head&gt;<\/p>\n<p>&lt;body&gt;<\/p>\n<p>    &lt;form action=&quot;\/submit&quot; method=&quot;post&quot;&gt;<\/p>\n<p>        &lt;label for=&quot;name&quot;&gt;Name:&lt;\/label&gt;<\/p>\n<p>        &lt;input type=&quot;text&quot; id=&quot;name&quot; name=&quot;name&quot;&gt;&lt;br&gt;<\/p>\n<p>        &lt;label for=&quot;age&quot;&gt;Age:&lt;\/label&gt;<\/p>\n<p>        &lt;input type=&quot;text&quot; id=&quot;age&quot; name=&quot;age&quot;&gt;&lt;br&gt;<\/p>\n<p>        &lt;label for=&quot;email&quot;&gt;Email:&lt;\/label&gt;<\/p>\n<p>        &lt;input type=&quot;email&quot; id=&quot;email&quot; name=&quot;email&quot;&gt;&lt;br&gt;<\/p>\n<p>        &lt;button type=&quot;submit&quot;&gt;Submit&lt;\/button&gt;<\/p>\n<p>    &lt;\/form&gt;<\/p>\n<p>&lt;\/body&gt;<\/p>\n<p>&lt;\/html&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u7528\u6237\u586b\u5199\u8868\u5355\u5e76\u63d0\u4ea4\u540e\uff0c\u6570\u636e\u4f1a\u901a\u8fc7POST\u8bf7\u6c42\u53d1\u9001\u5230Python\u540e\u7aef\u7684<code>\/submit<\/code>\u7aef\u70b9\u3002Python\u540e\u7aef\u63a5\u6536\u5230\u6570\u636e\u540e\uff0c\u4f1a\u5c06\u5176\u6253\u5370\u51fa\u6765\u5e76\u8fd4\u56de\u54cd\u5e94\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528AJAX\u8bf7\u6c42<\/h3>\n<\/p>\n<p><p>AJAX\uff08Asynchronous JavaScript and XML\uff09\u662f\u4e00\u79cd\u5728\u4e0d\u91cd\u65b0\u52a0\u8f7d\u6574\u4e2a\u9875\u9762\u7684\u60c5\u51b5\u4e0b\u66f4\u65b0\u7f51\u9875\u5185\u5bb9\u7684\u6280\u672f\u3002\u901a\u8fc7AJAX\u8bf7\u6c42\uff0c\u524d\u7aef\u53ef\u4ee5\u5f02\u6b65\u5730\u5c06\u6570\u636e\u4f20\u9012\u7ed9\u540e\u7aef\u5e76\u63a5\u6536\u54cd\u5e94\u3002<\/p>\n<\/p>\n<p><h4>1. \u8bbe\u7f6ePython\u540e\u7aef<\/h4>\n<\/p>\n<p><p>\u5728Python\u540e\u7aef\uff0c\u6211\u4eec\u53ef\u4ee5\u8bbe\u7f6e\u4e00\u4e2aAPI\u63a5\u53e3\u6765\u63a5\u6536AJAX\u8bf7\u6c42\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from flask import Flask, request, jsonify<\/p>\n<p>app = Flask(__name__)<\/p>\n<p>@app.route(&#39;\/ajax_data&#39;, methods=[&#39;POST&#39;])<\/p>\n<p>def receive_ajax_data():<\/p>\n<p>    data = request.json<\/p>\n<p>    print(data)<\/p>\n<p>    return jsonify({&quot;status&quot;: &quot;success&quot;, &quot;data_received&quot;: data}), 200<\/p>\n<p>if __name__ == &#39;__main__&#39;:<\/p>\n<p>    app.run(debug=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u524d\u7aef\u53d1\u9001AJAX\u8bf7\u6c42<\/h4>\n<\/p>\n<p><p>\u5728\u524d\u7aef\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528JavaScript\u7684<code>XMLHttpRequest<\/code>\u5bf9\u8c61\u6216\u5176\u4ed6\u7c7b\u4f3c\u7684\u5e93\uff08\u5982jQuery\uff09\u6765\u53d1\u9001AJAX\u8bf7\u6c42\u3002\u8fd9\u91cc\u6211\u4eec\u4f7f\u7528<code>XMLHttpRequest<\/code>\u4f5c\u4e3a\u793a\u4f8b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-html\">&lt;!DOCTYPE html&gt;<\/p>\n<p>&lt;html lang=&quot;en&quot;&gt;<\/p>\n<p>&lt;head&gt;<\/p>\n<p>    &lt;meta charset=&quot;UTF-8&quot;&gt;<\/p>\n<p>    &lt;title&gt;AJAX Example&lt;\/title&gt;<\/p>\n<p>&lt;\/head&gt;<\/p>\n<p>&lt;body&gt;<\/p>\n<p>    &lt;button id=&quot;sendAjaxDataButton&quot;&gt;Send AJAX Data&lt;\/button&gt;<\/p>\n<p>    &lt;script&gt;<\/p>\n<p>        document.getElementById(&#39;sendAjaxDataButton&#39;).addEventListener(&#39;click&#39;, () =&gt; {<\/p>\n<p>            const data = {<\/p>\n<p>                name: &quot;Alice&quot;,<\/p>\n<p>                age: 25,<\/p>\n<p>                email: &quot;alice@example.com&quot;<\/p>\n<p>            };<\/p>\n<p>            const xhr = new XMLHttpRequest();<\/p>\n<p>            xhr.open(&#39;POST&#39;, &#39;http:\/\/127.0.0.1:5000\/ajax_data&#39;, true);<\/p>\n<p>            xhr.setRequestHeader(&#39;Content-Type&#39;, &#39;application\/json;charset=UTF-8&#39;);<\/p>\n<p>            xhr.onreadystatechange = function() {<\/p>\n<p>                if (xhr.readyState === 4 &amp;&amp; xhr.status === 200) {<\/p>\n<p>                    console.log(&#39;Success:&#39;, JSON.parse(xhr.responseText));<\/p>\n<p>                }<\/p>\n<p>            };<\/p>\n<p>            xhr.send(JSON.stringify(data));<\/p>\n<p>        });<\/p>\n<p>    &lt;\/script&gt;<\/p>\n<p>&lt;\/body&gt;<\/p>\n<p>&lt;\/html&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u5f53\u7528\u6237\u70b9\u51fb\u6309\u94ae\u65f6\uff0cJavaScript\u4f1a\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u7528\u6237\u4fe1\u606f\u7684\u5bf9\u8c61\uff0c\u5e76\u4f7f\u7528<code>XMLHttpRequest<\/code>\u5bf9\u8c61\u5c06\u5176\u4f5c\u4e3aJSON\u53d1\u9001\u5230Python\u540e\u7aef\u7684<code>\/ajax_data<\/code>\u7aef\u70b9\u3002Python\u540e\u7aef\u63a5\u6536\u5230\u6570\u636e\u540e\uff0c\u4f1a\u5c06\u5176\u6253\u5370\u51fa\u6765\u5e76\u8fd4\u56de\u4e00\u4e2a\u6210\u529f\u7684\u54cd\u5e94\u3002<\/p>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0a\u4ecb\u7ecd\u4e86\u56db\u79cd\u5e38\u89c1\u7684\u524d\u7aef\u5c06\u6570\u636e\u4f20\u7ed9Python\u7684\u65b9\u6cd5\uff1a<strong>\u4f7f\u7528HTTP\u8bf7\u6c42\u3001WebSocket\u3001\u8868\u5355\u63d0\u4ea4\u3001AJAX\u8bf7\u6c42<\/strong>\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u9002\u7528\u7684\u573a\u666f\u548c\u4f18\u7f3a\u70b9\u3002\u5bf9\u4e8e\u5927\u591a\u6570\u5e94\u7528\u7a0b\u5e8f\u6765\u8bf4\uff0c\u4f7f\u7528HTTP\u8bf7\u6c42\u6216AJAX\u8bf7\u6c42\u662f\u6700\u5e38\u89c1\u548c\u6700\u6613\u4e8e\u5b9e\u73b0\u7684\u65b9\u5f0f\u3002\u8fd9\u4e9b\u65b9\u6cd5\u4e0d\u4ec5\u7b80\u6d01\u660e\u4e86\uff0c\u800c\u4e14\u5177\u6709\u5e7f\u6cdb\u7684\u652f\u6301\u548c\u826f\u597d\u7684\u517c\u5bb9\u6027\u3002\u5e0c\u671b\u672c\u6587\u80fd\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u5b9e\u73b0\u524d\u7aef\u4e0ePython\u540e\u7aef\u7684\u6570\u636e\u4f20\u9012\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u524d\u7aef\u5982\u4f55\u5c06\u6570\u636e\u4f20\u7ed9Python\u7684\u5e38\u7528\u65b9\u6cd5\u6709\u54ea\u4e9b\uff1f<\/strong><br \/>\u524d\u7aef\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5c06\u6570\u636e\u4f20\u9012\u7ed9Python\uff0c\u901a\u5e38\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u662f\u4f7f\u7528HTTP\u8bf7\u6c42\u3002\u524d\u7aef\u53ef\u4ee5\u901a\u8fc7AJAX\u6216Fetch API\u5411Python\u540e\u7aef\u53d1\u9001POST\u8bf7\u6c42\uff0c\u4f20\u9012JSON\u683c\u5f0f\u7684\u6570\u636e\u3002\u53e6\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528\u8868\u5355\u63d0\u4ea4\uff0c\u524d\u7aef\u5c06\u6570\u636e\u586b\u5165\u8868\u5355\u5e76\u901a\u8fc7\u8868\u5355\u63d0\u4ea4\u5c06\u6570\u636e\u53d1\u9001\u7ed9\u540e\u7aef\u3002WebSockets\u4e5f\u662f\u4e00\u79cd\u5b9e\u65f6\u4f20\u8f93\u6570\u636e\u7684\u65b9\u5f0f\uff0c\u9002\u7528\u4e8e\u9700\u8981\u9891\u7e41\u66f4\u65b0\u6570\u636e\u7684\u5e94\u7528\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u540e\u7aef\u63a5\u6536\u524d\u7aef\u4f20\u6765\u7684\u6570\u636e\uff1f<\/strong><br \/>\u5728Python\u540e\u7aef\uff0c\u901a\u5e38\u4f7f\u7528Flask\u6216Django\u7b49\u6846\u67b6\u6765\u5904\u7406\u63a5\u6536\u5230\u7684\u8bf7\u6c42\u3002\u5bf9\u4e8eFlask\uff0c\u53ef\u4ee5\u4f7f\u7528<code>request<\/code>\u5bf9\u8c61\u4e2d\u7684<code>json<\/code>\u5c5e\u6027\u6765\u83b7\u53d6JSON\u6570\u636e\uff0c\u6216\u8005\u4f7f\u7528<code>form<\/code>\u5c5e\u6027\u6765\u83b7\u53d6\u8868\u5355\u6570\u636e\u3002Django\u5219\u53ef\u4ee5\u901a\u8fc7<code>request.POST<\/code>\u6216<code>request.body<\/code>\u6765\u83b7\u53d6\u76f8\u5e94\u7684\u6570\u636e\u3002\u786e\u4fdd\u5728\u540e\u7aef\u8bbe\u7f6e\u5408\u9002\u7684\u8def\u7531\u548c\u89c6\u56fe\u51fd\u6570\u6765\u5904\u7406\u8fd9\u4e9b\u8bf7\u6c42\u3002<\/p>\n<p><strong>\u524d\u7aef\u6570\u636e\u4f20\u9012\u7ed9Python\u65f6\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u5b89\u5168\u95ee\u9898\uff1f<\/strong><br \/>\u5728\u524d\u7aef\u4e0ePython\u540e\u7aef\u8fdb\u884c\u6570\u636e\u4f20\u9012\u65f6\uff0c\u5b89\u5168\u6027\u975e\u5e38\u91cd\u8981\u3002\u5e94\u907f\u514d\u76f4\u63a5\u5c06\u7528\u6237\u8f93\u5165\u7684\u6570\u636e\u4f20\u9012\u7ed9\u540e\u7aef\uff0c\u800c\u662f\u9700\u8981\u8fdb\u884c\u8f93\u5165\u9a8c\u8bc1\u548c\u8fc7\u6ee4\uff0c\u4ee5\u9632\u6b62SQL\u6ce8\u5165\u548c\u8de8\u7ad9\u811a\u672c\u653b\u51fb\uff08XSS\uff09\u3002\u4f7f\u7528HTTPS\u534f\u8bae\u52a0\u5bc6\u6570\u636e\u4f20\u8f93\u4e5f\u662f\u4e00\u79cd\u6709\u6548\u7684\u5b89\u5168\u63aa\u65bd\u3002\u6b64\u5916\uff0c\u786e\u4fdd\u540e\u7aef\u5bf9\u63a5\u6536\u5230\u7684\u6570\u636e\u8fdb\u884c\u9002\u5f53\u7684\u9a8c\u8bc1\u548c\u5904\u7406\uff0c\u4ee5\u907f\u514d\u6f5c\u5728\u7684\u5b89\u5168\u98ce\u9669\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u524d\u7aef\u5c06\u6570\u636e\u4f20\u7ed9Python\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u5e38\u89c1\u65b9\u6cd5\uff1a\u4f7f\u7528HTTP\u8bf7\u6c42\u3001WebSocket\u3001\u8868\u5355\u63d0\u4ea4\u3001AJAX\u8bf7 [&hellip;]","protected":false},"author":3,"featured_media":1083338,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1083332"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1083332"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1083332\/revisions"}],"predecessor-version":[{"id":1083342,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1083332\/revisions\/1083342"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1083338"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1083332"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1083332"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1083332"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}