{"id":1054861,"date":"2024-12-31T14:45:55","date_gmt":"2024-12-31T06:45:55","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1054861.html"},"modified":"2024-12-31T14:45:57","modified_gmt":"2024-12-31T06:45:57","slug":"python%e5%a6%82%e4%bd%95%e7%9c%8b%e8%af%b7%e6%b1%82%e6%98%af%e5%90%a6%e5%93%8d%e5%ba%94code","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1054861.html","title":{"rendered":"python\u5982\u4f55\u770b\u8bf7\u6c42\u662f\u5426\u54cd\u5e94code"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/ff572da6-03c0-492b-a368-5eb61f8a7bb5.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u770b\u8bf7\u6c42\u662f\u5426\u54cd\u5e94code\" \/><\/p>\n<p><p> <strong>\u8981\u67e5\u770bPython\u4e2d\u7684\u8bf7\u6c42\u662f\u5426\u6709\u54cd\u5e94\u4ee3\u7801\uff0c\u53ef\u4ee5\u4f7f\u7528requests\u5e93\u3001urllib\u5e93\u3001http.client\u6a21\u5757\u3002<\/strong><\/p>\n<\/p>\n<p><p><strong>requests\u5e93\u975e\u5e38\u7b80\u4fbf\u3001\u9002\u5408\u521d\u5b66\u8005\u3001\u529f\u80fd\u4e30\u5bcc\u3001\u6613\u4e8e\u4f7f\u7528\u3002<\/strong> \u4e0b\u9762\u5c06\u8be6\u7ec6\u89e3\u91ca\u5982\u4f55\u4f7f\u7528requests\u5e93\u8fdb\u884cHTTP\u8bf7\u6c42\uff0c\u5e76\u67e5\u770b\u54cd\u5e94\u4ee3\u7801\u3002requests\u5e93\u662f\u4e00\u4e2a\u7528\u4e8e\u53d1\u9001HTTP\u8bf7\u6c42\u7684Python\u5e93\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528\u5b83\u6765\u53d1\u9001\u5404\u79cd\u7c7b\u578b\u7684HTTP\u8bf7\u6c42\uff0c\u4f8b\u5982GET\u3001POST\u3001PUT\u3001DELETE\u7b49\uff0c\u5e76\u68c0\u67e5\u54cd\u5e94\u72b6\u6001\u7801\uff0c\u4ee5\u786e\u5b9a\u8bf7\u6c42\u662f\u5426\u6210\u529f\u3002<\/p>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u8be6\u7ec6\u6b65\u9aa4\u548c\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5requests\u5e93<\/h3>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u4e4b\u524d\uff0c\u60a8\u9700\u8981\u786e\u4fdd\u5df2\u5b89\u88c5requests\u5e93\u3002\u5982\u679c\u8fd8\u6ca1\u6709\u5b89\u88c5\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\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><h3>\u4e8c\u3001\u53d1\u9001HTTP\u8bf7\u6c42\u5e76\u67e5\u770b\u54cd\u5e94\u4ee3\u7801<\/h3>\n<\/p>\n<p><h4>1\u3001GET\u8bf7\u6c42<\/h4>\n<\/p>\n<p><p>GET\u8bf7\u6c42\u901a\u5e38\u7528\u4e8e\u4ece\u670d\u52a1\u5668\u83b7\u53d6\u6570\u636e\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528requests\u5e93\u53d1\u9001GET\u8bf7\u6c42\u5e76\u67e5\u770b\u54cd\u5e94\u4ee3\u7801\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>response = requests.get(&#39;https:\/\/jsonplaceholder.typicode.com\/posts\/1&#39;)<\/p>\n<h2><strong>\u67e5\u770b\u54cd\u5e94\u4ee3\u7801<\/strong><\/h2>\n<p>print(response.status_code)<\/p>\n<h2><strong>\u68c0\u67e5\u54cd\u5e94\u662f\u5426\u6210\u529f<\/strong><\/h2>\n<p>if response.status_code == 200:<\/p>\n<p>    print(&quot;\u8bf7\u6c42\u6210\u529f&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;\u8bf7\u6c42\u5931\u8d25&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5411\u4e00\u4e2a\u793a\u4f8bAPI\u53d1\u9001\u4e86GET\u8bf7\u6c42\uff0c\u5e76\u901a\u8fc7<code>response.status_code<\/code>\u67e5\u770b\u54cd\u5e94\u4ee3\u7801\u3002\u6839\u636e\u54cd\u5e94\u4ee3\u7801\u7684\u503c\uff0c\u6211\u4eec\u53ef\u4ee5\u5224\u65ad\u8bf7\u6c42\u662f\u5426\u6210\u529f\u3002<\/p>\n<\/p>\n<p><h4>2\u3001POST\u8bf7\u6c42<\/h4>\n<\/p>\n<p><p>POST\u8bf7\u6c42\u901a\u5e38\u7528\u4e8e\u5411\u670d\u52a1\u5668\u53d1\u9001\u6570\u636e\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528requests\u5e93\u53d1\u9001POST\u8bf7\u6c42\u5e76\u67e5\u770b\u54cd\u5e94\u4ee3\u7801\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>url = &#39;https:\/\/jsonplaceholder.typicode.com\/posts&#39;<\/p>\n<p>data = {<\/p>\n<p>    &#39;title&#39;: &#39;foo&#39;,<\/p>\n<p>    &#39;body&#39;: &#39;bar&#39;,<\/p>\n<p>    &#39;userId&#39;: 1<\/p>\n<p>}<\/p>\n<p>response = requests.post(url, json=data)<\/p>\n<h2><strong>\u67e5\u770b\u54cd\u5e94\u4ee3\u7801<\/strong><\/h2>\n<p>print(response.status_code)<\/p>\n<h2><strong>\u68c0\u67e5\u54cd\u5e94\u662f\u5426\u6210\u529f<\/strong><\/h2>\n<p>if response.status_code == 201:<\/p>\n<p>    print(&quot;\u521b\u5efa\u6210\u529f&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;\u521b\u5efa\u5931\u8d25&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5411\u4e00\u4e2a\u793a\u4f8bAPI\u53d1\u9001\u4e86POST\u8bf7\u6c42\uff0c\u5e76\u901a\u8fc7<code>response.status_code<\/code>\u67e5\u770b\u54cd\u5e94\u4ee3\u7801\u3002\u6839\u636e\u54cd\u5e94\u4ee3\u7801\u7684\u503c\uff0c\u6211\u4eec\u53ef\u4ee5\u5224\u65ad\u8bf7\u6c42\u662f\u5426\u6210\u529f\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001HTTP\u54cd\u5e94\u4ee3\u7801\u7684\u542b\u4e49<\/h3>\n<\/p>\n<p><p>HTTP\u54cd\u5e94\u4ee3\u7801\u662f\u4e00\u79cd\u7528\u4e8e\u6307\u793aHTTP\u8bf7\u6c42\u7ed3\u679c\u7684\u4e09\u4f4d\u6570\u5b57\u3002\u5e38\u89c1\u7684\u54cd\u5e94\u4ee3\u7801\u53ca\u5176\u542b\u4e49\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>1xx\uff08\u4fe1\u606f\u6027\u54cd\u5e94\uff09\uff1a<\/strong> \u8bf7\u6c42\u5df2\u88ab\u63a5\u6536\uff0c\u7ee7\u7eed\u5904\u7406\u3002<\/li>\n<li><strong>2xx\uff08\u6210\u529f\uff09\uff1a<\/strong> \u8bf7\u6c42\u5df2\u6210\u529f\u63a5\u6536\u3001\u7406\u89e3\u5e76\u63a5\u53d7\u3002\n<ul>\n<li><strong>200 OK\uff1a<\/strong> \u8bf7\u6c42\u6210\u529f\u3002<\/li>\n<li><strong>201 Created\uff1a<\/strong> \u6210\u529f\u8bf7\u6c42\u5e76\u521b\u5efa\u4e86\u65b0\u7684\u8d44\u6e90\u3002<\/li>\n<li><strong>204 No Content\uff1a<\/strong> \u670d\u52a1\u5668\u6210\u529f\u5904\u7406\u4e86\u8bf7\u6c42\uff0c\u4f46\u6ca1\u6709\u8fd4\u56de\u4efb\u4f55\u5185\u5bb9\u3002<\/li>\n<\/ul>\n<\/li>\n<li><strong>3xx\uff08\u91cd\u5b9a\u5411\uff09\uff1a<\/strong> \u9700\u8981\u8fdb\u4e00\u6b65\u7684\u64cd\u4f5c\u4ee5\u5b8c\u6210\u8bf7\u6c42\u3002\n<ul>\n<li><strong>301 Moved Permanently\uff1a<\/strong> \u8bf7\u6c42\u7684\u8d44\u6e90\u5df2\u88ab\u6c38\u4e45\u79fb\u52a8\u5230\u65b0\u4f4d\u7f6e\u3002<\/li>\n<li><strong>302 Found\uff1a<\/strong> \u8bf7\u6c42\u7684\u8d44\u6e90\u4e34\u65f6\u4ece\u4e0d\u540c\u7684URI\u54cd\u5e94\u8bf7\u6c42\u3002<\/li>\n<\/ul>\n<\/li>\n<li><strong>4xx\uff08\u5ba2\u6237\u7aef\u9519\u8bef\uff09\uff1a<\/strong> \u8bf7\u6c42\u5305\u542b\u9519\u8bef\u6216\u65e0\u6cd5\u5b8c\u6210\u8bf7\u6c42\u3002\n<ul>\n<li><strong>400 Bad Request\uff1a<\/strong> \u670d\u52a1\u5668\u65e0\u6cd5\u7406\u89e3\u8bf7\u6c42\u3002<\/li>\n<li><strong>401 Unauthorized\uff1a<\/strong> \u8bf7\u6c42\u9700\u8981\u8eab\u4efd\u9a8c\u8bc1\u3002<\/li>\n<li><strong>403 Forbidden\uff1a<\/strong> \u670d\u52a1\u5668\u62d2\u7edd\u8bf7\u6c42\u3002<\/li>\n<li><strong>404 Not Found\uff1a<\/strong> \u8bf7\u6c42\u7684\u8d44\u6e90\u65e0\u6cd5\u627e\u5230\u3002<\/li>\n<\/ul>\n<\/li>\n<li><strong>5xx\uff08\u670d\u52a1\u5668\u9519\u8bef\uff09\uff1a<\/strong> \u670d\u52a1\u5668\u5728\u5904\u7406\u8bf7\u6c42\u65f6\u53d1\u751f\u9519\u8bef\u3002\n<ul>\n<li><strong>500 Internal Server Error\uff1a<\/strong> \u670d\u52a1\u5668\u9047\u5230\u9519\u8bef\uff0c\u65e0\u6cd5\u5b8c\u6210\u8bf7\u6c42\u3002<\/li>\n<li><strong>502 Bad Gateway\uff1a<\/strong> \u670d\u52a1\u5668\u4f5c\u4e3a\u7f51\u5173\u6216\u4ee3\u7406\uff0c\u4ece\u4e0a\u6e38\u670d\u52a1\u5668\u6536\u5230\u65e0\u6548\u54cd\u5e94\u3002<\/li>\n<li><strong>503 Service Unav<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>lable\uff1a<\/strong> \u670d\u52a1\u5668\u5f53\u524d\u65e0\u6cd5\u5904\u7406\u8bf7\u6c42\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p><p>\u901a\u8fc7\u67e5\u770b\u54cd\u5e94\u4ee3\u7801\uff0c\u60a8\u53ef\u4ee5\u4e86\u89e3\u8bf7\u6c42\u7684\u7ed3\u679c\uff0c\u5e76\u6839\u636e\u4e0d\u540c\u7684\u54cd\u5e94\u4ee3\u7801\u91c7\u53d6\u76f8\u5e94\u7684\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u5f02\u5e38\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u7f51\u7edc\u8bf7\u6c42\u53ef\u80fd\u4f1a\u5931\u8d25\uff0c\u4f8b\u5982\u7f51\u7edc\u8fde\u63a5\u95ee\u9898\u3001\u670d\u52a1\u5668\u9519\u8bef\u7b49\u3002\u4e3a\u4e86\u5904\u7406\u8fd9\u4e9b\u5f02\u5e38\u60c5\u51b5\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u5f02\u5e38\u5904\u7406\u673a\u5236\uff08try-except\uff09\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>url = &#39;https:\/\/jsonplaceholder.typicode.com\/posts\/1&#39;<\/p>\n<p>try:<\/p>\n<p>    response = requests.get(url)<\/p>\n<p>    response.raise_for_status()  # \u5982\u679c\u54cd\u5e94\u4ee3\u7801\u4e0d\u662f200\uff0c\u629b\u51faHTTPError<\/p>\n<p>    print(&quot;\u8bf7\u6c42\u6210\u529f\uff0c\u54cd\u5e94\u4ee3\u7801:&quot;, response.status_code)<\/p>\n<p>except requests.exceptions.HTTPError as errh:<\/p>\n<p>    print(&quot;Http Error:&quot;, errh)<\/p>\n<p>except requests.exceptions.ConnectionError as errc:<\/p>\n<p>    print(&quot;Error Connecting:&quot;, errc)<\/p>\n<p>except requests.exceptions.Timeout as errt:<\/p>\n<p>    print(&quot;Timeout Error:&quot;, errt)<\/p>\n<p>except requests.exceptions.RequestException as err:<\/p>\n<p>    print(&quot;OOps: Something Else&quot;, err)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86<code>try-except<\/code>\u6765\u5904\u7406\u5404\u79cd\u53ef\u80fd\u7684\u5f02\u5e38\u60c5\u51b5\u3002\u901a\u8fc7<code>response.raise_for_status()<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5728\u54cd\u5e94\u4ee3\u7801\u4e0d\u662f200\u7684\u60c5\u51b5\u4e0b\u629b\u51faHTTPError\u5f02\u5e38\uff0c\u5e76\u6839\u636e\u4e0d\u540c\u7684\u5f02\u5e38\u7c7b\u578b\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528urllib\u5e93<\/h3>\n<\/p>\n<p><p>\u9664\u4e86requests\u5e93\uff0cPython\u7684\u6807\u51c6\u5e93urllib\u4e5f\u53ef\u4ee5\u7528\u4e8e\u53d1\u9001HTTP\u8bf7\u6c42\u548c\u67e5\u770b\u54cd\u5e94\u4ee3\u7801\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528urllib\u5e93\u53d1\u9001GET\u8bf7\u6c42\u5e76\u67e5\u770b\u54cd\u5e94\u4ee3\u7801\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import urllib.request<\/p>\n<p>url = &#39;https:\/\/jsonplaceholder.typicode.com\/posts\/1&#39;<\/p>\n<p>try:<\/p>\n<p>    with urllib.request.urlopen(url) as response:<\/p>\n<p>        status_code = response.getcode()<\/p>\n<p>        print(&quot;\u54cd\u5e94\u4ee3\u7801:&quot;, status_code)<\/p>\n<p>        if status_code == 200:<\/p>\n<p>            print(&quot;\u8bf7\u6c42\u6210\u529f&quot;)<\/p>\n<p>        else:<\/p>\n<p>            print(&quot;\u8bf7\u6c42\u5931\u8d25&quot;)<\/p>\n<p>except urllib.error.HTTPError as e:<\/p>\n<p>    print(&quot;HTTPError:&quot;, e.code)<\/p>\n<p>except urllib.error.URLError as e:<\/p>\n<p>    print(&quot;URLError:&quot;, e.reason)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86<code>urllib.request.urlopen<\/code>\u65b9\u6cd5\u53d1\u9001GET\u8bf7\u6c42\uff0c\u5e76\u901a\u8fc7<code>response.getcode()<\/code>\u65b9\u6cd5\u67e5\u770b\u54cd\u5e94\u4ee3\u7801\u3002\u6211\u4eec\u8fd8\u4f7f\u7528\u4e86\u5f02\u5e38\u5904\u7406\u673a\u5236\u6765\u5904\u7406\u53ef\u80fd\u7684\u5f02\u5e38\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528http.client\u6a21\u5757<\/h3>\n<\/p>\n<p><p>Python\u7684http.client\u6a21\u5757\u4e5f\u53ef\u4ee5\u7528\u4e8e\u53d1\u9001HTTP\u8bf7\u6c42\u548c\u67e5\u770b\u54cd\u5e94\u4ee3\u7801\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528http.client\u6a21\u5757\u53d1\u9001GET\u8bf7\u6c42\u5e76\u67e5\u770b\u54cd\u5e94\u4ee3\u7801\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import http.client<\/p>\n<p>conn = http.client.HTTPSConnection(&quot;jsonplaceholder.typicode.com&quot;)<\/p>\n<p>conn.request(&quot;GET&quot;, &quot;\/posts\/1&quot;)<\/p>\n<p>response = conn.getresponse()<\/p>\n<p>print(&quot;\u54cd\u5e94\u4ee3\u7801:&quot;, response.status)<\/p>\n<p>if response.status == 200:<\/p>\n<p>    print(&quot;\u8bf7\u6c42\u6210\u529f&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;\u8bf7\u6c42\u5931\u8d25&quot;)<\/p>\n<p>conn.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86http.client\u6a21\u5757\u53d1\u9001GET\u8bf7\u6c42\uff0c\u5e76\u901a\u8fc7<code>response.status<\/code>\u67e5\u770b\u54cd\u5e94\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u672c\u6587\uff0c\u60a8\u4e86\u89e3\u4e86\u5982\u4f55\u4f7f\u7528requests\u5e93\u3001urllib\u5e93\u548chttp.client\u6a21\u5757\u5728Python\u4e2d\u53d1\u9001HTTP\u8bf7\u6c42\u5e76\u67e5\u770b\u54cd\u5e94\u4ee3\u7801\u3002<strong>requests\u5e93\u975e\u5e38\u7b80\u4fbf\u3001\u9002\u5408\u521d\u5b66\u8005\u3001\u529f\u80fd\u4e30\u5bcc\u3001\u6613\u4e8e\u4f7f\u7528<\/strong>\u3002\u6b64\u5916\uff0c\u6211\u4eec\u8fd8\u4ecb\u7ecd\u4e86\u5982\u4f55\u5904\u7406\u5f02\u5e38\u60c5\u51b5\uff0c\u4ee5\u786e\u4fdd\u7a0b\u5e8f\u7684\u7a33\u5065\u6027\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u60a8\u6709\u6240\u5e2e\u52a9\uff0c\u80fd\u591f\u66f4\u597d\u5730\u5904\u7406HTTP\u8bf7\u6c42\u548c\u54cd\u5e94\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u68c0\u67e5HTTP\u8bf7\u6c42\u7684\u54cd\u5e94\u72b6\u6001\u7801\uff1f<\/strong><\/p>\n<p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>requests<\/code>\u5e93\u8f7b\u677e\u68c0\u67e5HTTP\u8bf7\u6c42\u7684\u54cd\u5e94\u72b6\u6001\u7801\u3002\u53ef\u4ee5\u901a\u8fc7\u53d1\u9001\u8bf7\u6c42\u5e76\u8bbf\u95ee\u54cd\u5e94\u5bf9\u8c61\u7684<code>status_code<\/code>\u5c5e\u6027\u6765\u5b9e\u73b0\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>requests.get()<\/code>\u65b9\u6cd5\u53d1\u9001GET\u8bf7\u6c42\uff0c\u5e76\u6253\u5370\u54cd\u5e94\u7684\u72b6\u6001\u7801\u3002<\/p>\n<pre><code class=\"language-python\">import requests\n\nresponse = requests.get(&#39;http:\/\/example.com&#39;)\nprint(response.status_code)\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u663e\u793a\u8bf7\u6c42\u8fd4\u56de\u7684HTTP\u72b6\u6001\u7801\uff0c\u5982200\u8868\u793a\u8bf7\u6c42\u6210\u529f\uff0c404\u8868\u793a\u672a\u627e\u5230\u9875\u9762\u7b49\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5904\u7406\u4e0d\u540c\u7684\u54cd\u5e94\u72b6\u6001\u7801\uff1f<\/strong><\/p>\n<p>\u4e0d\u540c\u7684\u72b6\u6001\u7801\u4ee3\u8868\u4e0d\u540c\u7684\u8bf7\u6c42\u7ed3\u679c\uff0c\u5904\u7406\u8fd9\u4e9b\u72b6\u6001\u7801\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u597d\u5730\u7ba1\u7406\u5e94\u7528\u903b\u8f91\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528\u6761\u4ef6\u8bed\u53e5\u6765\u5904\u7406\u4e0d\u540c\u7684\u72b6\u6001\u7801\uff1a<\/p>\n<pre><code class=\"language-python\">if response.status_code == 200:\n    print(&quot;\u8bf7\u6c42\u6210\u529f\uff01&quot;)\nelif response.status_code == 404:\n    print(&quot;\u8bf7\u6c42\u7684\u9875\u9762\u672a\u627e\u5230\uff01&quot;)\nelif response.status_code == 500:\n    print(&quot;\u670d\u52a1\u5668\u5185\u90e8\u9519\u8bef\uff01&quot;)\nelse:\n    print(f&quot;\u53d1\u751f\u4e86\u5176\u4ed6\u9519\u8bef\uff0c\u72b6\u6001\u7801\uff1a{response.status_code}&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u6839\u636e\u4e0d\u540c\u7684\u72b6\u6001\u7801\u505a\u51fa\u76f8\u5e94\u7684\u5904\u7406\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u8bbe\u7f6e\u8bf7\u6c42\u8d85\u65f6\u5e76\u68c0\u67e5\u54cd\u5e94\uff1f<\/strong><\/p>\n<p>\u5728\u8fdb\u884cHTTP\u8bf7\u6c42\u65f6\uff0c\u8bbe\u7f6e\u8d85\u65f6\u662f\u4e00\u4e2a\u826f\u597d\u7684\u5b9e\u8df5\u3002\u53ef\u4ee5\u5728\u8bf7\u6c42\u4e2d\u6307\u5b9a<code>timeout<\/code>\u53c2\u6570\uff0c\u4ee5\u9632\u6b62\u8bf7\u6c42\u957f\u65f6\u95f4\u6302\u8d77\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">try:\n    response = requests.get(&#39;http:\/\/example.com&#39;, timeout=5)\n    print(f&quot;\u72b6\u6001\u7801\uff1a{response.status_code}&quot;)\nexcept requests.exceptions.Timeout:\n    print(&quot;\u8bf7\u6c42\u8d85\u65f6\uff01&quot;)\nexcept requests.exceptions.RequestException as e:\n    print(f&quot;\u53d1\u751f\u4e86\u8bf7\u6c42\u9519\u8bef\uff1a{e}&quot;)\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u60a8\u53ef\u4ee5\u5728\u8bf7\u6c42\u8d85\u65f6\u6216\u53d1\u751f\u5176\u4ed6\u9519\u8bef\u65f6\u8fdb\u884c\u5904\u7406\uff0c\u4ece\u800c\u63d0\u9ad8\u5e94\u7528\u7684\u7a33\u5b9a\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u67e5\u770bPython\u4e2d\u7684\u8bf7\u6c42\u662f\u5426\u6709\u54cd\u5e94\u4ee3\u7801\uff0c\u53ef\u4ee5\u4f7f\u7528requests\u5e93\u3001urllib\u5e93\u3001http.client\u6a21 [&hellip;]","protected":false},"author":3,"featured_media":1054867,"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\/1054861"}],"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=1054861"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1054861\/revisions"}],"predecessor-version":[{"id":1054873,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1054861\/revisions\/1054873"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1054867"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1054861"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1054861"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1054861"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}