Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask-Session extension vs default session

Tags:

python

flask

I'm using:

from flask import session  @app.route('/') def main_page():     if session.get('key'):         print ("session exist" + session.get('key'))     else:         print ("could not find session")         session['key'] = '34544646###########'     return render_template('index.html') 

I don't have the Flask-Session extension installed but this still works fine. I'm trying to understand why and when is that extension imp to me. As far as I see, the default session works well for me.

like image 789
Ankit Avatar asked Aug 19 '15 00:08

Ankit


People also ask

Is Flask session client-side or server-side?

Flask's sessions are client-side sessions. Any data that you write to the session is written to a cookie and sent to the client to store.

What is session type in Flask?

Flask – Sessions Session is the time interval when a client logs into a server and logs out of it. The data, which is needed to be held across this session, is stored in the client browser. A session with each client is assigned a Session ID.

What is session permanent?

session. permanent by default is actually session['_permanent'] . Its value will stay in session . But if you are going to assign it only when users sign in, keep alert by checking how users can by-pass the sign-in route to sign in. For example, by signing up.

How are sessions managed in Flask?

In order to store data across multiple requests, Flask utilizes cryptographically-signed cookies (stored on the web browser) to store the data for a session. This cookie is sent with each request to the Flask app on the server-side where it's decoded.


1 Answers

The difference is in where the session data is stored.

Flask's sessions are client-side sessions. Any data that you write to the session is written to a cookie and sent to the client to store. The client will send the cookie back to the server with every request, that is how the data that you write in the session remains available in subsequent requests. The data stored in the cookie is cryptographically signed to prevent any tampering. The SECRET_KEY setting from your configuration is used to generate the signature, so the data in your client-side sessions is secure as long as your secret key is kept private. Note that secure in this context means that the data in the session cannot be modified by a potential attacker. The data is still visible to anybody who knows how to look, so you should never write sensitive information in a client-side session.

Flask-Session and Flask-KVSession are two extensions for Flask that implement server-side sessions. These sessions work exactly in the same way as the Flask native sessions from the point of view of your application, but they store the data in the server. The data is never sent to the client, so there is a bit of increased security. The client still receives a signed cookie, but the only data in the cookie is a session ID that references the file or database index in the server where the data is stored.

like image 104
Miguel Avatar answered Sep 17 '22 04:09

Miguel