烧瓶 - 如何获得会话ID [英] Flask - How to get Session ID

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

问题描述

使用flask开发服务器环境与Flask,Gevent和web套接字进行项目。我用 flask_login 。这里


  1. 我怎样才能得到每个连接的唯一会话ID?
  2. 将数据库中的 SessionID 存储在数据库中,并在客户端断开连接时删除它。

    $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $'$ $ $ $' b @ app.route(/,methods = [GET,POST])
    def login():
    login_user([username],记住):

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

    解决方案

    没有会话ID



    在Flask中的会话只是通过cookie进行包装。您保存的内容是数字签名并作为cookie发送给客户端。当你发出一个请求时,这个cookie被发送到你的服务器,然后在一个Python对象中进行验证和转换。

    AFAIK,Flask-Login在会话中保存用户ID 。
    $ b

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


    1. 登录时,生成一个唯一的ID并将其保存在会话中(例如 flask.session ['uid'] = uuid.uuid4()),然后将其保存到数据库中。
    2. 在注销时,从会话中删除该唯一标识( del flask.session ['uid'] ),并从数据库中删除。
    3. 使用您最喜欢的方法(ORM /原始SQL)检索活动会话的数量


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

    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() 
      

    解决方案

    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:

    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)

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

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