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 ...
随机推荐
- 解题报告 HDU1176 免费馅饼
免费馅饼 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- Battle ships(二分图,建图,好题)
Battle ships Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Tot ...
- Gradle 1.12 翻译——第十四章. 教程 - 杂七杂八
有关其它已翻译的章节请关注Github上的项目:https://github.com/msdx/gradledoc/tree/1.12,或訪问:http://gradledoc.qiniudn.com ...
- [Unity 3D] Unity 3D 性能优化(二)
IsAlive U3D的粒子系统脚本接口相信很多人都用过,ParticleSyetem类的一系列接口都有一个bool类型的参数——withChildren,通过这个参数可以直接将相同的判断或者操作应用 ...
- ftp上传错误
明明设置好了权限,但是在上传的时候提示如下错误,但在使用的过程当中,发现有的时候是可以上传的,很奇怪的问题. baidu 了一下,发现是下面的这个设置导致的.改过来后,果然正常. 这个设置只是一个字符 ...
- 【第一篇:C++与opencv】图片的读取和显示
这里介绍C++版本的opencv,和C语言版本有些不同,先看代码^_^ [编译环境:opencv2.4.4和VS2008] #include "stdafx.h" #include ...
- 实战nginx 基础知识总结(一)1.1
squid Squid是一个缓存Internet数据的软件,其接收用户的下载申请,并自动处理所下载的数据.当一个用户想要下载一个主页时,可以向Squid发出一个申请,要Squid代替其进行下载,然后S ...
- Codeforces 489A SwapSort
这题第一次看的时候以为是区间替换,后来发现看错了,只是单纯的元素替换. 解题思路: 先对输入的序列加个数组排个序 遍历下来,如果和排序后的结果当前元素不同,设当前位置为 i, 则往下面找,设查找位置为 ...
- java.util.zip.Deflater 压缩 inflater解压 实例
原文:java压缩解压缩类实例[转] package com.example.helloworld; import java.io.ByteArrayOutputStream; import java ...
- 基于visual Studio2013解决算法导论之020单链表
题目 单链表操作 解决代码及点评 #include <iostream> using namespace std; struct LinkNode { public: LinkNo ...