bottle-session 0.2 : Python Package Index
bottle-session 0.2 : Python Package Index
bottle-session 0.2
Redis based sessions for bottle.
Latest Version: 0.3
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://devries@bitbucket.org/devries/bottle-session.git
$ cd bottle-session
$ python setup.py install Requirements
------------
In order to use bottle-session you must have the both the redis and of course
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 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`.
- `keyword` is the plugin keyword. It defaults to `session`. 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.
bottle-session 0.2 : Python Package Index的更多相关文章
- django-cookieless 0.7 : Python Package Index
django-cookieless 0.7 : Python Package Index django-cookieless 0.7 Download django-cookieless-0.7.ta ...
- Ghost.py 0.1b3 : Python Package Index
Ghost.py 0.1b3 : Python Package Index Ghost.py 0.1b3 Download Ghost.py-0.1b3.tar.gz Webkit based web ...
- pyrailgun 0.24 : Python Package Index
pyrailgun 0.24 : Python Package Index pyrailgun 0.24 Download pyrailgun-0.24.zip Fast Crawler For Py ...
- qrcode 4.0.4 : Python Package Index
qrcode 4.0.4 : Python Package Index qrcode 4.0.4 Download qrcode-4.0.4.tar.gz QR Code image generato ...
- bottle-session 0.3 : Python Package Index
bottle-session 0.3 : Python Package Index bottle-session 0.3
- graphterm 0.40.1 : Python Package Index
graphterm 0.40.1 : Python Package Index graphterm 0.40.1 Downloads ↓ A Graphical Terminal Interface ...
- Beaker 1.6.4 : Python Package Index
Beaker 1.6.4 : Python Package Index Beaker 1.6.4 Download Beaker-1.6.4.tar.gz A Session and Caching ...
- Install OpenCV 3.0 and Python 2.7+ on OSX
http://www.pyimagesearch.com/2015/06/15/install-OpenCV-3-0-and-Python-2-7-on-osx/ As I mentioned las ...
- Install OpenCV 3.0 and Python 2.7+ on Ubuntu
为了防止原文消失或者被墙,转载留个底,最好还是去看原贴,因为随着版本变化,原贴是有人维护升级的 http://www.pyimagesearch.com/2015/06/22/install-Open ...
随机推荐
- 英文:known good assembly(KGA) / 中文:确认好的组装件,已知好组装件
英文:known good assembly(KGA) / 中文:确认好的组装件,已知好组装件 正确地操作印制板装配,并可作为标准件与其它同类型装配件比较的组装.也称黄金组装.
- cocos2dx进阶学习之CCLayer
继承关系 CCLayer -> CCNode,CCTouchDelegate,CCAccelerometerDelegate,CCKeypadDelegate CCLayer在cocos2dx中 ...
- poj 3176 Cow Bowling(区间dp)
题目链接:http://poj.org/problem?id=3176 思路分析:基本的DP题目:将每个节点视为一个状态,记为B[i][j], 状态转移方程为 B[i][j] = A[i][j] + ...
- 移动端rem,scale动态设置
pt:物理像素(电容屏上像素块个数) px:逻辑像素.设备独立像素 高清屏:1px = 4pt 普通屏:1px = 1pt dpr:设备像素比:(某一方向上)物理像素/逻辑像素 通常设置1rem=屏幕 ...
- Jquery - checked 全选与取消全选
html: <!DOCTYPE html> <html> <head> <title>节点列表</title> <link rel=& ...
- dpkg卸载和安装deb
今天在linux mint上安装个东西,没有安装完全,但是启动的时候能够启动,为了防止以后出现异常,想把它卸载了,在软件上点卸载,没有反应, 如下图: 没有指定卸载的包源,无奈使用sudo apt-g ...
- CMarkUp接口说明
CMarkup是一个小型XML的分析器,实现语言是C++,英文版的接口说明地址为:http://www.firstobject.com/dn_markupmethods.htm 有厉害的网友已经翻译出 ...
- [Swust 549]--变位词(vector水过)
Time limit(ms): 1000 Memory limit(kb): 65535 Description 输入N和一个要查找的字符串,以下有N个字符串,我们需要找出其中的所有待查找字符串的 ...
- android术语笔记
参考:http://blog.csdn.net/luoshengyang/article/details/6618363 http://blog.csdn.net/singwhatiwanna/art ...
- php下正则表达式整理
一.正则表达式的历史背景 1,内容深厚的正则表达式 ^.+@.+\\..+$ 形式 字符串搜索与匹配的工具 2,应用范围 手机输入法 Windows文件搜索 linux 列出文件命令 网站用户注册,如 ...