{"id":654,"date":"2021-11-13T14:52:00","date_gmt":"2021-11-13T14:52:00","guid":{"rendered":"https:\/\/262235.xyz\/?p=654"},"modified":"2021-11-13T14:52:00","modified_gmt":"2021-11-13T14:52:00","slug":"654","status":"publish","type":"post","link":"https:\/\/lyvba.com\/index.php\/2021\/11\/13\/654\/","title":{"rendered":"Python\u4f7f\u7528requests\u5e93 HTTP \u8bf7\u6c42\u54cd\u5e94\u5b66\u4e60\u548c httpbin.org \u5bb9\u5668\u90e8\u7f72"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/lyvba.com\/wp-content\/uploads\/2021\/11\/585788748.webp\" alt=\"http.webp\" title=\"http.webp\"><br \/>httpbin.org \u8fd9\u4e2a\u7f51\u7ad9\u80fd\u6d4b\u8bd5 HTTP \u8bf7\u6c42\u548c\u54cd\u5e94\u7684\u5404\u79cd\u4fe1\u606f\uff0c\u6bd4\u5982 cookie\u3001ip\u3001headers \u548c\u767b\u5f55\u9a8c\u8bc1\u7b49\uff0c\u4e14\u652f\u6301 GET\u3001POST \u7b49\u591a\u79cd\u65b9\u6cd5\uff0c\u5bf9 web \u5f00\u53d1\u548c\u6d4b\u8bd5\u5f88\u6709\u5e2e\u52a9\u3002<\/p>\n<p>\u5b83\u7528 Python + Flask \u7f16\u5199\uff0c\u662f\u4e00\u4e2a\u5f00\u6e90\u9879\u76ee\u3002<\/p>\n<h3>\u5b98\u65b9\u7f51\u7ad9\uff1a<a href=\"http:\/\/httpbin.org\/\">http:\/\/httpbin.org\/<\/a><\/h3>\n<p><img decoding=\"async\" src=\"https:\/\/lyvba.com\/wp-content\/uploads\/2021\/11\/3741599335.png\" alt=\"httpbin.png\" title=\"httpbin.png\"><\/p>\n<h2>Python\u4f7f\u7528requests\u5e93: \u590d\u6742\u7684 POST \u8bf7\u6c42<\/h2>\n<p>\u901a\u5e38\uff0c\u4f60\u60f3\u8981\u53d1\u9001\u4e00\u4e9b\u7f16\u7801\u4e3a\u8868\u5355\u5f62\u5f0f\u7684\u6570\u636e\u2014\u2014\u975e\u5e38\u50cf\u4e00\u4e2a HTML \u8868\u5355\u3002\u8981\u5b9e\u73b0\u8fd9\u4e2a\uff0c\u53ea\u9700\u7b80\u5355\u5730\u4f20\u9012\u4e00\u4e2a\u5b57\u5178\u7ed9 data \u53c2\u6570\u3002<br \/>\u4f60\u7684\u6570\u636e\u5b57\u5178\u5728\u53d1\u51fa\u8bf7\u6c42\u65f6\u4f1a\u81ea\u52a8\u7f16\u7801\u4e3a\u8868\u5355\u5f62\u5f0f\uff1a<\/p>\n<pre><code>import requests\npayload = (('key1', 'value1'), ('key1', 'value2'))\nr = requests.post('http:\/\/httpbin.org\/post', data=payload)\nprint(r.text)<\/code><\/pre>\n<ul>\n<li>\n<p>\u8fd4\u56de\u503c <code>json<\/code> \u6570\u636e<\/p>\n<pre><code>{\n\"args\": {},\n\"data\": \"\",\n\"files\": {},\n\"form\": {\n  \"key1\": [\n    \"value1\",\n    \"value2\"\n  ]\n},\n\"headers\": {\n  \"Accept\": \"*\/*\",\n  \"Accept-Encoding\": \"gzip, deflate\",\n  \"Content-Length\": \"23\",\n  \"Content-Type\": \"application\/x-www-form-urlencoded\",\n  \"Host\": \"httpbin.org\",\n  \"User-Agent\": \"python-requests\/2.25.1\",\n  \"X-Amzn-Trace-Id\": \"Root=1-618fd260-75b10ed47a82fac820a8c6da\"\n},\n\"json\": null,\n\"origin\": \"188.212.108.88\",\n\"url\": \"http:\/\/httpbin.org\/post\"\n}<\/code><\/pre>\n<\/li>\n<\/ul>\n<h3>httpbin.org \u5bb9\u5668\u90e8\u7f72<\/h3>\n<ul>\n<li>\u4f7f\u7528\u5b98\u65b9\u7248\uff0c\u6bd4\u8f83\u5360\u7528\u7a7a\u95f4\uff0c\u5bb9\u5668\u5927\u5c0f534M<\/li>\n<li>\n<p>\u4f7f\u7528 <code>simonkowallik\/httpbin-alpine<\/code> \u955c\u50cf\u53ea\u9700\u8981 100M<\/p>\n<pre><code>docker run  --name httpbin  --restart=always  -d -p 80:80 simonkowallik\/httpbin-alpine\n\ndocker run  --name httpbin  --restart=always  -d -p 80:80 kennethreitz\/httpbin\n\nREPOSITORY                     TAG       IMAGE ID       CREATED        SIZE\nsimonkowallik\/httpbin-alpine   latest    a7befd1a1b0e   9 months ago   99.6MB\nkennethreitz\/httpbin           latest    b138b9264903   3 years ago    534MB<\/code><\/pre>\n<\/li>\n<\/ul>\n<h2>Python\u4f7f\u7528requests\u5e93 \u8bbf\u95ee\u672c\u5730\u7f51\u7ad9\u6d4b\u8bd5 HTTP \u8bf7\u6c42\u548c\u54cd\u5e94\u7684\u5404\u79cd\u4fe1\u606f<\/h2>\n<pre><code>&gt;&gt;&gt; r = requests.post('http:\/\/192.168.1.150\/post', data=payload)\n&gt;&gt;&gt; print(r.text)\n\n{\n  \"args\": {},\n  \"data\": \"\",\n  \"files\": {},\n  \"form\": {\n    \"key1\": [\n      \"value1\",\n      \"value2\"\n    ]\n  },\n  \"headers\": {\n    \"Accept\": \"*\/*\",\n    \"Accept-Encoding\": \"gzip, deflate\",\n    \"Connection\": \"keep-alive\",\n    \"Content-Length\": \"23\",\n    \"Content-Type\": \"application\/x-www-form-urlencoded\",\n    \"Host\": \"192.168.1.150\",\n    \"User-Agent\": \"python-requests\/2.25.1\"\n  },\n  \"json\": null,\n  \"origin\": \"192.168.1.121\",\n  \"url\": \"http:\/\/192.168.1.150\/post\"\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>httpbin.org \u8fd9\u4e2a\u7f51\u7ad9\u80fd\u6d4b\u8bd5 HTTP \u8bf7\u6c42\u548c\u54cd\u5e94\u7684\u5404\u79cd\u4fe1\u606f\uff0c\u6bd4\u5982 cookie\u3001ip\u3001h [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,12],"tags":[15,45],"class_list":["post-654","post","type-post","status-publish","format-standard","hentry","category-docker","category-learn","tag-docker","tag-python"],"_links":{"self":[{"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/posts\/654","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/comments?post=654"}],"version-history":[{"count":0,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/posts\/654\/revisions"}],"wp:attachment":[{"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/media?parent=654"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/categories?post=654"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/tags?post=654"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}