Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
238 views
in Technique[技术] by (71.8m points)

python 2.7 - Flask-Login - How to get Session ID

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() 
    
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...