From 9ee479c98cd34edfd2a0b0749bfa10ebf3d1d446 Mon Sep 17 00:00:00 2001 From: zhushixia Date: Thu, 16 Aug 2018 20:39:08 +0800 Subject: [PATCH] =?UTF-8?q?session=E5=82=A8=E5=AD=98=E4=BD=8D=E7=BD=AE?= =?UTF-8?q?=E6=8C=87=E5=AE=9A=E5=92=8C=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/vcs.xml | 1 + manager.py | 14 ++++++++++++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/.idea/vcs.xml b/.idea/vcs.xml index 94a25f7..9333ed1 100644 --- a/.idea/vcs.xml +++ b/.idea/vcs.xml @@ -2,5 +2,6 @@ + \ No newline at end of file diff --git a/manager.py b/manager.py index 8542f12..205ac10 100644 --- a/manager.py +++ b/manager.py @@ -1,17 +1,25 @@ -from flask import Flask +import redis +from flask import Flask,session from flask.ext.sqlalchemy import SQLAlchemy from flask.ext.wtf import CSRFProtect from redis import StrictRedis - +from flask_session import Session#可以指定session保存到位置 class Config(object):#项目的配置 DEBUG = True + SECRET_KEY = "LzUA6O2R2R/mNSD6cbQ5Fzqkq+5VUa3B9/42IG9uoVSzRFbqf2Q6ZGUSVNa4JDstGSADO7hEsQhwk6papcOs4g==" #为数据库添加配置 SQLAlchemy_DATABASE_URI = "mysql://root:mysql@127.0.0.1:3306/information27" SQLALCHEMY_TRACK_MODIFICATIONS = False #Redis的配置 REDIS_HOST = "127.0.0.1" REDIS_PORT = 6379 + # flask_session的配置信息 + SESSION_TYPE = "redis" # 指定 session 保存到 redis 中 + SESSION_USE_SIGNER = True # 让 cookie 中的 session_id 被加密签名处理 + SESSION_REDIS = redis.StrictRedis(host=REDIS_HOST, port=REDIS_PORT) # 使用 redis 的实例 + PERMANENT_SESSION_LIFETIME = 86400 # session 的有效期,单位是秒 + SESSION_PERMANENT = False app = Flask(__name__) #加载配置 @@ -23,11 +31,13 @@ class Config(object):#项目的配置 redis_store = StrictRedis(host=Config.REDIS_HOST, port=Config.REDIS_PORT) #开启当前项目csrf保护,只做服务验证工作 CSRFProtect(app) +Session(app) @app.route("/") def index(): + session["name"] = "itheima" return "index112e111" if __name__ == "__main__":