Skip to main content

Simple session implementation for python3 web frameworks

Project description


Web session implementation with redis store

PYPI Version Build Status


Implementated for python3 web frameworks to handle client sessions(Tornado and Flask are supported, other web frameworks to be supported later), using redis hashes to save session data.

  • For controlling expiration of sessions, we are using redis key expiration, and we only control session expiration at server/database side, while using default expiration time of secure cookies for session ids.

  • Reading session data in a request will cause a session expiration reset per request. Modify session data will cause a reset per modification.

  • Please specify cookie_secret for we are using secure cookie keys.

  • Getting session attributes is achived by directly fetching from redis to avoid stale data being read. So, when it's not necessary, copy the data instead of reading again, which means:

# Do this
name =
names.append(name) = make_new_name(name)
# Instead of (When we are not worried about conflicts with other processes/threads)
names.append( = make_new_name(  # This will read name from redis again

Setup & Install

Via pip

pip install redis_session

From source

python build && python install

Session data parsing

We provide common redis result parsing methods. For example:

# Get raw bytes         # b'stefan'

# Parsing to types         # Fallback to ''        # Fallback to 0         # Fallback to False  # Fallback to {}

# Check if is none


  • We are using tornado.options module, so please run below once at start.
# or

Command line parameters

# For tornado users
define('session-redis', default='redis://localhost:6379', help='session store redis url', type=str)
define('session-redis-prefix', help='redis key prefix', type=str)
define('session-expire', help='session ttl(seconds)', type=int)
define('session-cookie-id', help='cookie key, default: session-id', type=str)
define('session-http-only', default=True, help='if set session cookie as http only', type=bool)


# With tornado framework

import tornado.web
import tornado.httpserver
import tornado.ioloop
from tornado.options import options, define, parse_command_line
from redis_session.tornado_session import SessionHandler

define('port', default=3000, help='run on the given port', type=int)
define('debug', default=False, help='run in debug mode')

class MainHandler(SessionHandler):
    def get(self):
        count =
        self.write(f'Current session value of count:{count}\n')
        self.session.count = count + 1
        self.write(f'Current session value of count:{}\n')

def main():
    application = tornado.web.Application([(r'/', MainHandler)], cookie_secret='udxas-efasx-ase323fs-3efsxf3eFdes')
    http_server = tornado.httpserver.HTTPServer(application)

if __name__ == '__main__':


Please not httpOnly for flask session cookie is not well implementated in this project yet.


from redis_session.flask_session import attach_session
from flask import Flask, request

app = Flask(__name__)

def handle():
    count =
    res = f'Current session value of count:{count}\n'
    request.session.count = count + 1
    res += f'Current session value:{}\n'
    return res

def main():
        SESSION_HTTP_ONLY = True,

if __name__ == "__main__":

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for redis-session, version 0.1.0
Filename, size File type Python version Upload date Hashes
Filename, size redis_session-0.1.0-py3-none-any.whl (7.1 kB) File type Wheel Python version py3 Upload date Hashes View hashes
Filename, size redis_session-0.1.0.tar.gz (5.9 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page