\u5173\u4e8esession\u7684\u8bbe\u7f6e<\/strong><\/span><\/div>\napp.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>\ncookies\u4e2d\u7684session<\/strong><\/div>\ncookies \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>\nfrom 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}]}}