{"id":370,"date":"2022-06-10T03:11:02","date_gmt":"2022-06-10T03:11:02","guid":{"rendered":"http:\/\/fanaticboy.com\/?p=370"},"modified":"2022-06-10T07:31:50","modified_gmt":"2022-06-10T07:31:50","slug":"%e4%bd%bf%e7%94%a8flask%e6%a1%86%e6%9e%b6%e5%86%99%e6%8c%a1%e6%9d%bf","status":"publish","type":"post","link":"https:\/\/fanaticboy.com\/?p=370","title":{"rendered":"\u4f7f\u7528flask\u6846\u67b6\u5199\u6321\u677f"},"content":{"rendered":"\n<p>flask\u6846\u67b6\u529f\u80fd\u4e5f\u662f\u6bd4\u8f83\u591a\uff0c\u90a3\u4e48\u4eca\u5929\u5c31\u8bb2\u4e00\u4e0b\u5728\u6321\u677f\u4e0a\u7684\u5e94\u7528\u3002\u9996\u5148\u8bf4\u4e00\u4e0b\u4ec0\u4e48\u6321\u677f\u5462\uff0c\u8fd9\u4e2a\u8bcd\u4e00\u822c\u5728\u6027\u80fd\u6d4b\u8bd5\u4e2d\u6bd4\u8f83\u5e38\u89c1\uff0c\u5c31\u662f\u5b8c\u6574\u7684\u6d4b\u8bd5\u73af\u5883\u6ca1\u6709\u642d\u5b8c\uff0c\u6216\u8005\u6d4b\u8bd5\u7684\u6210\u672c\u5f88\u9ad8\uff0c\u6bd4\u5982\u53d1\u9001\u77ed\u4fe1\u7b49\u3002\u90a3\u4e48\u5c31\u9700\u8981\u6a21\u62df\u4e00\u4e9b\u6ca1\u6709\u642d\u6216\u6210\u672c\u9ad8\u7684\u7684\u529f\u80fd\uff0c\u8fd9\u5c31\u53ef\u4ee5\u7528\u6321\u677f\u6765\u505a\uff0c\u76f4\u89c2\u7684\u8bf4\u5c31\u662f\u6a21\u62df\u771f\u5b9e\u7684\u73af\u5883\u3002<\/p>\n\n\n\n<p>\u54b1\u4eec\u5bf9\u6321\u677f\u4e5f\u662f\u6709\u4e86\u4e00\u5b9a\u7684\u4e86\u89e3\uff0c\u90a3\u4e48\u5177\u4f53\u600e\u4e48\u642d\u5efa\u5462\uff0c\u63a5\u4e0b\u6765\u5c31\u8ddf\u7740\u6211\uff0c\u642d\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u6321\u677f<\/p>\n\n\n\n<p>\u4e00\u3001\u6253\u5f00python\u7f16\u8f91\u5668\uff0c\u8fd9\u91cc\u5462\u6211\u4f7f\u7528\u7684\u662fpycharm\uff0c\u521b\u5efa\u4e00\u4e2a\u5de5\u7a0b\uff0c\u53eb\u4ec0\u4e48\u90fd\u53ef\u4ee5\uff0c\u5728\u91cc\u9762\u5efa\u4e00\u4e2arun.py<\/p>\n\n\n\n<p><img decoding=\"async\" alt=\"\" src=\"https:\/\/img-blog.csdnimg.cn\/img_convert\/96188b29c91a1e188154d5c0d9bfbbb9.png\">\u200b<img loading=\"lazy\" decoding=\"async\" height=\"15\" src=\"blob:http:\/\/fanaticboy.com\/3af4d634-2521-45f7-bb55-bf532b1abbca\" width=\"15\">\u7f16\u8f91<\/p>\n\n\n\n<p>\u4e8c\u3001\u7f16\u8f91\u4ee3\u7801\uff0c\u8fd9\u8fb9\u9700\u8981\u6709flask\u57fa\u7840\u7684\u540c\u5b66\uff0c&nbsp;\u5982\u679c\u6ca1\u6709\u7684\u8bdd\u53ef\u4ee5\u770b\u770b\u6211\u4e4b\u524d\u53d1\u7684\u5173\u4e8eflask\u6846\u67b6\u7684\u57fa\u7840\u77e5\u8bc6<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from&nbsp;flask&nbsp;import&nbsp;Flask,&nbsp;session,&nbsp;request,&nbsp;redirect,&nbsp;url_for\uff0cResponse,&nbsp;jsonify\nimport json\nimport time\napp = Flask(__name__)\n# \u5b9a\u4e49\u63a5\u53e3\u8def\u5f84\n@app.route('\/api\/login', methods=&#91;'post', 'get'])\ndef do_login():\n    # \u5224\u65ad\u8bf7\u6c42\u7684\u65b9\u5f0f\u662fpost\u8fd8\u662fget\n    if request.method == \"GET\":\n        fail = {\n            \"timestamp\": (time.asctime()),\n            \"error\": 1\n        }\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;get\u8bf7\u6c42\u7684\u8bdd\u8fd4\u56defail\u6570\u636e\n        return jsonify(fail)\n&nbsp;&nbsp;&nbsp;&nbsp;# post\u8bf7\u6c42\u7684\u8bdd\u8fd4\u56desuccess\u6570\u636e\n&nbsp;&nbsp;&nbsp;&nbsp;else:\n        success = {\n            \"data\": {\n                \"user_info\": request.get_json()&#91;\"user_info\"],\n                \"password\": request.get_json()&#91;\"password\"]\n            },\n            # \u65f6\u95f4\u6233\n            \"timestamp\": (time.asctime()),\n            \"success\": 1\n        }\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;\u8fd4\u56dejson\u5f62\u5f0f\u7684\u6570\u636e\n        return jsonify(success)\n        \nif __name__ == '__main__':\n    app.run(debug=True)<\/code><\/pre>\n\n\n\n<p><img loading=\"lazy\" decoding=\"async\" height=\"15\" src=\"blob:http:\/\/fanaticboy.com\/550e261c-ac53-4713-ad6a-87d14bb58d4e\" width=\"15\"><\/p>\n\n\n\n<p>\u4e09\u3001\u8fd0\u884c\u4ee3\u7801<\/p>\n\n\n\n<p><img decoding=\"async\" alt=\"\" src=\"https:\/\/img-blog.csdnimg.cn\/img_convert\/3a1ae40d790a4cca51e5d2dd49990e7f.png\">\u200b<img loading=\"lazy\" decoding=\"async\" height=\"15\" src=\"blob:http:\/\/fanaticboy.com\/e1289e7a-006c-44f1-9b6d-262dae4620f6\" width=\"15\">\u7f16\u8f91<\/p>\n\n\n\n<p>\u597d\u4e86\uff0c\u8fd9\u6837\u4e00\u4e2a\u7b80\u5355\u7684\u6321\u677f\u670d\u52a1\u5668\u5c31\u53ef\u4ee5\u4e86\uff0c\u5176\u5b9e\u5f88\u7b80\u5355\uff0c\u76f8\u6bd4\u6b63\u5f0f\u4e1a\u52a1\u6765\u8bf4\u6ca1\u6709\u590d\u6742\u7684\u4e1a\u52a1\u5904\u7406\u7684\u8fc7\u7a0b\uff0c\u5c31\u662f\u7b80\u5355\u7684\u6a21\u62df\u8fd4\u56de\u62a5\u6587\u7684\u8fc7\u7a0b\uff0c\u63a5\u4e0b\u6765\u54b1\u4eec\u7528requests\u6765\u6d4b\u8bd5\u4e00\u4e0b\uff0c\u770b\u6548\u679c\u600e\u4e48\u6837<\/p>\n\n\n\n<p>\u65b0\u5efatest.py\u6587\u4ef6<\/p>\n\n\n\n<p><img decoding=\"async\" alt=\"\" src=\"https:\/\/img-blog.csdnimg.cn\/img_convert\/d839da9fefd7e202a4ae3affbef5d239.png\">\u200b<img loading=\"lazy\" decoding=\"async\" height=\"15\" src=\"blob:http:\/\/fanaticboy.com\/cb2dda65-8b3b-4422-9406-872b69a83666\" width=\"15\">\u7f16\u8f91<\/p>\n\n\n\n<p>\u91cc\u9762\u5199\u4e0a\u4ee3\u7801<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import json\nimport requests\n# GET\u8bf7\u6c42\n# requests.get(url, params=None\uff09\n# POST\u8bf7\u6c42\nurl = \"http:\/\/127.0.0.1:5000\/api\/login\"\ndata = {\n    \"user_info\": \"xiaoli\",\n    \"password\": \"123456\"\n}\n#&nbsp;\u8bf7\u6c42\u5934\uff0c\u8fd9\u91cc\u53d1\u9001\u7684\u662fjson\u6570\u636e\uff0c\u6240\u4ee5\u8981\u9009\u62e9\u4ee5\u4e0b\u5f62\u5f0f\nheaders = {'Content-Type': 'application\/json'}\n#&nbsp;\u5c06\u666e\u901a\u7684\u5b57\u5178\u7c7b\u578b\u8f6c\u6362\u6210json\u7c7b\u578b\ndata_json = json.dumps(data)\n#&nbsp;\u5176\u4e2ddata\u53c2\u6570\u5c31\u662f\u53d1\u9001\u7684\u6570\u636e\uff0c\u4e5f\u5c31\u662f\u54b1\u4eec\u7684json\u7c7b\u578b\u6570\u636e\nres = requests.post(url, data=data_json, headers=headers, json=None)\nprint(res.text)<\/code><\/pre>\n\n\n\n<p><img loading=\"lazy\" decoding=\"async\" height=\"15\" src=\"blob:http:\/\/fanaticboy.com\/91b7b34f-6898-49c9-a60c-c7052b467b28\" width=\"15\"><\/p>\n\n\n\n<p>\u4e0b\u9762\u8fd0\u884c\uff0c\u4e00\u4e0b\u7231\uff0c\u770b\u4e00\u4e0b\u8fd4\u56de\u7ed3\u679c<\/p>\n\n\n\n<p><img decoding=\"async\" alt=\"\" src=\"https:\/\/img-blog.csdnimg.cn\/img_convert\/4bded7a4d225b0d948b3526a08448f29.png\">\u200b<img loading=\"lazy\" decoding=\"async\" height=\"15\" src=\"blob:http:\/\/fanaticboy.com\/ed81b2ab-db05-43e6-b93f-528a042625c2\" width=\"15\">\u7f16\u8f91<\/p>\n\n\n\n<p>\u51fa\u73b0\u4ee5\u4e0a\u7684\u8fd4\u56de\u5185\u5bb9\u5c31\u8bf4\u660e\u54b1\u4eec\u7684\u6321\u677f\u914d\u7f6e\u6210\u529f\u4e86\u3002\u8fd9\u6837\u54b1\u4eec\u5c31\u53ef\u4ee5\u6839\u636e\u9700\u6c42\u8fd4\u56de\u6307\u5b9a\u7684\u8fd4\u56de\u5185\u5bb9\u4e86\uff0c\u5177\u4f53\u5c31\u662f\u5728\u8fd9\u6837\u7684\u57fa\u7840\u4e0a\u4fee\u4fee\u6539\u6539\u5c31\u53ef\u4ee5\u4e86 \u200b<\/p>\n","protected":false},"excerpt":{"rendered":"<p>flask\u6846\u67b6\u529f\u80fd\u4e5f\u662f\u6bd4\u8f83\u591a\uff0c\u90a3\u4e48\u4eca\u5929\u5c31\u8bb2\u4e00\u4e0b\u5728\u6321\u677f\u4e0a\u7684\u5e94\u7528\u3002\u9996\u5148\u8bf4\u4e00\u4e0b\u4ec0\u4e48\u6321\u677f\u5462\uff0c\u8fd9\u4e2a\u8bcd\u4e00\u822c\u5728\u6027\u80fd\u6d4b\u8bd5\u4e2d\u6bd4\u8f83 &#8230; <a title=\"\u4f7f\u7528flask\u6846\u67b6\u5199\u6321\u677f\" class=\"read-more\" href=\"https:\/\/fanaticboy.com\/?p=370\" aria-label=\"\u9605\u8bfb \u4f7f\u7528flask\u6846\u67b6\u5199\u6321\u677f\">\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":[17,15],"tags":[],"class_list":["post-370","post","type-post","status-publish","format-standard","hentry","category-flask","category-python"],"_links":{"self":[{"href":"https:\/\/fanaticboy.com\/index.php?rest_route=\/wp\/v2\/posts\/370","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=370"}],"version-history":[{"count":1,"href":"https:\/\/fanaticboy.com\/index.php?rest_route=\/wp\/v2\/posts\/370\/revisions"}],"predecessor-version":[{"id":371,"href":"https:\/\/fanaticboy.com\/index.php?rest_route=\/wp\/v2\/posts\/370\/revisions\/371"}],"wp:attachment":[{"href":"https:\/\/fanaticboy.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=370"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fanaticboy.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=370"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fanaticboy.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=370"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}