A flask with a web server terminates all sessions?

For example, this code:

from flask import session @app.route('/is_logged_in/') def is_logged_in(): return 'user' in session 

It works fine for me when starting the Flask development server, but it gives a 500 error with any web server setup (FastCGI, uWSGI, etc.) on any server (Apache, nginx, Lighttpd).

Does anyone know why?


My actual code is on GitHub, if that matters.

It works flawlessly when working with the internal Flask server, but I cannot get the session variables to work with the production web server: https://github.com/Blender3D/Webminal/blob/master/server.py

+4
source share
3 answers

I finally tried Tornado , thinking it would help with my problems (this is written in Python, after all).

Lo and behold, readable trace:

 RuntimeError: the session is unavailable because no secret key was set. Set the secret_key on the application to something unique and secret. 

Looks like I just forgot to add a secret key for signing up sessions with:

 app.secret_key = 'why would I tell you my secret key?' 
+14
source

Your return value must be one of several types: a basestring ( string or unicode ), a tuple (representing the arguments passed to the constructor of the Response object), a Response object itself, or - rejection of them - a function that calls WSGI.

You return the bool . Flask suggests that since it is not a basestring , tuple or Response object, it must be WSGI-invoked. Subsequently, when processing the response, it tries to call () your return value bool , which leads to an exception. The flag captures the received TypeError. When Flask is in debug mode, it passes this back to a simple Werkzeug web server, which will call the built-in debugger. However, when Flask is in production mode, it will simply cause an internal server error - for example, code 500 - without additional information.

So, to fix your problem, make sure you do this:

return str('user' in session)

+1
source

Although, in fact, this does not answer your question, I would rewrite it like this:

 from flask import redirect, session, url_for @app.route('/') def index(): return 'foo' @app.route('/is_logged_in/') def is_logged_in(): user = session.get('user', None) if user: return user return redirect(url_for('index')) 
0
source

Source: https://habr.com/ru/post/1392409/


All Articles