Flask会话到期时调用函数 [英] Call a function when Flask session expires
本文介绍了Flask会话到期时调用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的Flask应用程序中,我正在保存与用户相对应的文件,并希望在用户的会话"过期时删除这些文件.是否可以检测到会话到期并立即调用函数?
In my Flask application, I am saving files that correspond to a user, and want to delete these files when the user's "session" expires. Is it possible to detect the session expiration and immediately call a function?
推荐答案
遇到了相同的问题,并没有通过使用内置的永久会话到期功能来解决它,而是在会话中添加了我自己的密钥并在每次请求之前对其进行了检查如下所示:
Had the same issue and solved it not by using the in-built permanent session expiry feature, but added my own key to the session and checking it before every request like follows:
@app.before_request
def before_request()
now = datetime.datetime.now()
try:
last_active = session['last_active']
delta = now - last_active
if delta.seconds > 1800:
session['last_active'] = now
return logout('Your session has expired after 30 minutes, you have been logged out')
except:
pass
try:
session['last_active'] = now
except:
pass
这篇关于Flask会话到期时调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文