{"id":947239,"date":"2024-12-26T23:49:51","date_gmt":"2024-12-26T15:49:51","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/947239.html"},"modified":"2024-12-26T23:49:54","modified_gmt":"2024-12-26T15:49:54","slug":"python-%e5%a6%82%e4%bd%95%e8%bf%90%e8%a1%8c%e7%bd%91%e7%bb%9c","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/947239.html","title":{"rendered":"python \u5982\u4f55\u8fd0\u884c\u7f51\u7edc"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25083103\/e6139d22-3e75-446a-afb5-b2e0c83be546.webp\" alt=\"python \u5982\u4f55\u8fd0\u884c\u7f51\u7edc\" \/><\/p>\n<p><p> <strong>Python\u8fd0\u884c\u7f51\u7edc\u7684\u4e3b\u8981\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u8bf7\u6c42\u5e93\u8fdb\u884cHTTP\u8bf7\u6c42\u3001\u4f7f\u7528Flask\u6216Django\u6846\u67b6\u6784\u5efaWeb\u5e94\u7528\u3001\u5229\u7528Socket\u7f16\u7a0b\u8fdb\u884c\u4f4e\u7ea7\u7f51\u7edc\u901a\u4fe1\u3002<\/strong>\u5176\u4e2d\uff0c\u4f7f\u7528Flask\u6216Django\u6846\u67b6\u6784\u5efaWeb\u5e94\u7528\u662f\u6700\u5e38\u7528\u548c\u9ad8\u6548\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u8fd9\u4e9b\u6846\u67b6\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u5de5\u5177\u548c\u529f\u80fd\uff0c\u53ef\u4ee5\u5feb\u901f\u642d\u5efa\u548c\u8fd0\u884c\u4e00\u4e2a\u5b8c\u6574\u7684Web\u5e94\u7528\u3002Flask\u662f\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u7684Web\u6846\u67b6\uff0c\u975e\u5e38\u9002\u5408\u6784\u5efa\u5c0f\u578b\u5e94\u7528\u6216\u539f\u578b\uff0c\u800cDjango\u5219\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684\u6846\u67b6\uff0c\u9002\u5408\u6784\u5efa\u590d\u6742\u7684\u5927\u578b\u5e94\u7528\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u53ca\u5176\u5e94\u7528\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528\u8bf7\u6c42\u5e93\u8fdb\u884cHTTP\u8bf7\u6c42<\/p>\n<\/p>\n<p><p>Python\u4e2d\u7684\u8bf7\u6c42\u5e93\uff08Requests\uff09\u662f\u4e00\u4e2a\u7b80\u5355\u6613\u7528\u7684HTTP\u5e93\uff0c\u53ef\u4ee5\u7528\u4e8e\u53d1\u9001HTTP\u8bf7\u6c42\u5e76\u5904\u7406\u54cd\u5e94\u3002\u5b83\u652f\u6301GET\u3001POST\u3001PUT\u3001DELETE\u7b49\u5e38\u89c1\u7684HTTP\u65b9\u6cd5\uff0c\u63d0\u4f9b\u4e86\u4f1a\u8bdd\u5bf9\u8c61\u7528\u4e8e\u7ba1\u7406\u548c\u4fdd\u6301\u8bf7\u6c42\u4f1a\u8bdd\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u5b89\u88c5\u548c\u4f7f\u7528Requests\u5e93<\/strong><\/li>\n<\/ol>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u5b89\u88c5Requests\u5e93\uff0c\u53ef\u4ee5\u901a\u8fc7pip\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install requests<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528Requests\u5e93\u53d1\u9001\u4e00\u4e2aGET\u8bf7\u6c42\u5e76\u83b7\u53d6\u54cd\u5e94\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>response = requests.get(&#39;https:\/\/api.example.com\/data&#39;)<\/p>\n<p>print(response.status_code)  # \u6253\u5370\u72b6\u6001\u7801<\/p>\n<p>print(response.json())       # \u6253\u5370\u54cd\u5e94\u7684JSON\u5185\u5bb9<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u5904\u7406\u54cd\u5e94<\/strong><\/li>\n<\/ol>\n<p><p>Requests\u5e93\u53ef\u4ee5\u8f7b\u677e\u5904\u7406\u54cd\u5e94\u6570\u636e\u3002\u53ef\u4ee5\u901a\u8fc7<code>response.text<\/code>\u83b7\u53d6\u54cd\u5e94\u7684\u6587\u672c\u5185\u5bb9\uff0c\u901a\u8fc7<code>response.json()<\/code>\u83b7\u53d6JSON\u683c\u5f0f\u7684\u6570\u636e\u3002\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u901a\u8fc7<code>response.headers<\/code>\u67e5\u770b\u54cd\u5e94\u5934\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">if response.status_code == 200:<\/p>\n<p>    data = response.json()<\/p>\n<p>    # \u5904\u7406\u6570\u636e<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;\u8bf7\u6c42\u5931\u8d25\uff0c\u72b6\u6001\u7801\uff1a{response.status_code}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u53d1\u9001POST\u8bf7\u6c42<\/strong><\/li>\n<\/ol>\n<p><p>Requests\u5e93\u652f\u6301\u53d1\u9001POST\u8bf7\u6c42\uff0c\u53ef\u4ee5\u5728\u8bf7\u6c42\u4e2d\u9644\u5e26\u6570\u636e\uff0c\u5982\u8868\u5355\u6570\u636e\u6216JSON\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">payload = {&#39;key1&#39;: &#39;value1&#39;, &#39;key2&#39;: &#39;value2&#39;}<\/p>\n<p>response = requests.post(&#39;https:\/\/api.example.com\/post&#39;, data=payload)<\/p>\n<p>print(response.text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528Flask\u6846\u67b6\u6784\u5efaWeb\u5e94\u7528<\/p>\n<\/p>\n<p><p>Flask\u662f\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u7684Web\u6846\u67b6\uff0c\u975e\u5e38\u9002\u5408\u5feb\u901f\u6784\u5efaWeb\u5e94\u7528\u6216API\u670d\u52a1\u3002\u5b83\u6613\u4e8e\u5b66\u4e60\u548c\u4f7f\u7528\uff0c\u63d0\u4f9b\u4e86\u8def\u7531\u3001\u6a21\u677f\u6e32\u67d3\u3001\u8bf7\u6c42\u5904\u7406\u7b49\u57fa\u672c\u529f\u80fd\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u5b89\u88c5\u548c\u521b\u5efaFlask\u5e94\u7528<\/strong><\/li>\n<\/ol>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u5b89\u88c5Flask\uff0c\u53ef\u4ee5\u901a\u8fc7pip\u547d\u4ee4\u8fdb\u884c\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><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684Flask\u5e94\u7528\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from flask import Flask<\/p>\n<p>app = Flask(__name__)<\/p>\n<p>@app.route(&#39;\/&#39;)<\/p>\n<p>def home():<\/p>\n<p>    return &quot;Hello, Flask!&quot;<\/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<ol start=\"2\">\n<li><strong>\u5b9a\u4e49\u8def\u7531\u548c\u89c6\u56fe\u51fd\u6570<\/strong><\/li>\n<\/ol>\n<p><p>\u5728Flask\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u88c5\u9970\u5668<code>@app.route<\/code>\u6765\u5b9a\u4e49URL\u8def\u7531\uff0c\u5e76\u7ed1\u5b9a\u5230\u89c6\u56fe\u51fd\u6570\u3002\u89c6\u56fe\u51fd\u6570\u7528\u4e8e\u5904\u7406\u8bf7\u6c42\u5e76\u8fd4\u56de\u54cd\u5e94\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">@app.route(&#39;\/hello\/&lt;name&gt;&#39;)<\/p>\n<p>def hello(name):<\/p>\n<p>    return f&quot;Hello, {name}!&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u4f7f\u7528\u6a21\u677f\u6e32\u67d3HTML\u9875\u9762<\/strong><\/li>\n<\/ol>\n<p><p>Flask\u652f\u6301\u4f7f\u7528Jinja2\u6a21\u677f\u5f15\u64ce\u6e32\u67d3HTML\u9875\u9762\uff0c\u53ef\u4ee5\u5728\u6a21\u677f\u4e2d\u4f7f\u7528\u53d8\u91cf\u548c\u63a7\u5236\u7ed3\u6784\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from flask import render_template<\/p>\n<p>@app.route(&#39;\/user\/&lt;username&gt;&#39;)<\/p>\n<p>def show_user_profile(username):<\/p>\n<p>    return render_template(&#39;profile.html&#39;, username=username)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728<code>templates\/profile.html<\/code>\u4e2d\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;&lt;title&gt;User Profile&lt;\/title&gt;&lt;\/head&gt;<\/p>\n<p>&lt;body&gt;<\/p>\n<p>  &lt;h1&gt;Hello, {{ username }}!&lt;\/h1&gt;<\/p>\n<p>&lt;\/body&gt;<\/p>\n<p>&lt;\/html&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528Django\u6846\u67b6\u6784\u5efaWeb\u5e94\u7528<\/p>\n<\/p>\n<p><p>Django\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684Web\u6846\u67b6\uff0c\u63d0\u4f9b\u4e86\u5b8c\u6574\u7684\u5e94\u7528\u5f00\u53d1\u5de5\u5177\uff0c\u5305\u62ecORM\u3001\u6a21\u677f\u3001\u8868\u5355\u3001\u8ba4\u8bc1\u7b49\u3002\u9002\u5408\u6784\u5efa\u590d\u6742\u7684\u5927\u578b\u5e94\u7528\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u5b89\u88c5\u548c\u521b\u5efaDjango\u9879\u76ee<\/strong><\/li>\n<\/ol>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u5b89\u88c5Django\uff0c\u53ef\u4ee5\u901a\u8fc7pip\u547d\u4ee4\u8fdb\u884c\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><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528Django\u547d\u4ee4\u884c\u5de5\u5177\u521b\u5efa\u4e00\u4e2a\u65b0\u9879\u76ee\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">django-admin startproject myproject<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fdb\u5165\u9879\u76ee\u76ee\u5f55\u540e\uff0c\u53ef\u4ee5\u542f\u52a8\u5f00\u53d1\u670d\u52a1\u5668\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python manage.py runserver<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u521b\u5efa\u5e94\u7528\u548c\u5b9a\u4e49\u89c6\u56fe<\/strong><\/li>\n<\/ol>\n<p><p>\u5728Django\u4e2d\uff0c\u5e94\u7528\u662f\u4e00\u4e2a\u529f\u80fd\u6a21\u5757\uff0c\u53ef\u4ee5\u5305\u542b\u6a21\u578b\u3001\u89c6\u56fe\u3001\u6a21\u677f\u7b49\u3002\u53ef\u4ee5\u4f7f\u7528\u547d\u4ee4\u521b\u5efa\u4e00\u4e2a\u65b0\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><p>\u5728<code>myapp\/views.py<\/code>\u4e2d\u5b9a\u4e49\u89c6\u56fe\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from django.http import HttpResponse<\/p>\n<p>def home(request):<\/p>\n<p>    return HttpResponse(&quot;Hello, Django!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u914d\u7f6eURL\u8def\u7531<\/strong><\/li>\n<\/ol>\n<p><p>\u5728Django\u4e2d\uff0c\u9700\u8981\u5728<code>urls.py<\/code>\u4e2d\u914d\u7f6eURL\u8def\u7531\uff0c\u5c06URL\u6620\u5c04\u5230\u89c6\u56fe\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><p>\u5728<code>myproject\/urls.py<\/code>\u4e2d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from django.contrib import admin<\/p>\n<p>from django.urls import path<\/p>\n<p>from myapp import views<\/p>\n<p>urlpatterns = [<\/p>\n<p>    path(&#39;admin\/&#39;, admin.site.urls),<\/p>\n<p>    path(&#39;&#39;, views.home, name=&#39;home&#39;),<\/p>\n<p>]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"4\">\n<li><strong>\u4f7f\u7528Django\u7684ORM<\/strong><\/li>\n<\/ol>\n<p><p>Django\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684ORM\uff0c\u53ef\u4ee5\u8f7b\u677e\u5b9a\u4e49\u548c\u64cd\u4f5c\u6570\u636e\u5e93\u6a21\u578b\u3002\u5728<code>myapp\/models.py<\/code>\u4e2d\u5b9a\u4e49\u6a21\u578b\u7c7b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from django.db import models<\/p>\n<p>class Article(models.Model):<\/p>\n<p>    title = models.CharField(max_length=200)<\/p>\n<p>    content = models.TextField()<\/p>\n<p>    pub_date = models.DateTimeField(&#39;date published&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u540c\u6b65\u6570\u636e\u5e93\u540e\uff0c\u53ef\u4ee5\u5728\u89c6\u56fe\u4e2d\u4f7f\u7528\u6a21\u578b\u67e5\u8be2\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from myapp.models import Article<\/p>\n<p>def articles(request):<\/p>\n<p>    latest_articles = Article.objects.order_by(&#39;-pub_date&#39;)[:5]<\/p>\n<p>    output = &#39;, &#39;.join([a.title for a in latest_articles])<\/p>\n<p>    return HttpResponse(output)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u5229\u7528Socket\u7f16\u7a0b\u8fdb\u884c\u4f4e\u7ea7\u7f51\u7edc\u901a\u4fe1<\/p>\n<\/p>\n<p><p>Python\u5185\u7f6e\u7684socket\u5e93\u652f\u6301TCP\u548cUDP\u534f\u8bae\u7684\u7f51\u7edc\u901a\u4fe1\uff0c\u53ef\u4ee5\u7528\u4e8e\u6784\u5efa\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\u5e94\u7528\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u521b\u5efaTCP\u5ba2\u6237\u7aef<\/strong><\/li>\n<\/ol>\n<p><p>\u53ef\u4ee5\u4f7f\u7528socket\u5e93\u521b\u5efa\u4e00\u4e2aTCP\u5ba2\u6237\u7aef\uff0c\u4e0e\u670d\u52a1\u5668\u8fdb\u884c\u901a\u4fe1\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import socket<\/p>\n<p>def tcp_client():<\/p>\n<p>    host = &#39;localhost&#39;<\/p>\n<p>    port = 12345<\/p>\n<p>    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:<\/p>\n<p>        s.connect((host, port))<\/p>\n<p>        s.sendall(b&#39;Hello, server&#39;)<\/p>\n<p>        data = s.recv(1024)<\/p>\n<p>    print(&#39;Received&#39;, repr(data))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u521b\u5efaTCP\u670d\u52a1\u5668<\/strong><\/li>\n<\/ol>\n<p><p>\u53ef\u4ee5\u4f7f\u7528socket\u5e93\u521b\u5efa\u4e00\u4e2aTCP\u670d\u52a1\u5668\uff0c\u63a5\u6536\u5ba2\u6237\u7aef\u8fde\u63a5\u5e76\u5904\u7406\u8bf7\u6c42\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import socket<\/p>\n<p>def tcp_server():<\/p>\n<p>    host = &#39;localhost&#39;<\/p>\n<p>    port = 12345<\/p>\n<p>    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:<\/p>\n<p>        s.bind((host, port))<\/p>\n<p>        s.listen()<\/p>\n<p>        print(&#39;Server listening on&#39;, (host, port))<\/p>\n<p>        conn, addr = s.accept()<\/p>\n<p>        with conn:<\/p>\n<p>            print(&#39;Connected by&#39;, addr)<\/p>\n<p>            while True:<\/p>\n<p>                data = conn.recv(1024)<\/p>\n<p>                if not data:<\/p>\n<p>                    break<\/p>\n<p>                conn.sendall(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u4f7f\u7528UDP\u534f\u8bae<\/strong><\/li>\n<\/ol>\n<p><p>Socket\u5e93\u4e5f\u652f\u6301UDP\u534f\u8bae\uff0c\u53ef\u4ee5\u7528\u4e8e\u65e0\u8fde\u63a5\u7684\u7f51\u7edc\u901a\u4fe1\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def udp_client():<\/p>\n<p>    host = &#39;localhost&#39;<\/p>\n<p>    port = 12345<\/p>\n<p>    with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:<\/p>\n<p>        s.sendto(b&#39;Hello, UDP server&#39;, (host, port))<\/p>\n<p>        data, server = s.recvfrom(4096)<\/p>\n<p>    print(&#39;Received&#39;, repr(data))<\/p>\n<p>def udp_server():<\/p>\n<p>    host = &#39;localhost&#39;<\/p>\n<p>    port = 12345<\/p>\n<p>    with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:<\/p>\n<p>        s.bind((host, port))<\/p>\n<p>        print(&#39;UDP Server listening on&#39;, (host, port))<\/p>\n<p>        while True:<\/p>\n<p>            data, addr = s.recvfrom(4096)<\/p>\n<p>            print(&#39;Received from&#39;, addr, data)<\/p>\n<p>            s.sendto(data, addr)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0cPython\u53ef\u4ee5\u6709\u6548\u5730\u8fd0\u884c\u548c\u7ba1\u7406\u7f51\u7edc\u5e94\u7528\uff0c\u65e0\u8bba\u662f\u901a\u8fc7HTTP\u534f\u8bae\u4e0e\u7f51\u7edc\u8d44\u6e90\u4ea4\u4e92\uff0c\u8fd8\u662f\u6784\u5efa\u590d\u6742\u7684Web\u5e94\u7528\uff0c\u6216\u8005\u8fdb\u884c\u4f4e\u7ea7\u7684\u7f51\u7edc\u901a\u4fe1\uff0cPython\u90fd\u63d0\u4f9b\u4e86\u5f3a\u5927\u4e14\u7075\u6d3b\u7684\u5de5\u5177\u3002\u9009\u62e9\u9002\u5408\u7684\u5de5\u5177\u548c\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5927\u5927\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u548c\u5e94\u7528\u6027\u80fd\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u8fdb\u884c\u7f51\u7edc\u7f16\u7a0b\uff1f<\/strong><br \/>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u5e93\u548c\u6a21\u5757\uff0c\u5982<code>socket<\/code>\u548c<code>requests<\/code>\uff0c\u53ef\u4ee5\u7528\u4e8e\u7f51\u7edc\u7f16\u7a0b\u3002\u901a\u8fc7<code>socket<\/code>\u6a21\u5757\uff0c\u53ef\u4ee5\u521b\u5efaTCP\u6216UDP\u670d\u52a1\u5668\u548c\u5ba2\u6237\u7aef\uff0c\u800c<code>requests<\/code>\u5e93\u5219\u7b80\u5316\u4e86HTTP\u8bf7\u6c42\u7684\u8fc7\u7a0b\u3002\u5bf9\u4e8e\u65b0\u624b\u6765\u8bf4\uff0c\u5b66\u4e60\u4f7f\u7528\u8fd9\u4e9b\u5e93\u53ef\u4ee5\u5e2e\u52a9\u5feb\u901f\u5165\u95e8\u7f51\u7edc\u7f16\u7a0b\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5904\u7406\u7f51\u7edc\u9519\u8bef\uff1f<\/strong><br \/>\u5728\u8fdb\u884c\u7f51\u7edc\u7f16\u7a0b\u65f6\uff0c\u5904\u7406\u9519\u8bef\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u4f7f\u7528<code>try-except<\/code>\u8bed\u53e5\u53ef\u4ee5\u6355\u6349\u7f51\u7edc\u8bf7\u6c42\u4e2d\u7684\u5f02\u5e38\uff0c\u4f8b\u5982\u8fde\u63a5\u8d85\u65f6\u3001\u65e0\u6cd5\u89e3\u6790\u4e3b\u673a\u7b49\u3002\u901a\u8fc7\u6355\u6349\u8fd9\u4e9b\u5f02\u5e38\uff0c\u53ef\u4ee5\u4f7f\u7a0b\u5e8f\u66f4\u5065\u58ee\uff0c\u5e76\u63d0\u4f9b\u7528\u6237\u53cb\u597d\u7684\u9519\u8bef\u4fe1\u606f\u3002<\/p>\n<p><strong>Python\u7f51\u7edc\u7f16\u7a0b\u7684\u6700\u4f73\u5b9e\u8df5\u6709\u54ea\u4e9b\uff1f<\/strong><br \/>\u4e3a\u4e86\u63d0\u9ad8\u7f51\u7edc\u7f16\u7a0b\u7684\u6548\u7387\u548c\u5b89\u5168\u6027\uff0c\u5efa\u8bae\u4f7f\u7528HTTPS\u534f\u8bae\u8fdb\u884c\u6570\u636e\u4f20\u8f93\uff0c\u786e\u4fdd\u6570\u636e\u52a0\u5bc6\u3002\u6b64\u5916\uff0c\u4f7f\u7528\u8fde\u63a5\u6c60\u6765\u7ba1\u7406\u7f51\u7edc\u8fde\u63a5\u53ef\u4ee5\u63d0\u9ad8\u6027\u80fd\u3002\u786e\u4fdd\u5bf9\u7528\u6237\u8f93\u5165\u8fdb\u884c\u9a8c\u8bc1\uff0c\u5e76\u4f7f\u7528\u9002\u5f53\u7684\u5f02\u5e38\u5904\u7406\u673a\u5236\u6765\u907f\u514d\u6f5c\u5728\u7684\u5b89\u5168\u6f0f\u6d1e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u8fd0\u884c\u7f51\u7edc\u7684\u4e3b\u8981\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u8bf7\u6c42\u5e93\u8fdb\u884cHTTP\u8bf7\u6c42\u3001\u4f7f\u7528Flask\u6216Django\u6846\u67b6\u6784\u5efaWeb\u5e94\u7528 [&hellip;]","protected":false},"author":3,"featured_media":947250,"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\/947239"}],"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=947239"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/947239\/revisions"}],"predecessor-version":[{"id":947252,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/947239\/revisions\/947252"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/947250"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=947239"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=947239"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=947239"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}