Flask redis server session: Error in save_session
Asked Answered
E

2

5

Folks: I am moving my Flask app with redis for session storage into docker containers. My app runs happily when redis is run locally. However, when running a docker-compose with a redis image, I see the following error. I get an identical error whether my redis container is running or stopped:

2017-04-02 03:36:09,861] ERROR in app: Exception on / [GET]
web_1    | Traceback (most recent call last):
web_1    |   File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1988, in wsgi_app
web_1    |     response = self.full_dispatch_request()
web_1    |   File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1643, in full_dispatch_request
web_1    |     response = self.process_response(response)
web_1    |   File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1864, in process_response
web_1    |     self.save_session(ctx.session, response)
web_1    |   File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 926, in save_session
web_1    |     return self.session_interface.save_session(self, session, response)
web_1    |   File "/usr/local/lib/python2.7/site-packages/flask_session/sessions.py", line 165, in save_session
web_1    |     self.redis.setex(name=self.key_prefix + session.sid, value=val,
web_1    | AttributeError: 'str' object has no attribute 'setex'

I have added the following configuration properties to my app for session config:

SESSION_TYPE = "redis"
SESSION_REDIS = "redis:6379"

I don't think this is a docker-compose issue as my app is successfully able to connect to the MySQL instance, still, here is my docker-compose.yml file just in case it points to something:

version: "3"
services:
  mysql:
    build:
      context: db/
  web:
    build:
      context: web/
    ports:
    - "10080:80"
    depends_on:
    - "mysql"
    - "redis"
  redis:
    image: "redis:alpine"
Erivan answered 2/4, 2017 at 4:0 Comment(0)
E
8

Figured it out. The issue was in session config. I had to change

SESSION_REDIS = "redis:6379"

to

SESSION_REDIS = redis.Redis("redis")
Erivan answered 2/4, 2017 at 5:4 Comment(3)
Also Keep habit of Accepting your answer instead keep it open for Stackoverflow.Nephogram
Hi @LuFFy, just to confirm, the recommended practice is to accept my answer and not keep it open? It looks like I have to wait 8 more hours before accepting my own answer, so I'll try that at the time.Erivan
I had this problem now and you need to explicitly inform the default host and server in order to solve the 'problem' - 'SESSION_REDIS': redis.Redis(host='localhost', port=6379),.Genteelism
O
0

This Worked for me:

from flask import Flask, session
from flask_session import Session
import redis

app = Flask(__name__)

# Load the config file
app.config.from_pyfile('config.cfg')

# Create and initialize the Flask-Session object 
app.config['SESSION_REDIS'] = redis.from_url(app.config['REDIS_URL'])
server_session = Session(app)

config.cfg

REDIS_URL = 'redis://:p1ba23....'
Oversew answered 26/7, 2021 at 22:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.