Giter Club home page Giter Club logo

bottle-session's Introduction

Bottle Sessions with Redis

Bottle_session is a session manager for the Bottle microframework that uses a cookie to maintain your web session and stores a hash associated with that cookie using the redis key-value store. It is designed as a simple Bottle plugin.

Installation

Install using either pip or easy_install:

$ pip install bottle-session

or you can download the latest version from bitbucket:

$ git clone https://[email protected]/devries/bottle-session.git
$ cd bottle-session
$ python setup.py install

Requirements

In order to use bottle-session you must have both the redis and of course the bottle modules installed. I recommend also installing pycrypto, although it is not required. If pycrypto is installed, then the pycrypto random number generator is used to generate session cookies, otherwise python's internal random number generator is used.

Using Bottle-session

The first requirement is that you import the bottle_session module:

:::python
import bottle_session
import bottle

Next, initialize the plugin:

:::python
app = bottle.app()
plugin = bottle_session.SessionPlugin(cookie_lifetime=600)
app.install(plugin)

The cookie_lifetime parameter is the lifetime of the cookie in seconds, if the lifetime is explicitly set to None it will last 1 week. The SessionPlugin class initializer takes several optional parameters:

  • host is the host for the redis instance. It defaults to localhost.
  • port is the port for the redis instance. It defaults to 6379.
  • db is the redis database number. It defaults to 0.
  • cookie_name is the name of the session cookie. It defaults to bottle.session.
  • cookie_secure is a boolean variable to set the Secure cookie flag. It defaults to False.
  • cookie_httponly is a boolean variable to set the HttpOnly cookie flag. It defaults to False.
  • keyword is the plugin keyword. It defaults to session.
  • password is the optional password for the redis instance. It defaults to none.

To use the plugin, just add the keyword (session by default) to the routed method:

:::python
@bottle.route('/')
def index(session):
    user_name = session.get('name')
    if user_name is not None:
        return "Hello, %s"%user_name
    else:
        return "I don't recognize you."

@bottle.route('/set/:user_name')
def set_name(session,user_name=None):
    if user_name is not None:
        session['name']=user_name
        return "I recognize you now."
    else:
        return "What was that?"

bottle.debug(True)
bottle.run(app=app,host='localhost',port=8888)

In this example you can set the name property of the session cookie to Chris by visiting the http://localhost:8888/set/Chris and then that value is retrieved when you visit http://localhost:8888/.

Using Bottle-session and Bottle-redis

If you are using redis for sessions you are likely using redis to store other data as well, and likely use the bottle-redis plugin. You can use both plugins together, and you can even get them to use the same connection pool. Initialize them by creating a connection pool which you attach to each plugin object before installing them into the bottle application as shown below:

:::python
#!/usr/bin/env python
import bottle_session
import bottle_redis
import bottle
import redis
from datetime import datetime

app = bottle.app()
session_plugin = bottle_session.SessionPlugin()
redis_plugin = bottle_redis.RedisPlugin()

connection_pool = redis.ConnectionPool(host='localhost', port=6379)

session_plugin.connection_pool = connection_pool
redis_plugin.redisdb = connection_pool
app.install(session_plugin)
app.install(redis_plugin)

@bottle.route('/')
def index(session,rdb):
    rdb.incr('visitors')
    visitor = rdb.get('visitors')
    last_visit = session['visit']
    session['visit'] = datetime.now().isoformat()

    return 'You are visitor %s, your last visit was on %s'%(visitor,last_visit)

bottle.debug(True)
bottle.run(app=app,host='localhost',port=8888)

Acknowledgments

Thanks to Marcel Hellkamp and the bottle community for the framework and to Sean M. Collins whose bottle-redis package in bottle-extras served as the inspiration for this bottle plugin. Thank you to James Burke for your contributions.

bottle-session's People

Contributors

crazyhatfish avatar crimsonglory avatar devries avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

bottle-session's Issues

How do I set the time for each cookie?

An instance has been installed.
session['id'] = int(userInfo.id)
session['account'] = userInfo.account
session['accountID'] = userInfo.account_id
session['level'] = userInfo.level
session['lastLoginTime'] = userInfo.last_login_time

# Seven days to login
if autoLogin:
    # session.max_age = 7*24*60*60
    session.__init__()
    # session.SessionPlugin(cookie_lifetime=7*24*60*60)
    print session.max_age

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.