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 ...
随机推荐
- VC++对象布局的奥秘:虚函数、多继承、虚拟继承
哈哈,从M$ Visual C++ Team的Andy Rich那里又偷学到一招:VC8的隐含编译项/d1reportSingleClassLayout和/d1reportAllClassLayout ...
- fedora21安装无线驱动
来源:http://www.2cto.com/os/201202/120249.html 一.导入rpmfushion源,使用第三方yum 源: su -c 'yum localinstall --n ...
- BeanUtils数据封装与表单JavaBean
一.BeanUtils工具的解释 (1)Apache的Commons组件中.提供了一个实用的工具类BeanUtils,利用它可以方便的将表单数据值填充值Bean中: (2)javax.servlet. ...
- FragmentPagerAdapter刷新fragment最完美解决方案
FragmentPagerAdapter刷新fragment最完美解决方案 先感谢kingjxl2006的博客文章<Android FragmentPagerAdapter数据刷新notif ...
- AJAX同步与异步
今天来大概说说AJAX中的同步与异步.其实,就我的理解,同步与异步的区别就是程序执行过程中是否有等待. 同步:意思就是js代码加载到当前的 AJAX时候,会等待AJAX代码执行完毕后再开始加载其他代码 ...
- WAMP多站点配置,更改服务器端口
修改apache.conf的配置文件 设置保存路径 原本的路径:DocumentRoot "D:/wamp/www/" 修改为自己定义的路径:D:\all_code\php 查询: ...
- 在Windows下github展示代码
最近大爱Web编程,于是寻找各种代码中,然后就发现了GitHub这个网站,如果你知道Google Code,那么你就知道这个GitHub是做什么的了.不过GitHub主要是用作基于Git的分布式版本管 ...
- 2014 HDU多校弟九场I题 不会DP也能水出来的简单DP题
听了ZWK大大的思路,就立马1A了 思路是这样的: 算最小GPA的时候,首先每个科目分配到69分(不足的话直接输出GPA 2),然后FOR循环下来使REMAIN POINT减少,每个科目的上限加到10 ...
- [置顶] 浏览器模式和标准对于javascript的影响
今天在编写代码的时候遇到了一个莫名其妙的问题,请看下面 <html> <head> <title> Test </title> <!--<m ...
- C# 窗体在线2,8,16进制转换以及,在线更新时间
class Program { static void Main(string[] args) { //十进制转二进制 Console.WriteLine(, )); //十进制转八进制 Consol ...