{"id":994375,"date":"2024-12-27T08:54:51","date_gmt":"2024-12-27T00:54:51","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/994375.html"},"modified":"2024-12-27T08:54:54","modified_gmt":"2024-12-27T00:54:54","slug":"python%e5%a6%82%e4%bd%95%e6%9f%a5%e7%9c%8b%e5%93%8d%e5%ba%94%e5%a4%b4","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/994375.html","title":{"rendered":"python\u5982\u4f55\u67e5\u770b\u54cd\u5e94\u5934"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25071403\/cae65523-c396-4359-b1da-13667aad9f82.webp\" alt=\"python\u5982\u4f55\u67e5\u770b\u54cd\u5e94\u5934\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u67e5\u770b\u54cd\u5e94\u5934\u7684\u5e38\u7528\u65b9\u6cd5\u662f\u4f7f\u7528<code>requests<\/code>\u5e93\u3001\u901a\u8fc7<code>Response.headers<\/code>\u5c5e\u6027\u83b7\u53d6\u3001\u8c03\u8bd5\u4e0e\u5206\u6790HTTP\u8bf7\u6c42\u3002<\/strong> <code>requests<\/code>\u5e93\u662f\u4e00\u4e2a\u975e\u5e38\u6d41\u884c\u7684HTTP\u8bf7\u6c42\u5e93\uff0c\u7b80\u5316\u4e86HTTP\u8bf7\u6c42\u7684\u5904\u7406\uff0c\u53ef\u4ee5\u8f7b\u677e\u83b7\u53d6\u54cd\u5e94\u5934\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u4f7f\u7528<code>requests<\/code>\u5e93\u53d1\u9001HTTP\u8bf7\u6c42\u540e\uff0c\u54cd\u5e94\u5bf9\u8c61\u7684<code>headers<\/code>\u5c5e\u6027\u4f1a\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u6240\u6709HTTP\u54cd\u5e94\u5934\u7684\u5b57\u5178\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u6df1\u5165\u63a2\u8ba8\u5982\u4f55\u5728Python\u4e2d\u67e5\u770b\u54cd\u5e94\u5934\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528<code>requests<\/code>\u5e93\u67e5\u770b\u54cd\u5e94\u5934<\/p>\n<\/p>\n<p><p><code>requests<\/code>\u5e93\u662fPython\u4e2d\u5904\u7406HTTP\u8bf7\u6c42\u7684\u5f3a\u5927\u5de5\u5177\u3002\u901a\u8fc7\u5b83\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u83b7\u53d6\u54cd\u5e94\u5934\u4fe1\u606f\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5\u4e0e\u5bfc\u5165<code>requests<\/code>\u5e93<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u4f7f\u7528<code>requests<\/code>\u5e93\u4e4b\u524d\uff0c\u9700\u8981\u786e\u4fdd\u5b83\u5df2\u7ecf\u5b89\u88c5\u3002\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><p>\u7136\u540e\uff0c\u5728Python\u811a\u672c\u4e2d\u5bfc\u5165\u8fd9\u4e2a\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u53d1\u9001HTTP\u8bf7\u6c42\u5e76\u83b7\u53d6\u54cd\u5e94\u5934<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>requests<\/code>\u5e93\u53d1\u9001\u4e00\u4e2aHTTP GET\u8bf7\u6c42\uff0c\u5e76\u901a\u8fc7<code>Response.headers<\/code>\u5c5e\u6027\u83b7\u53d6\u54cd\u5e94\u5934\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">response = requests.get(&#39;https:\/\/www.example.com&#39;)<\/p>\n<p>headers = response.headers<\/p>\n<p>print(headers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc\uff0c<code>response.headers<\/code>\u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u5305\u542bHTTP\u54cd\u5e94\u5934\u7684\u5b57\u5178\uff0c\u53ef\u4ee5\u8f7b\u677e\u83b7\u53d6\u7279\u5b9a\u7684\u5934\u4fe1\u606f\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001\u901a\u8fc7<code>Response.headers<\/code>\u5c5e\u6027\u83b7\u53d6\u5177\u4f53\u5934\u4fe1\u606f<\/p>\n<\/p>\n<p><p>\u9664\u4e86\u67e5\u770b\u6240\u6709\u7684\u54cd\u5e94\u5934\uff0c\u6709\u65f6\u6211\u4eec\u53ef\u80fd\u53ea\u9700\u8981\u83b7\u53d6\u67d0\u4e2a\u7279\u5b9a\u7684\u5934\u4fe1\u606f\uff0c\u6bd4\u5982<code>Content-Type<\/code>\u6216<code>Server<\/code>\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u83b7\u53d6\u7279\u5b9a\u7684\u5934\u4fe1\u606f<\/strong><\/p>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u5b57\u5178\u7684\u952e\u6765\u83b7\u53d6\u7279\u5b9a\u7684\u54cd\u5e94\u5934\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">content_type = response.headers.get(&#39;Content-Type&#39;)<\/p>\n<p>server = response.headers.get(&#39;Server&#39;)<\/p>\n<p>print(f&quot;Content-Type: {content_type}&quot;)<\/p>\n<p>print(f&quot;Server: {server}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u65b9\u4fbf\uff0c\u56e0\u4e3a\u5982\u679c\u67d0\u4e2a\u5934\u4fe1\u606f\u4e0d\u5b58\u5728\uff0c<code>get<\/code>\u65b9\u6cd5\u4f1a\u8fd4\u56de<code>None<\/code>\uff0c\u800c\u4e0d\u662f\u629b\u51fa\u5f02\u5e38\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5904\u7406\u5934\u4fe1\u606f\u7684\u5927\u5c0f\u5199\u95ee\u9898<\/strong><\/p>\n<\/p>\n<p><p>HTTP\u5934\u4fe1\u606f\u7684\u952e\u662f\u5927\u5c0f\u5199\u4e0d\u654f\u611f\u7684\u3002\u56e0\u6b64\uff0c\u5728\u4f7f\u7528<code>headers.get()<\/code>\u65b9\u6cd5\u65f6\uff0c\u4e0d\u5fc5\u62c5\u5fc3\u952e\u7684\u5927\u5c0f\u5199\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u8c03\u8bd5\u4e0e\u5206\u6790HTTP\u8bf7\u6c42<\/p>\n<\/p>\n<p><p>\u5728\u5f00\u53d1\u548c\u8c03\u8bd5\u65f6\uff0c\u67e5\u770bHTTP\u54cd\u5e94\u5934\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u4e86\u89e3\u670d\u52a1\u5668\u7684\u884c\u4e3a\u548c\u72b6\u6001\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u8c03\u8bd5HTTP\u8bf7\u6c42<\/strong><\/p>\n<\/p>\n<p><p>\u5f53\u53d1\u9001\u8bf7\u6c42\u540e\u53d1\u73b0\u54cd\u5e94\u4e0d\u7b26\u5408\u9884\u671f\u65f6\uff0c\u67e5\u770b\u54cd\u5e94\u5934\u53ef\u4ee5\u63d0\u4f9b\u6709\u4ef7\u503c\u7684\u8c03\u8bd5\u4fe1\u606f\u3002\u4f8b\u5982\uff0c<code>Content-Type<\/code>\u5934\u53ef\u4ee5\u544a\u8bc9\u6211\u4eec\u8fd4\u56de\u7684\u6570\u636e\u683c\u5f0f\uff0c\u800c<code>Cache-Control<\/code>\u5934\u5219\u53ef\u4ee5\u63d0\u4f9b\u7f13\u5b58\u7b56\u7565\u7684\u4fe1\u606f\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5206\u6790\u54cd\u5e94\u5934<\/strong><\/p>\n<\/p>\n<p><p>\u54cd\u5e94\u5934\u4e2d\u5305\u542b\u4e86\u8bb8\u591a\u6709\u7528\u7684\u4fe1\u606f\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<ul>\n<li><code>Date<\/code>\u5934\u8868\u793a\u54cd\u5e94\u53d1\u9001\u7684\u65f6\u95f4\u3002<\/li>\n<li><code>Set-Cookie<\/code>\u5934\u7528\u4e8e\u8bbe\u7f6e\u5ba2\u6237\u7aef\u7684Cookie\u3002<\/li>\n<li><code>Content-Encoding<\/code>\u5934\u6307\u793a\u4e86\u5185\u5bb9\u7684\u7f16\u7801\u65b9\u5f0f\uff08\u5982gzip\uff09\u3002<\/li>\n<\/ul>\n<p><p>\u901a\u8fc7\u5206\u6790\u8fd9\u4e9b\u4fe1\u606f\uff0c\u53ef\u4ee5\u4f18\u5316\u8bf7\u6c42\u548c\u54cd\u5e94\u7684\u5904\u7406\u903b\u8f91\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u56db\u3001\u4f7f\u7528\u5176\u4ed6\u5e93\u6216\u5de5\u5177\u67e5\u770b\u54cd\u5e94\u5934<\/p>\n<\/p>\n<p><p>\u9664\u4e86<code>requests<\/code>\u5e93\uff0cPython\u8fd8\u6709\u5176\u4ed6\u5e93\u548c\u5de5\u5177\u53ef\u4ee5\u7528\u4e8e\u67e5\u770bHTTP\u54cd\u5e94\u5934\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u4f7f\u7528<code>http.client<\/code>\u5e93<\/strong><\/p>\n<\/p>\n<p><p><code>http.client<\/code>\u662fPython\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u53ef\u4ee5\u7528\u4e8e\u66f4\u5e95\u5c42\u7684HTTP\u8bf7\u6c42\u5904\u7406\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import http.client<\/p>\n<p>conn = http.client.HTTPConnection(&quot;www.example.com&quot;)<\/p>\n<p>conn.request(&quot;GET&quot;, &quot;\/&quot;)<\/p>\n<p>response = conn.getresponse()<\/p>\n<p>headers = response.getheaders()<\/p>\n<p>print(headers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u63d0\u4f9b\u4e86\u5bf9HTTP\u8bf7\u6c42\u548c\u54cd\u5e94\u7684\u66f4\u7ec6\u7c92\u5ea6\u63a7\u5236\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528<code>curl<\/code>\u5de5\u5177<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u547d\u4ee4\u884c\u4e2d\u4f7f\u7528<code>curl<\/code>\u5de5\u5177\u4e5f\u53ef\u4ee5\u67e5\u770bHTTP\u54cd\u5e94\u5934\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">curl -I https:\/\/www.example.com<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>-I<\/code>\u9009\u9879\u8868\u793a\u53ea\u83b7\u53d6\u54cd\u5e94\u5934\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e94\u3001\u603b\u7ed3\u4e0e\u5b9e\u8df5<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\u67e5\u770bHTTP\u54cd\u5e94\u5934\u662f\u5f00\u53d1\u4e2d\u5e38\u89c1\u7684\u9700\u6c42\u3002\u901a\u8fc7<code>requests<\/code>\u5e93\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u8f7b\u677e\u83b7\u53d6\u548c\u5206\u6790\u54cd\u5e94\u5934\u4fe1\u606f\uff0c\u4e3a\u8c03\u8bd5\u548c\u4f18\u5316\u63d0\u4f9b\u4e86\u6781\u5927\u7684\u4fbf\u5229\u3002\u6b64\u5916\uff0c\u7ed3\u5408\u5176\u4ed6\u5de5\u5177\u548c\u5e93\uff0c\u53ef\u4ee5\u5728\u4e0d\u540c\u7684\u5e94\u7528\u573a\u666f\u4e2d\u7075\u6d3b\u67e5\u770b\u54cd\u5e94\u5934\u3002\u5b9e\u8df5\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5c06\u5e2e\u52a9\u4f60\u66f4\u6df1\u5165\u5730\u7406\u89e3HTTP\u534f\u8bae\uff0c\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u4f7f\u7528requests\u5e93\u83b7\u53d6HTTP\u54cd\u5e94\u5934\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528requests\u5e93\u8f7b\u677e\u83b7\u53d6HTTP\u54cd\u5e94\u5934\u3002\u53d1\u9001\u8bf7\u6c42\u540e\uff0c\u901a\u8fc7<code>response.headers<\/code>\u5c5e\u6027\u53ef\u4ee5\u67e5\u770b\u54cd\u5e94\u5934\u4fe1\u606f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">import requests\n\nresponse = requests.get(&#39;https:\/\/example.com&#39;)\nprint(response.headers)\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u6240\u6709\u54cd\u5e94\u5934\u4fe1\u606f\u7684\u5b57\u5178\uff0c\u60a8\u53ef\u4ee5\u6839\u636e\u9700\u8981\u63d0\u53d6\u7279\u5b9a\u7684\u5934\u4fe1\u606f\u3002<\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528urllib\u5e93\u67e5\u770bHTTP\u54cd\u5e94\u5934\uff1f<\/strong><br \/>\u9664\u4e86requests\u5e93\uff0curllib\u5e93\u4e5f\u662fPython\u4e2d\u5e38\u7528\u7684\u7f51\u7edc\u8bf7\u6c42\u5e93\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u83b7\u53d6\u54cd\u5e94\u5934\uff1a  <\/p>\n<pre><code class=\"language-python\">import urllib.request\n\nresponse = urllib.request.urlopen(&#39;https:\/\/example.com&#39;)\nprint(response.getheaders())\n<\/code><\/pre>\n<p>\u8be5\u65b9\u6cd5\u5c06\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u6240\u6709\u54cd\u5e94\u5934\u7684\u5217\u8868\uff0c\u6bcf\u4e2a\u5934\u4fe1\u606f\u4ee5\u5143\u7ec4\u7684\u5f62\u5f0f\u8868\u793a\u3002<\/p>\n<p><strong>\u5982\u679c\u8981\u67e5\u770b\u7279\u5b9a\u7684\u54cd\u5e94\u5934\uff0c\u5e94\u8be5\u600e\u4e48\u505a\uff1f<\/strong><br \/>\u65e0\u8bba\u662f\u4f7f\u7528requests\u8fd8\u662furllib\uff0c\u60a8\u90fd\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9a\u5934\u7684\u540d\u79f0\u6765\u67e5\u770b\u7279\u5b9a\u7684\u54cd\u5e94\u5934\u3002\u5bf9\u4e8erequests\uff0c\u60a8\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a  <\/p>\n<pre><code class=\"language-python\">content_type = response.headers.get(&#39;Content-Type&#39;)\nprint(content_type)\n<\/code><\/pre>\n<p>\u5bf9\u4e8eurllib\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u7c7b\u4f3c\u7684\u65b9\u5f0f\u83b7\u53d6\u7279\u5b9a\u5934\u4fe1\u606f\uff1a  <\/p>\n<pre><code class=\"language-python\">content_type = dict(response.getheaders()).get(&#39;Content-Type&#39;)\nprint(content_type)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u60a8\u66f4\u5feb\u901f\u5730\u83b7\u53d6\u9700\u8981\u7684\u4fe1\u606f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u67e5\u770b\u54cd\u5e94\u5934\u7684\u5e38\u7528\u65b9\u6cd5\u662f\u4f7f\u7528requests\u5e93\u3001\u901a\u8fc7Response.headers\u5c5e\u6027\u83b7\u53d6\u3001\u8c03 [&hellip;]","protected":false},"author":3,"featured_media":994385,"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\/994375"}],"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=994375"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/994375\/revisions"}],"predecessor-version":[{"id":994387,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/994375\/revisions\/994387"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/994385"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=994375"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=994375"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=994375"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}