gpt4 book ai didi

python - 如何防止更新特定 URL 的 session 过期时间

转载 作者:太空狗 更新时间:2023-10-30 02:53:51 25 4
gpt4 key购买 nike

flask 版本:

>>> flask.__version__
'0.12.2'

我不想更新某个 URL 的过期时间。
目前,我使用以下方法设置 session 过期时间:

session.permanent = true
app.permanent_session_lifetime = timedelta(minutes=int(10))

我有一个状态页面,它使用 ajax 方法每分钟自动刷新一次以仅获取数据。
在这种情况下,我不想重置 session 过期时间。
有什么办法吗?

编辑:

到目前为止我的代码:

from datetime import timedelta
from flask import Flask
from flask import session

APP = Flask(__name__, static_url_path='', static_folder='templates')

def create_user_session(user_accountid, auth_token):
session['token'] = auth_token
session['user_accountid'] = user_accountid

def update_session():
session.permanent = True
APP.permanent_session_lifetime = timedelta(minutes=10)
session.modified = True

@APP.route('/batch_status', methods=['GET'])
def view_status():
create_user_session(1, "****")
update_session()
return "Session Change"

@APP.route('/batch_status_json', methods=['GET'])
def retrieve_status():
return "Session No Change"

if __name__ == '__main__':
APP.secret_key = "My Precious Key"
APP.run(host='0.0.0.0', debug=True, port=8000)

我不想更改 batch_status_json 调用的 session 过期时间,但它会在调用时自动更改。

编辑2:

monitor_controller.py:

from datetime import timedelta
from flask import Flask
from flask import session
from status import RETRIEVE_STATUS

APP = Flask(__name__, static_url_path='', static_folder='templates')

APP.register_blueprint(RETRIEVE_STATUS)
def create_user_session(user_accountid, auth_token):
session['token'] = auth_token
session['user_accountid'] = user_accountid

def update_session():
session.permanent = True
APP.permanent_session_lifetime = timedelta(minutes=10)
session.modified = True

@APP.route('/batch_status', methods=['GET'])
def view_status():
create_user_session(1, "****")
update_session()
return "Session Change"

if __name__ == '__main__':
APP.secret_key = "My Precious Key"
APP.run(host='0.0.0.0', debug=True, port=8000)

status.py:

from flask import Blueprint

RETRIEVE_STATUS = Blueprint('retrieve_status', __name__)
@RETRIEVE_STATUS.route('/batch_status_json', methods=['GET'])
def retrieve_status():
return "Session No Change"

最佳答案

如果 session 被标记为已修改或如果设置了 SESSION_REFRESH_EACH_REQUEST 选项,则设置 cookie。默认情况下,该选项为 True。

在你的 Flask 配置中尝试设置

SESSION_REFRESH_EACH_REQUEST = False

来自文档:

SESSION_REFRESH_EACH_REQUEST

Control whether the cookie is sent with every response when session.permanent is true. Sending the cookie every time (the default) can more reliably keep the session from expiring, but uses more bandwidth. Non-permanent sessions are not affected.

Default: True

关于python - 如何防止更新特定 URL 的 session 过期时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48065865/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com