我们可以在Python Flask中跟踪当前活动的会话吗 [英] Can we track currently active sessions in Python Flask

查看:65
本文介绍了我们可以在Python Flask中跟踪当前活动的会话吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Flask Web应用程序中,只要有新用户登录,就会创建一个新的 session .在服务器端,有没有办法跟踪当前的活动会话?

In my Flask Web-app, a new session is created whenever a new user logs in. At the server-end, is there a way to track the currently active sessions?

推荐答案

如果您使用Flask-Login进行用户会话管理,则Flask-login的 is_authenticated 属性会告诉您是否已登录用户在或不在:

If you are using Flask-Login for your user session management then is_authenticated property of Flask-login tells you if the user is logged in or not:

if not current_user.is_authenticated:
    return current_app.login_manager.unauthorized()

如果要保护视图,可以使用 @login_required 装饰器.默认情况下,当用户尝试不登录而访问login_required视图时,Flask-Login将闪烁一条消息并将其重定向到登录视图.(如果未设置登录视图,它将终止并显示401错误.)

If you want to protect your views you can use @login_required decorator. By default, when a user attempts to access a login_required view without being logged in, Flask-Login will flash a message and redirect them to the login view. (If the login view is not set, it will abort with a 401 error.)

@app.route("/settings")
@login_required
def settings():
    pass

请参见文档

这篇关于我们可以在Python Flask中跟踪当前活动的会话吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆