Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Internal Server Error when using Flask session

I want to save an ID between requests, using Flask session cookie, but I'm getting an Internal Server Error as result, when I perform a request.

I prototyped a simple Flask app for demonstrating my problem:

#!/usr/bin/env python  from flask import Flask, session  app = Flask(__name__)  @app.route('/') def run():     session['tmp'] = 43     return '43'  if __name__ == '__main__':     app.run() 

Why I can't store the session cookie with the following value when I perform the request?

like image 452
sedavidw Avatar asked Aug 09 '13 04:08

sedavidw


People also ask

How do I fix an internal server error on a Flask?

Step 1 — Using The Flask Debugger. In this step, you'll create an application that has a few errors and run it without debug mode to see how the application responds. Then you'll run it with debug mode on and use the debugger to troubleshoot application errors.

Is Flask session server side?

Flask-Session is an extension for Flask that supports Server-side Session to your application. The Session is the time between the client logs in to the server and logs out of the server. The data that is required to be saved in the Session is stored in a temporary directory on the server.

How do I fix error 404 in Flask?

For this, we need to download and import flask. Download the flask through the following commands on CMD. Using app.py as our Python file to manage templates, 404. html be the file we will return in the case of a 404 error and header.


1 Answers

According to Flask sessions documentation:

... What this means is that the user could look at the contents of your cookie but not modify it, unless they know the secret key used for signing.

In order to use sessions you have to set a secret key.

Set secret key. And you should return string, not int.

#!/usr/bin/env python  from flask import Flask, session  app = Flask(__name__)  @app.route('/') def run():     session['tmp'] = 43     return '43'  if __name__ == '__main__':     app.secret_key = 'A0Zr98j/3yX R~XHH!jmN]LWX/,?RT'     app.run() 
like image 78
falsetru Avatar answered Sep 29 '22 02:09

falsetru