烧瓶会话Cookie到期

时间:2020-06-27 19:59:30

标签: python python-3.x flask session-cookies

我正在尝试将Cookie的过期时间设置为比浏览器会话更长。我的config.py是:

from datetime import timedelta
SESSION_FILE_DIR = 'C:/some/path15'
SECRET_KEY= 'abcdefg'
DEBUG = True
SESSION_PERMANENT = True
PERMANENT_SESSION_LIFETIME = timedelta(days=30)

然后,为了模仿此示例的应用程序结构,我有一个注册了蓝图的主应用程序:

from flask import Flask, render_template, request, session, current_app
from flask_session import Session
import tempfile
 
server = Flask(__name__)
server.config.from_pyfile('config.py')

### Import and Register Blueprints 
from tools.routes import my_bp

server.register_blueprint(my_bp)

@server.route('/')
def homepage():
    return "Hello"
   
if __name__ == '__main__':
    server.run(debug=True)

然后一个名为routes.py的蓝图位于主应用程序的一个名为tools的子目录中

from flask import Flask, render_template, request, session, Blueprint, current_app
from flask_session import Session
import tempfile

my_bp = Blueprint("my_bp", __name__)

@my_bp.route('/new', methods=['POST', 'GET'])
def path(): 
    if 'path' not in session: ##new
        session['path'] = tempfile.mkdtemp() ##new
    path = session['path'] ##new
    return path

运行此应用程序时(通过/ new路由),如果我在浏览器中的“存储”下检查Element,则表明Cookie过期/ max_age为Session

我该如何尊重config.py文件中设置的30天有效期?

1 个答案:

答案 0 :(得分:0)

from flask import session, app
@app.before_request
def before_request():
    session.permanent = True
    app.permanent_session_lifetime = datetime.timedelta(minutes=20) # session will be alive for 20 minutes