烧瓶 - 如何获得会话ID [英] Flask - How to get Session ID
问题描述
flask_login
。这里 - 我怎样才能得到每个连接的唯一会话ID?
- 将数据库中的
SessionID
存储在数据库中,并在客户端断开连接时删除它。
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $'$ $ $ $' b @ app.route(/,methods = [GET,POST])
def login():
login_user([username],记住):
@ app.route(/ logout)
@login_required
def logout():
logout_user()
$ p $
解决方案没有会话ID
在Flask中的会话只是通过cookie进行包装。您保存的内容是数字签名并作为cookie发送给客户端。当你发出一个请求时,这个cookie被发送到你的服务器,然后在一个Python对象中进行验证和转换。
AFAIK,Flask-Login在会话中保存用户ID 。
$ b要获得总的活动连接,您可以:
- 登录时,生成一个唯一的ID并将其保存在会话中(例如
flask.session ['uid'] = uuid.uuid4()
),然后将其保存到数据库中。
- 在注销时,从会话中删除该唯一标识(
del flask.session ['uid']
),并从数据库中删除。
- 使用您最喜欢的方法(ORM /原始SQL)检索活动会话的数量
Am doing a project with Flask, Gevent and web socket using flask development server environment. I used
flask_login
. Here- how can get i get the Unique Session ID for each connection?
- I want to store the
SessionID
in the Database and delete it once client disconnects. 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()
解决方案There is no session id.
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-Login saves on the session the user ID.
To get total active connections, you can:
- 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. - At logout, delete that unique id from the session (
del flask.session['uid']
) and also from your database. - Retrieve the count of active sessions using your favourite method (ORM/Raw SQL)
这篇关于烧瓶 - 如何获得会话ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- 登录时,生成一个唯一的ID并将其保存在会话中(例如