{"id":265324,"date":"2023-03-31T10:55:33","date_gmt":"2023-03-31T02:55:33","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=265324"},"modified":"2023-02-25T20:56:18","modified_gmt":"2023-02-25T12:56:18","slug":"flask-linux-session","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/flask-linux-session.html","title":{"rendered":"\u7b80\u5355\u4ecb\u7ecdFlask\u6846\u67b6\u4e2d\u7684session\u8bbe\u7f6e"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\nFlask\u662f\u4e00\u4e2a\u4f7f\u7528Python\u7f16\u5199\u7684\u8f7b\u91cf\u7ea7Web\u5e94\u7528\u6846\u67b6\u3002\u5176WSGI\u5de5\u5177\u7bb1\u91c7\u7528Werkzeug\uff0c\u6a21\u677f\u5f15\u64ce\u5219\u4f7f\u7528 Jinja2 \u3002Flask\u4f7f\u7528BSD\u6388\u6743\u3002Flask\u4e5f\u88ab\u79f0\u4e3a \u201cmicroframework\u201d\uff0c\u56e0\u4e3a\u5b83\u4f7f\u7528\u7b80\u5355\u7684\u6838\u5fc3\uff0c\u7528extension\u589e\u52a0\u5176\u4ed6\u529f\u80fd<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
Flask\u4e2d\u7684session<\/strong><\/div>\n
session\u8bbe\u7f6e<\/strong><\/span><\/div>\n

Flask\u9664\u8bf7\u6c42\u5bf9\u8c61\u4e4b\u5916\uff0c\u8fd8\u6709\u4e00\u4e2a session \u5bf9\u8c61\u3002<\/p>\n

\u5b83\u5141\u8bb8\u4f60\u5728\u4e0d\u540c\u8bf7\u6c42\u95f4\u5b58\u50a8\u7279\u5b9a\u7528\u6237\u7684\u4fe1\u606f\u3002\u5b83\u662f\u5728 Cookies \u7684\u57fa\u7840\u4e0a\u5b9e\u73b0\u7684\uff0c\u5e76\u4e14\u5bf9 Cookies \u8fdb\u884c\u5bc6\u94a5\u7b7e\u540d\u8981\u4f7f\u7528\u4f1a\u8bdd\uff0c\u4f60\u9700\u8981\u8bbe\u7f6e\u4e00\u4e2a\u5bc6\u94a5\u3002<\/p>\n

\u6211\u4eec\u77e5\u9053\uff0c\u5728django\u4e2d\u7684session\u662f\u5b58\u50a8\u5728\u670d\u52a1\u5668\u4e2d\u7684\u6570\u636e\u5e93\u4e2d\u7684\uff0c\u4e5f\u5c31\u662fdjango_session\u8868\u4e2d\uff0c\u800cflask\u4e2d\u7684session\u662f\u4ea4\u7531\u5ba2\u6237\u7aef\u4fdd\u7ba1\u7684\uff0c\u4e5f\u5c31\u662f\u5b58\u50a8\u5728\u672c\u5730\u7684cookie\u4e2d\u3002<\/p>\n

session\u7684\u65b9\u6cd5\uff1a<\/p>\n

  • session['username'] \uff1d 'stzz'\uff1b\u8bbe\u7f6esession<\/li>\n
  • session.pop(\"username\",None)\uff1b\u5220\u9664session<\/li>\n
    \u5173\u4e8esession\u7684\u8bbe\u7f6e<\/strong><\/span><\/div>\n
    app.session_cookie_name = \"I am not Session\"<\/pre>\n

    \u5176\u4ed6\u914d\u7f6e<\/p>\n

    'SESSION_COOKIE_NAME':                  'session',  # \u8bbe\u7f6esession\u7684\u540d\u5b57\r\n'SESSION_COOKIE_DOMAIN':                None,\r\n'SESSION_COOKIE_PATH':                  None,\r\n'SESSION_COOKIE_HTTPONLY':              True,\r\n'SESSION_COOKIE_SECURE':                False,\r\n'SESSION_REFRESH_EACH_REQUEST':         True,  # \u662f\u5426\u6bcf\u6b21\u90fd\u8ddf\u65b0\r\n'PERMANENT_SESSION_LIFETIME':           timedelta(days=31)  \r\n# \u8bbe\u7f6esession\u7684\u8fc7\u671f\u65f6\u95f4<\/pre>\n
    cookies\u4e2d\u7684session<\/strong><\/div>\n

    cookies \u4e2d session \u5b58\u50a8\u7684\u662f\u901a\u8fc7 secret_key \u52a0\u5bc6\u540e\u7684 key , \u901a\u8fc7\u8fd9\u4e2a key \u4eceflask\u7a0b\u5e8f\u7684\u5185\u5b58\u4e2d\u627e\u5230\u7528\u6237\u5bf9\u5e94\u7684session\u4fe1\u606f<\/p>\n

    session\u5e8f\u5217\u5316\u673a\u5236<\/strong><\/span><\/div>\n

    \u5f53\u6211\u4eec\u5f00\u542fsession\u65f6\uff0c\u8bbe\u7f6esession[\"username\"]=\"stzz\",flask\u4f1a\u5e2e\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u5b57\u5178{\"username\":\"stzz\"}\uff0c\u7136\u540e\u901a\u8fc7secret_key + \u65f6\u95f4\u6233 + \u7b7e\u540d\u7ecf\u8fc7\u52a0\u5bc6\u751f\u6210\u4e00\u4e2a\u5b57\u7b26\u4e32\u3002<\/p>\n

    session\u53cd\u5e8f\u5217\u5316\u673a\u5236<\/strong><\/span><\/div>\n

    \u5f53\u5ba2\u6237\u7aef\u53d1\u9001\u8bf7\u6c42\u65f6\uff0crequest\u8bf7\u6c42\u4f1a\u5e26\u4e0acookie\uff0c\u4e5f\u5c31\u662fsession\u4e2d\u7684\u6570\u636e\u5b58\u50a8\u5728\u5176\u4e2d\uff0c\u8fd9\u4e2a\u6570\u636e\u5c31\u662f\u4e4b\u524d\u52a0\u5bc6\u540e\u7684\u5b57\u7b26\u4e32\uff0c\u53d1\u9001\u5230\u540e\u7aef\u540e\uff0cflask\u4f1a\u901a\u8fc7secret_key\u53bb\u89e3\u5bc6session\u4e2d\u7684\u52a0\u5bc6\u5b57\u7b26\u4e32\uff0c\u4ece\u800c\u83b7\u53d6{\"username\":\"stzz\"}\u4ece\u800c\u6765\u9a8c\u8bc1\u662f\u5426\u767b\u5f55\u3002<\/p>\n

    session\u7684\u4f7f\u7528\u548c\u9a8c\u8bc1<\/strong><\/div>\n
    from flask.blueprints import Blueprint\r\nfrom flask import Response, session\r\nfrom flask_restful import fields, marshal\r\nfrom flask_sqlalchemy.query import Query\r\nfrom sqlalchemy import or_, and_\r\nfrom ..model.UserModel import User\r\nimport json\r\nuser_bp = Blueprint(\"user\", __name__, url_prefix=\"\/user\")\r\nuser_fields = {\r\n    \"id\": fields.Integer,\r\n    \"username\": fields.String,\r\n    \"password\": fields.String,\r\n    \"age\": fields.Integer\r\n}\r\n@user_bp.route(\"\/hello\")\r\ndef hello():\r\n    result = User.query.filter(or_(User.username.startswith(\"\u738b\"), and_(User.username == \"\u738b\u5929\u7965\", User.age > 21)))\r\n    return marshal(result.all(), fields=user_fields)\r\n@user_bp.route(\"\/setSession\", methods=[\"GET\", \"POST\"])\r\ndef setSession():\r\n    session[\"name\"] = \"STZZ\"\r\n    return \"\u8bbe\u7f6esession\u6210\u529f\uff01\uff01\uff01\"\r\n@user_bp.route(\"\/getSession\")\r\ndef getSession():\r\n    username = session.get(\"name\")\r\n    if username:\r\n        return username\r\n    else:\r\n        return \"\u7528\u6237\u672a\u767b\u5f55!!!\"<\/pre>\n

    \u5230\u6b64\u8fd9\u7bc7\u5173\u4e8eFlask\u6846\u67b6\u4e2d\u7684session\u8bbe\u7f6e\u8be6\u89e3\u7684\u6587\u7ae0\u5c31\u4ecb\u7ecd\u5230\u8fd9\u4e86<\/p>\n","protected":false},"excerpt":{"rendered":"

    Flask\u9664\u8bf7\u6c42\u5bf9\u8c61\u4e4b\u5916\uff0c\u8fd8\u6709\u4e00\u4e2a session \u5bf9\u8c61\u3002 \u5b83\u5141\u8bb8\u4f60\u5728\u4e0d\u540c\u8bf7\u6c42\u95f4\u5b58\u50a8\u7279\u5b9a\u7528\u6237\u7684\u4fe1\u606f\u3002\u5b83\u662f\u5728 C […]<\/p>\n","protected":false},"author":668,"featured_media":153228,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-265324","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/265324","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/668"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=265324"}],"version-history":[{"count":4,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/265324\/revisions"}],"predecessor-version":[{"id":265429,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/265324\/revisions\/265429"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/153228"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=265324"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=265324"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=265324"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}