{"id":97,"date":"2022-04-09T19:22:46","date_gmt":"2022-04-09T19:22:46","guid":{"rendered":"http:\/\/fanaticboy.com\/?p=97"},"modified":"2022-04-09T19:22:47","modified_gmt":"2022-04-09T19:22:47","slug":"python-requests%e7%9a%84%e5%9f%ba%e6%9c%ac%e7%94%a8%e6%b3%95","status":"publish","type":"post","link":"https:\/\/fanaticboy.com\/?p=97","title":{"rendered":"python requests\u7684\u57fa\u672c\u7528\u6cd5"},"content":{"rendered":"\n<p>\u200b<\/p>\n\n\n\n<p>&nbsp;\u5927\u5bb6\u90fd\u77e5\u9053\u4e86\u63a5\u53e3\u6d4b\u8bd5\uff0c\u90a3\u4e48\u90fd\u6709\u4ec0\u4e48\u6d4b\u8bd5\u65b9\u6cd5\u5462\uff0c\u54b1\u4eec\u6709\u5e38\u7528\u7684\u5de5\u5177postman\uff0cjmeter\u7b49\u3002\u8fd9\u4e9b\u90fd\u63d0\u4f9b\u4e86\u5f88\u5f3a\u5927\u7684\u529f\u80fd\uff0c\u4f46\u8fd8\u6709\u57fa\u4e8e\u4ee3\u7801\u7684\u6d4b\u8bd5\uff0c\u90a3\u5c31\u662fpython requests\uff0c\u5982\u679c\u60a8\u6709\u57fa\u672c\u7684\u7f16\u7a0b\u80fd\u529b\uff0c\u60a8\u53ef\u4ee5\u8003\u8651\u4e00\u4e0b\u8fd9\u4e2a\uff0c\u4f18\u70b9\u5728\u4e8e\u53ef\u4ee5\u81ea\u52a8\u5316\uff0c\u7ef4\u62a4\u6210\u672c\u66f4\u4f4e\u3002\u90a3\u4e48\u672c\u7bc7\u4e5f\u662f\u7ed9\u5927\u5bb6\u4ecb\u7ecd\u4e00\u4e0b\u57fa\u672c\u7684\u4f7f\u7528\u65b9\u6cd5\u3002<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;\u90a3\u4e48\u65e2\u7136\u662f\u63a5\u53e3\u6d4b\u8bd5\uff0c\u90a3\u4e48\u5c31\u9700\u8981\u6709\u63a5\u53e3\uff0c\u8fd9\u4e2a\u63a5\u53e3\u53ef\u4ee5\u662f\u516c\u5171\u7684api\uff0c\u6bd4\u5982\u767e\u5ea6\u7ffb\u8bd1\u63a5\u53e3\uff0c\u5fae\u4fe1\u516c\u4f17\u53f7\u63a5\u53e3\uff0c\u5fae\u4fe1\u767b\u5f55\u63a5\u53e3\uff0c\u652f\u4ed8\u5b9d\u4ed8\u6b3e\u63a5\u53e3\u7b49\uff0c\u5f53\u7136\u8fd8\u53ef\u4ee5\u662f\u4e00\u4e2a\u81ea\u5df1\u642d\u597d\u7684\u7f51\u7ad9\uff0c\u90a3\u4e48\u672c\u6b21\u54b1\u4eec\u5c31\u4ee5\u6211\u81ea\u5df1\u5199\u7684\u4e00\u4e2a\u6700\u7b80\u5355\u7684\u4f5c\u4e3a\u54b1\u4eec\u7684\u6d4b\u8bd5\u63a5\u53e3\uff0c\u5177\u4f53\u8fd8\u662f\u8bb2python requests\u7684\u7528\u6cd5\uff0c\u600e\u4e48\u600e\u4e48\u5199\u63a5\u53e3\u5462\uff0c\u54b1\u4eec\u5c31\u7528python\u4e2d\u7684flask\u6846\u67b6\u5199\u4e00\u4e2a\u5f88\u7b80\u5355\u7684\u4ee3\u7801\uff0c\u5728\u5199\u4e4b\u524d\u9700\u8981\u5b89\u88c5\u4e00\u4e2aflask\u6a21\u5757\uff0c\u5177\u4f53\u76f4\u63a5pip install flask\u5c31\u53ef\u4ee5\u4e86\u3002\u90a3\u4e48\u5177\u4f53\u5199\u51fa\u6765\u662f\u4ec0\u4e48\u6548\u679c\u5462\uff0c\u54b1\u4eec\u6765\u770b\u4ee3\u7801<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from flask import Flask,request\n\napp = Flask(__name__)  # ,template_folder=\napp.debug = True  # \u81ea\u52a8\u91cd\u542f\n# \u8def\u7531\u51fd\u6570\uff0c\u6539\u53d8\u51fd\u6570\u6267\u884c\u7684\u51fd\u6570\uff0c\u7406\u89e3\u4e3a\u8f93\u5165\u5730\u5740\u6267\u884c\u5bf9\u5e94\u5730\u5740\u7684\u51fd\u6570\n@app.route(\"\/\", methods=&#91;'GET', 'POST'])  # method\u52a0\u4e0a\u5176\u4ed6\u65b9\u6cd5\uff0c\u5426\u5219\u53ea\u6709get\ndef go():\n    if request.method==\"GET\":\n        return {\"code\": \"success\"}\n\n    elif request.method==\"POST\":\n        # \u83b7\u53d6post\u4f20\u8fc7\u6765\u7684\u503c\uff0c\u7136\u540e\u4f5c\u6bd4\u8f83\n        if request.form.get(\"user\")==\"xiaoli\" and request.form.get(\"pwd\")==\"123456\":\n            return {\"code\": \"success\"}\n        else:\n            return {\"code\": \"ERROR\"}\n\n# \u542f\u52a8\u670d\u52a1\u5668\nif __name__ == '__main__':\n    app.run()<\/code><\/pre>\n\n\n\n<p><img loading=\"lazy\" decoding=\"async\" src=\"blob:http:\/\/fanaticboy.com\/9b55ea97-d94a-40de-8f7c-2d944b74dcf3\" width=\"15\" height=\"15\"><\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;\u5982\u679c\u5927\u5bb6\u4e0d\u61c2\uff0c\u53ef\u4ee5\u770b\u770bflask\u6846\u67b6\u7684\u4e00\u4e9b\u77e5\u8bc6\uff0c\u8fd9\u91cc\u4e0d\u518d\u8fc7\u591a\u8bb2\u89e3\uff0c\u4e5f\u53ef\u4ee5\u5c06\u4e0a\u9762\u7684\u4ee3\u7801\u590d\u5236\uff0c\u76f4\u63a5\u8fd0\u884c\u5c31\u53ef\u4ee5\u4e86\uff0c\u5c31\u53ef\u4ee5\u628a\u672c\u673a\u5f53\u505a\u4e00\u4e2a\u670d\u52a1\u5668\u4e86\uff0curl\u662fhttp:\/\/127.0.0.1:5000\/\u3002\u54b1\u4eec\u5199\u5b8c\u4e86\u670d\u52a1\u7aef\uff0c\u54b1\u4eec\u5f00\u59cb\u5199python requests\u7684\u5177\u4f53\u4ee3\u7801\u4e86\uff0c\u5982\u4e0b\u200b\u200b\u200b\u200b\u200b\u200b\u200b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># \u9700\u8981\u5bfc\u5165requests\u6a21\u5757\nimport&nbsp;requests\n# \u7b80\u5355\u7684get\u8bf7\u6c42\uff0c\u7528get\u65b9\u6cd5\nres = requests.get(\"http:\/\/127.0.0.1:5000\/\")\n# print(res.text)\n# \u83b7\u53d6\u54cd\u5e94\u989d\u7ed3\u679c\nprint(res.content.decode(\"utf-8\"))<\/code><\/pre>\n\n\n\n<p><img loading=\"lazy\" decoding=\"async\" src=\"blob:http:\/\/fanaticboy.com\/7e6d71cf-0693-42b5-8354-e8e4b6648f3b\" width=\"15\" height=\"15\"><\/p>\n\n\n\n<p>\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\u56fe\u6240\u793a<\/p>\n\n\n\n<p><img decoding=\"async\" alt=\"\" src=\"https:\/\/img-blog.csdnimg.cn\/img_convert\/c2f298581c96645a8517dbba3811fc45.png\">\u200b<img loading=\"lazy\" decoding=\"async\" src=\"blob:http:\/\/fanaticboy.com\/a6d62d49-c87b-4e12-8b78-86b992786d0d\" width=\"15\" height=\"15\"><\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;\u4ee5\u4e0a\u5c31\u662f\u8fd0\u884c\u4e86\u7b80\u5355\u7684get\u8bf7\u6c42\uff0c\u8fd4\u56de\u7684\u6570\u636e\u8bf4\u660e\u6210\u529f\u4e86\uff0c\u63a5\u53e3\u6ca1\u6709\u95ee\u9898\uff0c\u4e0b\u9762\u6267\u884c\u4e00\u4e0bpost\u8bf7\u6c42\u4ee3\u7801\u5982\u4e0b\u200b\u200b\u200b\u200b\u200b\u200b\u200b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import requests\nurl = \"http:\/\/127.0.0.1:5000\/\"\ndata = {\"user\": \"xiaoli\",\n            \"pwd\":\"123456\" }\n\n# \u7b80\u5355\u7684get\u8bf7\u6c42\n# res = requests.get(\"http:\/\/127.0.0.1:5000\/\")\n# print(res.text)\n#&nbsp;\u6267\u884cpost\u8bf7\u6c42\uff0c\u53c2\u6570\u662furl\u548cbody\u6570\u636e\uff0c\u5982\u679c\u6709\u5934\u90e8\u53c2\u6570\u7279\u6b8a\u586b\u5199\u5219\u540e\u9762\u52a0\u4e0aheaders=\u201c\u201d\nres = requests.post(url=url, data=data)\nprint(res.content.decode(\"utf-8\"))<\/code><\/pre>\n\n\n\n<p><img loading=\"lazy\" decoding=\"async\" src=\"blob:http:\/\/fanaticboy.com\/e9a57359-3e77-4a79-b928-274ce437dd06\" width=\"15\" height=\"15\"><\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;\u4e0b\u9762\u7684\u7ed3\u679c\u5c31\u662fpost\u6210\u529f\u7684\u7ed3\u679c\uff0c\u7528\u6237\u540d\u5bc6\u7801\u4e00\u81f4\u3002<\/p>\n\n\n\n<p><img decoding=\"async\" alt=\"\" src=\"https:\/\/img-blog.csdnimg.cn\/img_convert\/ab503bc6a43544b779019aefc1df9ded.png\">\u200b<img loading=\"lazy\" decoding=\"async\" src=\"blob:http:\/\/fanaticboy.com\/38d156de-5495-4130-a2af-e9b0a00e3a5f\" width=\"15\" height=\"15\"><\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;\u4e0a\u9762\u54b1\u4eec\u4f20\u7684\u6570\u636e\u662f\u6b63\u786e\u7684\uff0c\u54b1\u4eec\u4f20\u4e00\u4e0b\u9519\u8bef\u7684\uff0c\u770b\u4ee3\u7801\u5982\u4f55\u53cd\u5e94\uff0c\u628a\u6570\u636euser\u6539\u6210xiaoli12\uff0c\u770b\u770b\u600e\u4e48\u53cd\u5e94\u200b\u200b\u200b\u200b\u200b\u200b\u200b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import requests\nurl = \"http:\/\/127.0.0.1:5000\/\"\n#&nbsp;user\u6539\u6210xiaoli123\ndata = {\"user\": \"xiaoli123\",\n            \"pwd\":\"123456\" }\n\n# \u7b80\u5355\u7684get\u8bf7\u6c42\n# res = requests.get(\"http:\/\/127.0.0.1:5000\/\")\n# print(res.text)\nres = requests.post(url=url, data=data)\nprint(res.content.decode(\"utf-8\"))<\/code><\/pre>\n\n\n\n<p><img loading=\"lazy\" decoding=\"async\" src=\"blob:http:\/\/fanaticboy.com\/6bd851fa-053b-481a-8ff0-792fbb1448c4\" width=\"15\" height=\"15\"><\/p>\n\n\n\n<p>\u7ed3\u679c\u5982\u4e0b\u6240\u793a<\/p>\n\n\n\n<p><img decoding=\"async\" alt=\"\" src=\"https:\/\/img-blog.csdnimg.cn\/img_convert\/0a54de9d56462f5f91c56cf2eb31cae8.png\">\u200b<img loading=\"lazy\" decoding=\"async\" src=\"blob:http:\/\/fanaticboy.com\/51d6f048-25ef-412f-b219-f38c0406f31a\" width=\"15\" height=\"15\"><\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;\u8fd9\u4e2a\u4e5f\u662f\u4e0e\u9884\u671f\u7ed3\u679c\u4e00\u6837\uff0c\u6ca1\u6709\u95ee\u9898\uff0c\u4fdd\u6301\u7528\u6237\u552f\u4e00\u6027\uff0c<\/p>\n\n\n\n<p>\u4ee5\u4e0a\u5c31\u662f\u54b1\u4eecrequests\u7684\u57fa\u7840\u7528\u6cd5\uff0c\u540e\u7eed\u4f1a\u7ee7\u7eed\u66f4\u65b0\u9ad8\u7ea7\u7684\u7528\u6cd5\uff0c\u6bd4\u5982\u81ea\u52a8\u5316\u7b49\uff0c\u597d\u4e86\u6587\u7ae0\u5c31\u5230\u8fd9\u91cc\u5566 \u200b<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u200b &nbsp;\u5927\u5bb6\u90fd\u77e5\u9053\u4e86\u63a5\u53e3\u6d4b\u8bd5\uff0c\u90a3\u4e48\u90fd\u6709\u4ec0\u4e48\u6d4b\u8bd5\u65b9\u6cd5\u5462\uff0c\u54b1\u4eec\u6709\u5e38\u7528\u7684\u5de5\u5177postman\uff0cjmeter\u7b49\u3002 &#8230; <a title=\"python requests\u7684\u57fa\u672c\u7528\u6cd5\" class=\"read-more\" href=\"https:\/\/fanaticboy.com\/?p=97\" aria-label=\"\u9605\u8bfb python requests\u7684\u57fa\u672c\u7528\u6cd5\">\u9605\u8bfb\u66f4\u591a<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15,2,9],"tags":[],"class_list":["post-97","post","type-post","status-publish","format-standard","hentry","category-python","category-postman","category-script"],"_links":{"self":[{"href":"https:\/\/fanaticboy.com\/index.php?rest_route=\/wp\/v2\/posts\/97","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fanaticboy.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fanaticboy.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fanaticboy.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fanaticboy.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=97"}],"version-history":[{"count":1,"href":"https:\/\/fanaticboy.com\/index.php?rest_route=\/wp\/v2\/posts\/97\/revisions"}],"predecessor-version":[{"id":98,"href":"https:\/\/fanaticboy.com\/index.php?rest_route=\/wp\/v2\/posts\/97\/revisions\/98"}],"wp:attachment":[{"href":"https:\/\/fanaticboy.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=97"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fanaticboy.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=97"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fanaticboy.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=97"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}