{"id":256106,"date":"2022-11-10T08:45:22","date_gmt":"2022-11-10T00:45:22","guid":{"rendered":"https:\/\/gulass.cn\/?p=256106"},"modified":"2022-10-28T12:46:10","modified_gmt":"2022-10-28T04:46:10","slug":"python-flask-look","status":"publish","type":"post","link":"https:\/\/gulass.cn\/python-flask-look.html","title":{"rendered":"Python Flask\u524d\u7aef\u81ea\u52a8\u767b\u5f55\u529f\u80fd\u5b9e\u73b0\u793a\u4f8b"},"content":{"rendered":"
\u5bfc\u8bfb<\/td>\n | \u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4ecb\u7ecd\u4e86Python Flask\u524d\u7aef\u81ea\u52a8\u767b\u5f55\u529f\u80fd\u5b9e\u73b0\uff0c\u672c\u6587\u7ed9\u5927\u5bb6\u4ecb\u7ecd\u7684\u975e\u5e38\u8be6\u7ec6\uff0c\u5bf9\u5927\u5bb6\u7684\u5b66\u4e60\u6216\u5de5\u4f5c\u5177\u6709\u4e00\u5b9a\u7684\u53c2\u8003\u501f\u9274\u4ef7\u503c\uff0c\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u53c2\u8003\u4e0b<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n \u5f15\u8a00<\/strong><\/div>\n \u5728\u5df2\u6709\u7684\u7f51\u7ad9\u4e2d\uff0c\u51e0\u4e4e\u6240\u6709\u7684\u7f51\u7ad9\u90fd\u5df2\u7ecf\u5b9e\u73b0\u4e86 \u81ea\u52a8\u767b\u5f55<\/p>\n \u6240\u8c13\u81ea\u52a8\u767b\u5f55\uff0c\u5176\u5b9e\u5c31\u662f\u5728\u4f60\u767b\u5f55\u540e\uff0c\u7136\u540e\u5173\u95ed\u6d4f\u89c8\u5668\uff0c\u63a5\u7740\u518d\u542f\u52a8\u6d4f\u89c8\u5668\u91cd\u65b0\u8fdb\u5165\u521a\u521a\u7684\u7f51\u7ad9\u65f6\uff0c\u65e0\u9700\u81ea\u5df1\u518d\u6b21\u767b\u5f55\u3002\u66f4\u51c6\u786e\u7684\u8bf4\uff0c\u5728\u4e00\u6bb5\u65f6\u95f4\u5185\uff0c\u65e0\u9700\u81ea\u5df1\u518d\u6b21\u767b\u5f55<\/p>\n \u601d\u8def\uff1a\u5176\u5b9e\u6240\u8c13\u7684\u81ea\u52a8\u767b\u5f55\uff0c\u5230\u6700\u540e\u7684\u540e\u7aef\u903b\u8f91\uff0c\u548c\u4f60\u6b63\u5e38\u7684\u767b\u5f55\u903b\u8f91\u662f\u4e00\u6837\u7684\uff0c\u4e5f\u662f\u5224\u65ad\u7528\u6237\u540d\u548c\u5bc6\u7801\u662f\u5426\u6b63\u786e\u3002\u53ea\u662f\u6211\u4eec\u8981\u7701\u7565\u8ba9\u7528\u6237\u518d\u6b21\u8f93\u5165\u7528\u6237\u540d\u548c\u5bc6\u7801\u7684\u6b65\u9aa4\uff0c\u90a3\u4e48\u80af\u5b9a\u5c31\u8981\u5c06\u7528\u6237\u540d\u548c\u5bc6\u7801\u5b58\u50a8\u5728\u4e00\u4e2a\u5730\u65b9\u3002\u5f53\u68c0\u6d4b\u5230\u7528\u6237\u518d\u6b21\u8fdb\u5165\u65f6\uff0c\u770b\u770b\u662f\u5426\u6ee1\u8db3\u53ef\u4ee5\u81ea\u52a8\u767b\u5f55\u7684\u6761\u4ef6\uff0c\u6ee1\u8db3\u7684\u8bdd\uff0c\u5c06\u4fdd\u5b58\u7684\u7528\u6237\u540d\u548c\u5bc6\u7801\u53d6\u51fa\u6765\uff0c\u5e2e\u7528\u6237\u81ea\u52a8\u767b\u5f55\u3002\u90a3\u6211\u4eec\u5c31\u8981\u53bb\u60f3\uff0c\u5c06\u7528\u6237\u540d\u548c\u5bc6\u7801\u5b58\u50a8\u5230\u4ec0\u4e48\u5730\u65b9<\/p>\n \u5728\u672c\u7bc7\u6587\u7ae0\u4e2d\uff0c\u672c\u4eba\u4f1a\u5728 Flask \u9879\u76ee\u4e2d\u4f7f\u7528 ** session + cookie** \u7684\u65b9\u5f0f\uff0c\u5b9e\u73b0\u4e0a\u8ff0\u7684\u529f\u80fd<\/p>\n \u5b9e\u73b0\u7684\u65b9\u5f0f\u6709\u5f88\u591a\u79cd\uff0c\u4e0b\u9762\u7684\u53ea\u662f\u5176\u4e2d\u4e00\u79cd\uff0c\u5927\u5bb6\u53ef\u4ee5\u81ea\u5df1\u7684\u9700\u6c42\u8fdb\u884c\u6539\u9020\u548c\u62d3\u5c55<\/p>\n 1. \u767b\u5f55\u65f6<\/strong><\/div>\n \u767b\u5f55\uff0c\u524d\u7aef\u4f20\u6765\u7528\u6237\u540d\u548c\u5bc6\u7801\uff0c\u7136\u540e\u53bb\u540e\u7aef\u8fdb\u884c\u9a8c\u8bc1\u3002\u5982\u679c\u7528\u6237\u540d\u548c\u5bc6\u7801\u90fd\u6b63\u786e\uff0c\u5219\u6b63\u5e38\u767b\u5f55<\/p>\n \u6b64\u65f6\uff0c\u6211\u4eec\u9700\u8981\u628a\u7528\u6237\u540d\u3001\u5bc6\u7801\u5b58\u8fdb cookie \u4e2d\uff0c\u7136\u540e\u5728 session \u5b58\u50a8\u4e00\u4e2a\u662f\u5426\u5df2\u7ecf\u767b\u5f55\u7684\u6807\u8bc6<\/p>\n \u91cd\u8981\u7684\u8bdd\u8bf4\u4e09\u6b21\uff1a<\/p>\n \u5b58\u50a8\u5bc6\u7801\u65f6\uff0c\u8bb0\u5f97\u52a0\u5bc6\uff01<\/p>\n \u5b58\u50a8\u5bc6\u7801\u65f6\uff0c\u8bb0\u5f97\u52a0\u5bc6\uff01<\/p>\n \u5b58\u50a8\u5bc6\u7801\u65f6\uff0c\u8bb0\u5f97\u52a0\u5bc6\uff01<\/p>\n \u53c2\u8003\u4ee3\u7801\u5982\u4e0b<\/p>\n from flask import make_response, session, request\r\n@user.route('\/login', methods=['POST'])\r\ndef login():\r\n username = request.form.get('username').strip()\r\n password = request.form.get('password').strip()\r\n ...... \/\/ \u81ea\u5df1\u7684\u903b\u8f91\u5904\u7406\r\n if \u767b\u5f55\u6210\u529f:\r\n session['islogin'] = 'true' \/\/ \u662f\u5426\u5df2\u7ecf\u767b\u5f55\u7684\u6807\u8bc6\r\n session['username'] = username \/\/ \u6839\u636e\u81ea\u5df1\u7684\u9700\u6c42\uff0c\u5728session\u91cc\u5b58\u50a8\u4e00\u4e9b\u503c\r\n # \u5c06Cookie\u5199\u5165\u6d4f\u89c8\u5668\r\n response = make_response('login-pass') \/\/ \u767b\u5f55\u6210\u529f\uff0c\u8fd4\u56de\u7ed9\u524d\u7aef\u7684\u503c\r\n response.set_cookie('username', username, max_age=30*24*3600) \/\/ max_age\uff0ccookie\u7684\u5b58\u6d3b\u65f6\u95f4\uff0c\u8fd9\u91cc\u8868\u793a\u4e00\u4e2a\u6708\r\n response.set_cookie('password', password, max_age=30*24*3600)\r\n return response\r\n else:\r\n return 'login-fail' \/\/ \u767b\u5f55\u5931\u8d25\uff0c\u8fd4\u56de\u7ed9\u524d\u7aef\u7684\u503c<\/pre>\n |