Flask-Login-如何获取会话ID [英] Flask-Login - How to get Session ID

查看:529
本文介绍了Flask-Login-如何获取会话ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Flask开发服务器环境使用Flask,Gevent和Web套接字进行项目.我使用了flask_login.在这里

Am doing a project with Flask, Gevent and web socket using flask development server environment. I used flask_login. Here

  1. 如何获取每个连接的唯一会话ID?
  2. 我想将SessionID存储在数据库中,并在客户端断开连接后将其删除.
  3. 如何获得活动连接总数

  1. how can get i get the Unique Session ID for each connection?
  2. I want to store the SessionID in the Database and delete it once client disconnects.
  3. How to get total active connections

from flask_login import * 
login_manager = LoginManager()
login_manager.setup_app(app)

@app.route("/", methods=["GET", "POST"]) 
def login():
    login_user([username], remember):    

@app.route("/logout") 
@login_required 
def logout(): 
    logout_user() 

推荐答案

没有会话ID.

Flask中的会话只是cookie的包装.您保存在上面的内容经过数字签名,并以cookie的形式发送给客户端.当您发出请求时,该cookie将发送到您的服务器,然后在Python对象中进行验证和转换.

Sessions in Flask are simply wrappers over cookies. What you save on it it's digitally signed and sent as a cookie to the client. When you make a request, that cookie is sent to your server and then verified and transformed in a Python object.

AFAIK,Flask登录将会话ID保存在会话中.

AFAIK, Flask-Login saves on the session the user ID.

要获得总的活动连接,您可以:

To get total active connections, you can:

  1. 登录时,生成一个唯一的ID并将其保存在会话中(例如,flask.session['uid'] = uuid.uuid4()),然后将其保存在数据库中.
  2. 注销时,从会话(del flask.session['uid'])以及您的数据库中删除该唯一ID.
  3. 使用您喜欢的方法(ORM/Raw SQL)检索活动会话的数量
  1. At login, generate an unique id and save it on the session (flask.session['uid'] = uuid.uuid4(), for example), then save it on your database.
  2. At logout, delete that unique id from the session (del flask.session['uid']) and also from your database.
  3. Retrieve the count of active sessions using your favourite method (ORM/Raw SQL)

这篇关于Flask-Login-如何获取会话ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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