{"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":"\n\n\n
\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
2. \u5b9a\u4e49\u5168\u5c40\u62e6\u622a\u5668<\/strong><\/div>\n

\u8fd9\u4e2a\u65f6\u5019\uff0c\u53ef\u80fd\u4f1a\u6709\u4eba\u6709\u7591\u95ee\uff0c\u4e3a\u4ec0\u4e48\u8981\u4f7f\u7528\u62e6\u622a\u5668\uff1f<\/p>\n

\u8bd5\u60f3\uff1a\u8fdb\u5165\u4e00\u4e2a\u7f51\u7ad9\u6709\u591a\u4e2a\u5165\u53e3\uff0c\u4f60\u77e5\u9053\u7528\u6237\u4f1a\u4ee5\u54ea\u4e2a\u5165\u53e3\u6765\u8bbf\u95ee\u4f60\u7684\u7f51\u7ad9\u5417\uff1f\u90a3\u6211\u4eec\u603b\u4e0d\u80fd\u6bcf\u4e00\u4e2a\u8fdb\u5165\u7f51\u7ad9\u7684\u63a5\u53e3\u90fd\u53bb\u5199\u4e00\u4efd\u81ea\u52a8\u767b\u5f55\u7684\u903b\u8f91\u4ee3\u7801\u5427\u3002\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u62e6\u622a\u5668\uff0c\u5c06\u6240\u6709\u7684\u8bf7\u6c42\u90fd\u62e6\u622a\u8d77\u6765\uff0c\u8fdb\u884c\u7edf\u4e00\u7684\u5904\u7406\u3002\u7136\u540e\u5c06\u9759\u6001\u7684\u8d44\u6e90\u548c\u4e0d\u7528\u8fdb\u884c\u81ea\u52a8\u767b\u5f55\u903b\u8f91\u5904\u7406\u7684\u8bf7\u6c42\u8fc7\u6ee4\u6389\u5373\u53ef<\/p>\n

\u5728\u7a0b\u5e8f\u7684\u4e3b\u5165\u53e3 main.py \u5b9a\u4e49\u4e00\u4e2a\u62e6\u622a\u5668<\/p>\n

# \u5b9a\u4e49\u5168\u5c40\u62e6\u622a\u5668\uff0c\u5b9e\u73b0\u81ea\u52a8\u767b\u5f55\r\n@app.before_request\r\ndef before():\r\n    url = request.path\r\n    pass_list = ['\/user', '\/login', 'logout']  \/\/  \u4e0d\u7528\u8fdb\u884c\u81ea\u52a8\u767b\u5f55\u903b\u8f91\u5224\u65ad\u7684\u8bf7\u6c42\r\n    if url in pass_list or url.endswith('.js') or url.endswith('.jpg'):\r\n        pass  \/\/  \u8fc7\u6ee4\u4e0d\u7528\u8fdb\u884c\u81ea\u52a8\u767b\u5f55\u903b\u8f91\u5904\u7406\u7684\u8bf7\u6c42\u548c\u9759\u6001\u7684\u8d44\u6e90\r\n    else:\r\n        if session.get('islogin') is None:\r\n            username = request.cookies.get('username')\r\n            password = request.cookies.get('password')\r\n            if username != None and password != None:\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<\/pre>\n
3. \u9000\u51fa\u767b\u5f55\u65f6<\/strong><\/div>\n

\u9000\u51fa\u767b\u5f55\u65f6\uff0c\u6e05\u7a7a session \u548c cookie<\/p>\n

from flask import make_response, session, request, url_for\r\n@user.route('\/logout')\r\ndef logout():\r\n    # \u6e05\u7a7asession\u548cCookie\uff0c\u9875\u9762\u8df3\u8f6c\r\n    session.clear()\r\n    response = make_response('\u6ce8\u9500\u5e76\u8fdb\u884c\u91cd\u5b9a\u5411', 302)  # 302\u72b6\u6001\u7801\u8868\u793a\u91cd\u5b9a\u5411\r\n    # \u4e24\u79cd\u91cd\u5b9a\u5411\u7684\u65b9\u6cd5\r\n    # response.headers['Location'] = '\/'\r\n    response.headers['Location'] = url_for('index.home')  # \u53e6\u4e00\u79cd\u65b9\u5f0f\uff0c\u62ec\u53f7\u91cc\u7684\u503c\u5199\u8bf7\u6c42\u4e3a\u201c\/\u201d \u7684 - Blueprint\u540d.\u65b9\u6cd5\u540d\r\n    # \u4e24\u79cd\u6e05\u9664cookie\u7684\u65b9\u5f0f\r\n    response.delete_cookie('username')\r\n    response.set_cookie('password', '', max_age=0)\r\n    return response<\/pre>\n

\u4e0a\u9762\u7b2c\u4e8c\u79cd\u91cd\u5b9a\u5411\u65b9\u5f0f\u4e2d\uff0c\u6240\u5bf9\u5e94\u7684\u503c<\/p>\n

index = Blueprint(\"index\", __name__)\r\n@index.route('\/')\r\ndef home():\r\n    ......  \/\/  \u81ea\u5df1\u7684\u903b\u8f91\u5904\u7406<\/pre>\n

\u5230\u6b64\u8fd9\u7bc7\u5173\u4e8ePython Flask\u524d\u7aef\u81ea\u52a8\u767b\u5f55\u529f\u80fd\u5b9e\u73b0\u8be6\u89e3\u7684\u6587\u7ae0\u5c31\u4ecb\u7ecd\u5230\u8fd9\u4e86<\/p>\n","protected":false},"excerpt":{"rendered":"

\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 \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 […]<\/p>\n","protected":false},"author":362,"featured_media":231815,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-256106","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/256106","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/users\/362"}],"replies":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/comments?post=256106"}],"version-history":[{"count":3,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/256106\/revisions"}],"predecessor-version":[{"id":256109,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/256106\/revisions\/256109"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media\/231815"}],"wp:attachment":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media?parent=256106"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/categories?post=256106"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/tags?post=256106"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}