{"id":1176636,"date":"2025-01-15T17:47:21","date_gmt":"2025-01-15T09:47:21","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1176636.html"},"modified":"2025-01-15T17:47:24","modified_gmt":"2025-01-15T09:47:24","slug":"python%e5%a6%82%e4%bd%95%e5%ae%9e%e7%8e%b0%e5%89%8d%e5%90%8e%e7%ab%af%e4%ba%a4%e4%ba%92","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1176636.html","title":{"rendered":"python\u5982\u4f55\u5b9e\u73b0\u524d\u540e\u7aef\u4ea4\u4e92"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25111708\/d68cb8a3-7078-4b1f-a8a6-553626a2abe8.webp\" alt=\"python\u5982\u4f55\u5b9e\u73b0\u524d\u540e\u7aef\u4ea4\u4e92\" \/><\/p>\n<p><p> <strong>Python\u5b9e\u73b0\u524d\u540e\u7aef\u4ea4\u4e92\u7684\u65b9\u5f0f\u4e3b\u8981\u6709\u4ee5\u4e0b\u51e0\u79cd\uff1a\u4f7f\u7528Flask\u6216Django\u6846\u67b6\u3001\u901a\u8fc7RESTful API\u3001\u4f7f\u7528WebSocket\u3001\u7ed3\u5408\u524d\u7aef\u6846\u67b6\u5982React\u6216Vue\u8fdb\u884c\u5b8c\u6574\u5f00\u53d1<\/strong>\u3002\u5176\u4e2d\uff0c\u901a\u8fc7RESTful API\u8fdb\u884c\u524d\u540e\u7aef\u4ea4\u4e92\u662f\u4e00\u79cd\u5e38\u89c1\u4e14\u9ad8\u6548\u7684\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528Flask\u6846\u67b6\u5b9e\u73b0\u524d\u540e\u7aef\u4ea4\u4e92<\/h3>\n<\/p>\n<p><p>Flask\u662f\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u7684Python Web\u6846\u67b6\uff0c\u975e\u5e38\u9002\u5408\u5c0f\u578b\u5e94\u7528\u548c\u5feb\u901f\u5f00\u53d1\u3002\u5b83\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u521b\u5efaRESTful API\uff0c\u5e76\u4e0e\u524d\u7aef\u8fdb\u884c\u4ea4\u4e92\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5Flask<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86Flask\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528pip\u6765\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install Flask<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684Flask\u5e94\u7528<\/h4>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684Flask\u5e94\u7528\u793a\u4f8b\uff1a<\/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;\/api\/data&#39;, methods=[&#39;GET&#39;])<\/p>\n<p>def get_data():<\/p>\n<p>    sample_data = {<\/p>\n<p>        &quot;name&quot;: &quot;John&quot;,<\/p>\n<p>        &quot;age&quot;: 30,<\/p>\n<p>        &quot;city&quot;: &quot;New York&quot;<\/p>\n<p>    }<\/p>\n<p>    return jsonify(sample_data)<\/p>\n<p>@app.route(&#39;\/api\/data&#39;, methods=[&#39;POST&#39;])<\/p>\n<p>def post_data():<\/p>\n<p>    data = request.json<\/p>\n<p>    response = {<\/p>\n<p>        &quot;status&quot;: &quot;success&quot;,<\/p>\n<p>        &quot;data_received&quot;: data<\/p>\n<p>    }<\/p>\n<p>    return jsonify(response)<\/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><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e24\u4e2aAPI\u7aef\u70b9\uff0c\u4e00\u4e2a\u7528\u4e8eGET\u8bf7\u6c42\uff0c\u53e6\u4e00\u4e2a\u7528\u4e8ePOST\u8bf7\u6c42\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u524d\u7aef\u8c03\u7528Flask API<\/h4>\n<\/p>\n<p><p>\u524d\u7aef\u53ef\u4ee5\u4f7f\u7528\u4efb\u4f55\u73b0\u4ee3\u7684JavaScript\u6846\u67b6\uff08\u5982React\u3001Vue\u3001Angular\uff09\u6216\u7eafJavaScript\u6765\u8c03\u7528\u8fd9\u4e9bAPI\u7aef\u70b9\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528\u7eafJavaScript\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-html\">&lt;!DOCTYPE html&gt;<\/p>\n<p>&lt;html&gt;<\/p>\n<p>&lt;head&gt;<\/p>\n<p>    &lt;title&gt;Flask API Example&lt;\/title&gt;<\/p>\n<p>&lt;\/head&gt;<\/p>\n<p>&lt;body&gt;<\/p>\n<p>    &lt;h1&gt;Flask API Example&lt;\/h1&gt;<\/p>\n<p>    &lt;button onclick=&quot;fetchData()&quot;&gt;Get Data&lt;\/button&gt;<\/p>\n<p>    &lt;button onclick=&quot;sendData()&quot;&gt;Send Data&lt;\/button&gt;<\/p>\n<p>    &lt;script&gt;<\/p>\n<p>        function fetchData() {<\/p>\n<p>            fetch(&#39;http:\/\/127.0.0.1:5000\/api\/data&#39;)<\/p>\n<p>                .then(response =&gt; response.json())<\/p>\n<p>                .then(data =&gt; console.log(data))<\/p>\n<p>                .catch(error =&gt; console.error(&#39;Error:&#39;, error));<\/p>\n<p>        }<\/p>\n<p>        function sendData() {<\/p>\n<p>            const data = {<\/p>\n<p>                name: &#39;Jane&#39;,<\/p>\n<p>                age: 25,<\/p>\n<p>                city: &#39;San Francisco&#39;<\/p>\n<p>            };<\/p>\n<p>            fetch(&#39;http:\/\/127.0.0.1:5000\/api\/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; console.log(data))<\/p>\n<p>                .catch(error =&gt; console.error(&#39;Error:&#39;, error));<\/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><h3>\u4e8c\u3001\u4f7f\u7528Django\u6846\u67b6\u5b9e\u73b0\u524d\u540e\u7aef\u4ea4\u4e92<\/h3>\n<\/p>\n<p><p>Django\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684Web\u6846\u67b6\uff0c\u9002\u5408\u5927\u578b\u5e94\u7528\u5f00\u53d1\u3002\u5b83\u81ea\u5e26\u4e86\u8bb8\u591a\u5b9e\u7528\u5de5\u5177\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u524d\u540e\u7aef\u4ea4\u4e92\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5Django<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86Django\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528pip\u6765\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install Django<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u521b\u5efa\u4e00\u4e2aDjango\u9879\u76ee\u548c\u5e94\u7528<\/h4>\n<\/p>\n<p><p>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684Django\u9879\u76ee\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">django-admin startproject myproject<\/p>\n<p>cd myproject<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684Django\u5e94\u7528\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python manage.py startapp myapp<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u914d\u7f6eDjango\u9879\u76ee<\/h4>\n<\/p>\n<p><p>\u5728<code>myproject\/settings.py<\/code>\u4e2d\uff0c\u6dfb\u52a0\u4f60\u7684\u5e94\u7528\u5230<code>INSTALLED_APPS<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">INSTALLED_APPS = [<\/p>\n<p>    ...<\/p>\n<p>    &#39;myapp&#39;,<\/p>\n<p>]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4\u3001\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684Django\u89c6\u56fe<\/h4>\n<\/p>\n<p><p>\u5728<code>myapp\/views.py<\/code>\u4e2d\uff0c\u521b\u5efa\u4e24\u4e2a\u89c6\u56fe\uff0c\u4e00\u4e2a\u7528\u4e8eGET\u8bf7\u6c42\uff0c\u53e6\u4e00\u4e2a\u7528\u4e8ePOST\u8bf7\u6c42\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from django.http import JsonResponse<\/p>\n<p>from django.views.decorators.csrf import csrf_exempt<\/p>\n<p>import json<\/p>\n<p>def get_data(request):<\/p>\n<p>    sample_data = {<\/p>\n<p>        &quot;name&quot;: &quot;John&quot;,<\/p>\n<p>        &quot;age&quot;: 30,<\/p>\n<p>        &quot;city&quot;: &quot;New York&quot;<\/p>\n<p>    }<\/p>\n<p>    return JsonResponse(sample_data)<\/p>\n<p>@csrf_exempt<\/p>\n<p>def post_data(request):<\/p>\n<p>    if request.method == &#39;POST&#39;:<\/p>\n<p>        data = json.loads(request.body)<\/p>\n<p>        response = {<\/p>\n<p>            &quot;status&quot;: &quot;success&quot;,<\/p>\n<p>            &quot;data_received&quot;: data<\/p>\n<p>        }<\/p>\n<p>        return JsonResponse(response)<\/p>\n<p>    else:<\/p>\n<p>        return JsonResponse({&quot;status&quot;: &quot;error&quot;}, status=400)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>5\u3001\u914d\u7f6eDjango URLs<\/h4>\n<\/p>\n<p><p>\u5728<code>myapp\/urls.py<\/code>\u4e2d\uff0c\u914d\u7f6eURL\u8def\u7531\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from django.urls import path<\/p>\n<p>from .views import get_data, post_data<\/p>\n<p>urlpatterns = [<\/p>\n<p>    path(&#39;api\/data&#39;, get_data, name=&#39;get_data&#39;),<\/p>\n<p>    path(&#39;api\/data&#39;, post_data, name=&#39;post_data&#39;),<\/p>\n<p>]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728<code>myproject\/urls.py<\/code>\u4e2d\uff0c\u5305\u542b\u5e94\u7528\u7684URL\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from django.contrib import admin<\/p>\n<p>from django.urls import path, include<\/p>\n<p>urlpatterns = [<\/p>\n<p>    path(&#39;admin\/&#39;, admin.site.urls),<\/p>\n<p>    path(&#39;&#39;, include(&#39;myapp.urls&#39;)),<\/p>\n<p>]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>6\u3001\u524d\u7aef\u8c03\u7528Django API<\/h4>\n<\/p>\n<p><p>\u524d\u7aef\u8c03\u7528Django API\u7684\u65b9\u5f0f\u4e0e\u8c03\u7528Flask API\u7c7b\u4f3c\uff0c\u53ef\u4ee5\u4f7f\u7528\u7eafJavaScript\u6216\u5176\u4ed6\u524d\u7aef\u6846\u67b6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528\u7eafJavaScript\u7684\u793a\u4f8b\uff08\u4e0e\u4e0a\u9762\u7684Flask\u793a\u4f8b\u76f8\u540c\uff09\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-html\">&lt;!DOCTYPE html&gt;<\/p>\n<p>&lt;html&gt;<\/p>\n<p>&lt;head&gt;<\/p>\n<p>    &lt;title&gt;Django API Example&lt;\/title&gt;<\/p>\n<p>&lt;\/head&gt;<\/p>\n<p>&lt;body&gt;<\/p>\n<p>    &lt;h1&gt;Django API Example&lt;\/h1&gt;<\/p>\n<p>    &lt;button onclick=&quot;fetchData()&quot;&gt;Get Data&lt;\/button&gt;<\/p>\n<p>    &lt;button onclick=&quot;sendData()&quot;&gt;Send Data&lt;\/button&gt;<\/p>\n<p>    &lt;script&gt;<\/p>\n<p>        function fetchData() {<\/p>\n<p>            fetch(&#39;http:\/\/127.0.0.1:8000\/api\/data&#39;)<\/p>\n<p>                .then(response =&gt; response.json())<\/p>\n<p>                .then(data =&gt; console.log(data))<\/p>\n<p>                .catch(error =&gt; console.error(&#39;Error:&#39;, error));<\/p>\n<p>        }<\/p>\n<p>        function sendData() {<\/p>\n<p>            const data = {<\/p>\n<p>                name: &#39;Jane&#39;,<\/p>\n<p>                age: 25,<\/p>\n<p>                city: &#39;San Francisco&#39;<\/p>\n<p>            };<\/p>\n<p>            fetch(&#39;http:\/\/127.0.0.1:8000\/api\/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; console.log(data))<\/p>\n<p>                .catch(error =&gt; console.error(&#39;Error:&#39;, error));<\/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><h3>\u4e09\u3001\u901a\u8fc7RESTful API\u5b9e\u73b0\u524d\u540e\u7aef\u4ea4\u4e92<\/h3>\n<\/p>\n<p><p>RESTful API\u662f\u4e00\u79cd\u5e38\u89c1\u7684Web\u670d\u52a1\u67b6\u6784\uff0c\u9002\u5408\u7528\u4e8e\u524d\u540e\u7aef\u5206\u79bb\u7684\u9879\u76ee\u3002\u5b83\u4f7f\u7528HTTP\u65b9\u6cd5\uff08\u5982GET\u3001POST\u3001PUT\u3001DELETE\uff09\u8fdb\u884c\u8d44\u6e90\u7684\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b9a\u4e49RESTful API<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u5b9a\u4e49\u4f60\u7684RESTful API\u7aef\u70b9\u548c\u8d44\u6e90\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u4f60\u6709\u4e00\u4e2a\u7528\u6237\u8d44\u6e90\uff0c\u4f60\u53ef\u4ee5\u5b9a\u4e49\u4ee5\u4e0b\u7aef\u70b9\uff1a<\/p>\n<\/p>\n<ul>\n<li>GET \/users &#8211; \u83b7\u53d6\u6240\u6709\u7528\u6237<\/li>\n<li>GET \/users\/{id} &#8211; \u83b7\u53d6\u7279\u5b9a\u7528\u6237<\/li>\n<li>POST \/users &#8211; \u521b\u5efa\u4e00\u4e2a\u65b0\u7528\u6237<\/li>\n<li>PUT \/users\/{id} &#8211; \u66f4\u65b0\u7279\u5b9a\u7528\u6237<\/li>\n<li>DELETE \/users\/{id} &#8211; \u5220\u9664\u7279\u5b9a\u7528\u6237<\/li>\n<\/ul>\n<p><h4>2\u3001\u521b\u5efaAPI\u7aef\u70b9<\/h4>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528Flask\u6216Django\u6765\u521b\u5efa\u8fd9\u4e9bAPI\u7aef\u70b9\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528Flask\u7684\u793a\u4f8b\uff1a<\/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>users = [<\/p>\n<p>    {&quot;id&quot;: 1, &quot;name&quot;: &quot;John&quot;, &quot;age&quot;: 30},<\/p>\n<p>    {&quot;id&quot;: 2, &quot;name&quot;: &quot;Jane&quot;, &quot;age&quot;: 25}<\/p>\n<p>]<\/p>\n<p>@app.route(&#39;\/users&#39;, methods=[&#39;GET&#39;])<\/p>\n<p>def get_users():<\/p>\n<p>    return jsonify(users)<\/p>\n<p>@app.route(&#39;\/users\/&lt;int:user_id&gt;&#39;, methods=[&#39;GET&#39;])<\/p>\n<p>def get_user(user_id):<\/p>\n<p>    user = next((user for user in users if user[&quot;id&quot;] == user_id), None)<\/p>\n<p>    if user:<\/p>\n<p>        return jsonify(user)<\/p>\n<p>    else:<\/p>\n<p>        return jsonify({&quot;message&quot;: &quot;User not found&quot;}), 404<\/p>\n<p>@app.route(&#39;\/users&#39;, methods=[&#39;POST&#39;])<\/p>\n<p>def create_user():<\/p>\n<p>    new_user = request.json<\/p>\n<p>    new_user[&quot;id&quot;] = len(users) + 1<\/p>\n<p>    users.append(new_user)<\/p>\n<p>    return jsonify(new_user), 201<\/p>\n<p>@app.route(&#39;\/users\/&lt;int:user_id&gt;&#39;, methods=[&#39;PUT&#39;])<\/p>\n<p>def update_user(user_id):<\/p>\n<p>    user = next((user for user in users if user[&quot;id&quot;] == user_id), None)<\/p>\n<p>    if user:<\/p>\n<p>        updated_data = request.json<\/p>\n<p>        user.update(updated_data)<\/p>\n<p>        return jsonify(user)<\/p>\n<p>    else:<\/p>\n<p>        return jsonify({&quot;message&quot;: &quot;User not found&quot;}), 404<\/p>\n<p>@app.route(&#39;\/users\/&lt;int:user_id&gt;&#39;, methods=[&#39;DELETE&#39;])<\/p>\n<p>def delete_user(user_id):<\/p>\n<p>    global users<\/p>\n<p>    users = [user for user in users if user[&quot;id&quot;] != user_id]<\/p>\n<p>    return jsonify({&quot;message&quot;: &quot;User deleted&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>3\u3001\u524d\u7aef\u8c03\u7528RESTful API<\/h4>\n<\/p>\n<p><p>\u524d\u7aef\u53ef\u4ee5\u4f7f\u7528\u7c7b\u4f3c\u524d\u9762\u7684\u65b9\u5f0f\u8c03\u7528\u8fd9\u4e9bAPI\u7aef\u70b9\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528\u7eafJavaScript\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-html\">&lt;!DOCTYPE html&gt;<\/p>\n<p>&lt;html&gt;<\/p>\n<p>&lt;head&gt;<\/p>\n<p>    &lt;title&gt;RESTful API Example&lt;\/title&gt;<\/p>\n<p>&lt;\/head&gt;<\/p>\n<p>&lt;body&gt;<\/p>\n<p>    &lt;h1&gt;RESTful API Example&lt;\/h1&gt;<\/p>\n<p>    &lt;button onclick=&quot;fetchUsers()&quot;&gt;Get Users&lt;\/button&gt;<\/p>\n<p>    &lt;button onclick=&quot;createUser()&quot;&gt;Create User&lt;\/button&gt;<\/p>\n<p>    &lt;script&gt;<\/p>\n<p>        function fetchUsers() {<\/p>\n<p>            fetch(&#39;http:\/\/127.0.0.1:5000\/users&#39;)<\/p>\n<p>                .then(response =&gt; response.json())<\/p>\n<p>                .then(data =&gt; console.log(data))<\/p>\n<p>                .catch(error =&gt; console.error(&#39;Error:&#39;, error));<\/p>\n<p>        }<\/p>\n<p>        function createUser() {<\/p>\n<p>            const data = {<\/p>\n<p>                name: &#39;Alice&#39;,<\/p>\n<p>                age: 28<\/p>\n<p>            };<\/p>\n<p>            fetch(&#39;http:\/\/127.0.0.1:5000\/users&#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; console.log(data))<\/p>\n<p>                .catch(error =&gt; console.error(&#39;Error:&#39;, error));<\/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><h3>\u56db\u3001\u4f7f\u7528WebSocket\u5b9e\u73b0\u524d\u540e\u7aef\u4ea4\u4e92<\/h3>\n<\/p>\n<p><p>WebSocket\u662f\u4e00\u79cd\u5728\u5355\u4e2aTCP\u8fde\u63a5\u4e0a\u8fdb\u884c\u5168\u53cc\u5de5\u901a\u4fe1\u7684\u534f\u8bae\uff0c\u9002\u7528\u4e8e\u9700\u8981\u5b9e\u65f6\u6570\u636e\u66f4\u65b0\u7684\u5e94\u7528\uff0c\u5982\u804a\u5929\u5e94\u7528\u3001\u5728\u7ebf\u6e38\u620f\u7b49\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5WebSocket\u5e93<\/h4>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528<code>websockets<\/code>\u5e93\u6765\u5b9e\u73b0WebSocket\u670d\u52a1\u5668\u3002\u4f7f\u7528pip\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install websockets<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u521b\u5efaWebSocket\u670d\u52a1\u5668<\/h4>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684WebSocket\u670d\u52a1\u5668\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import asyncio<\/p>\n<p>import websockets<\/p>\n<p>async def echo(websocket, path):<\/p>\n<p>    async for message in websocket:<\/p>\n<p>        await websocket.send(f&quot;Echo: {message}&quot;)<\/p>\n<p>start_server = websockets.serve(echo, &quot;localhost&quot;, 8765)<\/p>\n<p>asyncio.get_event_loop().run_until_complete(start_server)<\/p>\n<p>asyncio.get_event_loop().run_forever()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u524d\u7aef\u8fde\u63a5WebSocket\u670d\u52a1\u5668<\/h4>\n<\/p>\n<p><p>\u524d\u7aef\u53ef\u4ee5\u4f7f\u7528JavaScript\u7684WebSocket API\u6765\u8fde\u63a5\u548c\u901a\u4fe1\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-html\">&lt;!DOCTYPE html&gt;<\/p>\n<p>&lt;html&gt;<\/p>\n<p>&lt;head&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;h1&gt;WebSocket Example&lt;\/h1&gt;<\/p>\n<p>    &lt;button onclick=&quot;connectWebSocket()&quot;&gt;Connect WebSocket&lt;\/button&gt;<\/p>\n<p>    &lt;button onclick=&quot;sendMessage()&quot;&gt;Send Message&lt;\/button&gt;<\/p>\n<p>    &lt;script&gt;<\/p>\n<p>        let socket;<\/p>\n<p>        function connectWebSocket() {<\/p>\n<p>            socket = new WebSocket(&#39;ws:\/\/localhost:8765&#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: &#39; + event.data);<\/p>\n<p>            };<\/p>\n<p>            socket.onclose = function(event) {<\/p>\n<p>                console.log(&#39;WebSocket is closed now.&#39;);<\/p>\n<p>            };<\/p>\n<p>            socket.onerror = function(error) {<\/p>\n<p>                console.log(&#39;WebSocket Error: &#39; + error);<\/p>\n<p>            };<\/p>\n<p>        }<\/p>\n<p>        function sendMessage() {<\/p>\n<p>            if (socket &amp;&amp; socket.readyState === WebSocket.OPEN) {<\/p>\n<p>                socket.send(&#39;Hello, WebSocket!&#39;);<\/p>\n<p>            } else {<\/p>\n<p>                console.log(&#39;WebSocket is not open.&#39;);<\/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><h3>\u4e94\u3001\u7ed3\u5408\u524d\u7aef\u6846\u67b6\u5982React\u6216Vue\u8fdb\u884c\u5b8c\u6574\u5f00\u53d1<\/h3>\n<\/p>\n<p><p>\u5728\u73b0\u4ee3Web\u5f00\u53d1\u4e2d\uff0c\u524d\u540e\u7aef\u5206\u79bb\u7684\u505a\u6cd5\u8d8a\u6765\u8d8a\u666e\u904d\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u524d\u7aef\u6846\u67b6\u5982React\u6216Vue\u6765\u521b\u5efa\u7528\u6237\u754c\u9762\uff0c\u5e76\u901a\u8fc7API\u4e0e\u540e\u7aef\u8fdb\u884c\u4ea4\u4e92\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528React\u6846\u67b6<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86Node.js\u548cnpm\u3002\u7136\u540e\uff0c\u4f7f\u7528Create React App\u521b\u5efa\u4e00\u4e2a\u65b0\u7684React\u9879\u76ee\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">npx create-react-app my-react-app<\/p>\n<p>cd my-react-app<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u521b\u5efaReact\u7ec4\u4ef6<\/h4>\n<\/p>\n<p><p>\u5728\u4f60\u7684React\u9879\u76ee\u4e2d\uff0c\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u7ec4\u4ef6\u6765\u8c03\u7528API\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">import React, { useState, useEffect } from &#39;react&#39;;<\/p>\n<p>function App() {<\/p>\n<p>    const [data, setData] = useState(null);<\/p>\n<p>    useEffect(() =&gt; {<\/p>\n<p>        fetch(&#39;http:\/\/127.0.0.1:5000\/api\/data&#39;)<\/p>\n<p>            .then(response =&gt; response.json())<\/p>\n<p>            .then(data =&gt; setData(data))<\/p>\n<p>            .catch(error =&gt; console.error(&#39;Error:&#39;, error));<\/p>\n<p>    }, []);<\/p>\n<p>    const sendData = () =&gt; {<\/p>\n<p>        const dataToSend = {<\/p>\n<p>            name: &#39;Alice&#39;,<\/p>\n<p>            age: 28,<\/p>\n<p>            city: &#39;San Francisco&#39;<\/p>\n<p>        };<\/p>\n<p>        fetch(&#39;http:\/\/127.0.0.1:5000\/api\/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(dataToSend)<\/p>\n<p>        })<\/p>\n<p>            .then(response =&gt; response.json())<\/p>\n<p>            .then(data =&gt; console.log(data))<\/p>\n<p>            .catch(error =&gt; console.error(&#39;Error:&#39;, error));<\/p>\n<p>    };<\/p>\n<p>    return (<\/p>\n<p>        &lt;div&gt;<\/p>\n<p>            &lt;h1&gt;React API Example&lt;\/h1&gt;<\/p>\n<p>            &lt;button onClick={sendData}&gt;Send Data&lt;\/button&gt;<\/p>\n<p>            {data &amp;&amp; (<\/p>\n<p>                &lt;div&gt;<\/p>\n<p>                    &lt;h2&gt;Data from API:&lt;\/h2&gt;<\/p>\n<p>                    &lt;pre&gt;{JSON.stringify(data, null, 2)}&lt;\/pre&gt;<\/p>\n<p>                &lt;\/div&gt;<\/p>\n<p>            )}<\/p>\n<p>        &lt;\/div&gt;<\/p>\n<p>    );<\/p>\n<p>}<\/p>\n<p>export default App;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u8fd0\u884cReact\u5e94\u7528<\/h4>\n<\/p>\n<p><p>\u5728\u9879\u76ee\u76ee\u5f55\u4e2d\uff0c\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u542f\u52a8React\u5f00\u53d1\u670d\u52a1\u5668\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">npm start<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528Vue\u6846\u67b6<\/h3>\n<\/p>\n<p><p>Vue\u662f\u53e6\u4e00\u4e2a\u6d41\u884c\u7684\u524d\u7aef\u6846\u67b6\uff0c\u9002\u5408\u7528\u4e8e\u521b\u5efa\u52a8\u6001\u548c\u54cd\u5e94\u5f0f\u7684\u7528\u6237\u754c\u9762\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u521b\u5efaVue\u9879\u76ee<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86Vue CLI\u3002\u7136\u540e\uff0c\u4f7f\u7528Vue CLI\u521b\u5efa\u4e00\u4e2a\u65b0\u7684Vue\u9879\u76ee\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">npm install -g @vue\/cli<\/p>\n<p>vue create my-vue-app<\/p>\n<p>cd my-vue-app<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u521b\u5efaVue\u7ec4\u4ef6<\/h4>\n<\/p>\n<p><p>\u5728\u4f60\u7684Vue\u9879\u76ee\u4e2d\uff0c\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u7ec4\u4ef6\u6765\u8c03\u7528API\u3002\u7f16\u8f91<code>src\/components\/HelloWorld.vue<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-vue\">&lt;template&gt;<\/p>\n<p>  &lt;div&gt;<\/p>\n<p>    &lt;h1&gt;Vue API Example&lt;\/h1&gt;<\/p>\n<p>    &lt;button @click=&quot;sendData&quot;&gt;Send Data&lt;\/button&gt;<\/p>\n<p>    &lt;div v-if=&quot;data&quot;&gt;<\/p>\n<p>      &lt;h2&gt;Data from API:&lt;\/h2&gt;<\/p>\n<p>      &lt;pre&gt;{{ data }}&lt;\/pre&gt;<\/p>\n<p>    &lt;\/div&gt;<\/p>\n<p>  &lt;\/div&gt;<\/p>\n<p>&lt;\/template&gt;<\/p>\n<p>&lt;script&gt;<\/p>\n<p>export default {<\/p>\n<p>  data() {<\/p>\n<p>    return {<\/p>\n<p>      data: null,<\/p>\n<p>    };<\/p>\n<p>  },<\/p>\n<p>  mounted() {<\/p>\n<p>    fetch(&#39;http:\/\/127.0.0.1:5000\/api\/data&#39;)<\/p>\n<p>      .then(response =&gt; response.json())<\/p>\n<p>      .then(data =&gt; {<\/p>\n<p>        this.data = data;<\/p>\n<p>      })<\/p>\n<p>      .catch(error =&gt; console.error(&#39;Error:&#39;, error));<\/p>\n<p>  },<\/p>\n<p>  methods: {<\/p>\n<p>    sendData() {<\/p>\n<p>      const dataToSend = {<\/p>\n<p>        name: &#39;Alice&#39;,<\/p>\n<p>        age: 28,<\/p>\n<p>        city: &#39;San Francisco&#39;,<\/p>\n<p>      };<\/p>\n<p>      fetch(&#39;http:\/\/127.0.0.1:5000\/api\/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(dataToSend),<\/p>\n<p>      })<\/p>\n<p>        .then(response =&gt; response.json())<\/p>\n<p>        .then(data =&gt; console.log(data))<\/p>\n<p>        .catch(error =&gt; console.error(&#39;Error:&#39;, error));<\/p>\n<p>    },<\/p>\n<p>  },<\/p>\n<p>};<\/p>\n<p>&lt;\/script&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u8fd0\u884cVue\u5e94\u7528<\/h4>\n<\/p>\n<p><p>\u5728\u9879\u76ee\u76ee\u5f55\u4e2d\uff0c\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u542f\u52a8Vue\u5f00\u53d1\u670d\u52a1\u5668\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">npm run serve<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528Python\u548c\u73b0\u4ee3\u524d\u7aef\u6846\u67b6\uff08\u5982React\u6216Vue\uff09\u521b\u5efa\u4e00\u4e2a\u5b8c\u6574\u7684\u524d\u540e\u7aef\u4ea4\u4e92\u5e94\u7528\u3002\u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u65b9\u5f0f\uff0c\u5173\u952e\u5728\u4e8e\u7406\u89e3\u548c\u638c\u63e1API\u7684\u521b\u5efa\u548c\u8c03\u7528\uff0c\u4ee5\u53ca\u524d\u7aef\u4e0e\u540e\u7aef\u5982\u4f55\u901a\u8fc7API\u8fdb\u884c\u6570\u636e\u4ea4\u6362\u3002\u8fd9\u4e9b\u6280\u5de7\u548c\u65b9\u6cd5\u5c06\u6781\u5927\u5730\u63d0\u5347\u4f60\u7684Web\u5f00\u53d1\u80fd\u529b\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u5b9e\u73b0\u4e0e\u524d\u7aef\u7684\u6709\u6548\u6570\u636e\u4ea4\u4e92\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u524d\u540e\u7aef\u4ea4\u4e92\u901a\u5e38\u901a\u8fc7API\u5b9e\u73b0\u3002\u53ef\u4ee5\u4f7f\u7528Flask\u6216Django\u7b49\u6846\u67b6\u642d\u5efaRESTful API\uff0c\u524d\u7aef\u901a\u8fc7HTTP\u8bf7\u6c42\uff08\u5982GET\u3001POST\uff09\u4e0e\u540e\u7aef\u8fdb\u884c\u6570\u636e\u4ea4\u6362\u3002\u4f7f\u7528JSON\u683c\u5f0f\u4f20\u8f93\u6570\u636e\u662f\u6700\u5e38\u89c1\u7684\u65b9\u5f0f\uff0c\u786e\u4fdd\u524d\u7aef\u80fd\u591f\u8f7b\u677e\u89e3\u6790\u548c\u4f7f\u7528\u8fd9\u4e9b\u6570\u636e\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u5904\u7406\u524d\u7aef\u53d1\u9001\u7684\u8bf7\u6c42\u6570\u636e\uff1f<\/strong><br \/>\u5f53\u524d\u7aef\u53d1\u9001\u8bf7\u6c42\u5230\u540e\u7aef\u65f6\uff0c\u540e\u7aef\u53ef\u4ee5\u901a\u8fc7Flask\u7684<code>request<\/code>\u5bf9\u8c61\u6216Django\u7684<code>request<\/code>\u6a21\u5757\u83b7\u53d6\u6570\u636e\u3002\u5bf9\u4e8ePOST\u8bf7\u6c42\uff0c\u5e38\u7528<code>request.form<\/code>\u6216<code>request.json<\/code>\u6765\u63d0\u53d6\u8868\u5355\u6570\u636e\u6216JSON\u6570\u636e\u3002\u540c\u65f6\uff0c\u786e\u4fdd\u5728\u540e\u7aef\u8fdb\u884c\u6570\u636e\u9a8c\u8bc1\u548c\u5b89\u5168\u6027\u68c0\u67e5\uff0c\u9632\u6b62\u6076\u610f\u8f93\u5165\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u9879\u76ee\u4e2d\u5b9e\u73b0WebSocket\u4ee5\u5b9e\u73b0\u5b9e\u65f6\u4ea4\u4e92\uff1f<\/strong><br \/>\u4f7f\u7528WebSocket\u53ef\u4ee5\u5b9e\u73b0\u524d\u540e\u7aef\u7684\u5b9e\u65f6\u53cc\u5411\u901a\u4fe1\uff0c\u9002\u5408\u9700\u8981\u5373\u65f6\u66f4\u65b0\u7684\u5e94\u7528\u573a\u666f\u3002Python\u7684<code>channels<\/code>\uff08Django\uff09\u6216<code>Flask-SocketIO<\/code>\u5e93\u53ef\u4ee5\u5e2e\u52a9\u4f60\u8f7b\u677e\u5b9e\u73b0WebSocket\u529f\u80fd\u3002\u901a\u8fc7\u8bbe\u7f6e\u76f8\u5e94\u7684\u8def\u7531\u548c\u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f\uff0c\u524d\u7aef\u53ef\u4ee5\u5728\u4e0d\u5237\u65b0\u9875\u9762\u7684\u60c5\u51b5\u4e0b\u63a5\u6536\u6765\u81ea\u540e\u7aef\u7684\u5b9e\u65f6\u6570\u636e\u66f4\u65b0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5b9e\u73b0\u524d\u540e\u7aef\u4ea4\u4e92\u7684\u65b9\u5f0f\u4e3b\u8981\u6709\u4ee5\u4e0b\u51e0\u79cd\uff1a\u4f7f\u7528Flask\u6216Django\u6846\u67b6\u3001\u901a\u8fc7RESTful API\u3001 [&hellip;]","protected":false},"author":3,"featured_media":1176642,"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\/1176636"}],"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=1176636"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1176636\/revisions"}],"predecessor-version":[{"id":1176643,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1176636\/revisions\/1176643"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1176642"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1176636"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1176636"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1176636"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}