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 ...
随机推荐
- vs使代码可以折叠的方法
set [工具]->[选项]->[文本编辑器]->[C/C++]->[查看]->[大纲显示]->[大纲语句块] = True
- BZOJ 1613: [Usaco2007 Jan]Running贝茜的晨练计划
题目 1613: [Usaco2007 Jan]Running贝茜的晨练计划 Time Limit: 5 Sec Memory Limit: 64 MB Description 奶牛们打算通过锻炼来 ...
- poj 1084 Brainman(归并排序)
题目链接:http://poj.org/problem?id=1804 思路分析:序列的逆序数即为交换次数,所以求出该序列的逆序数即可. 根据分治法思想,序列分为两个大小相等的两部分,分别求子序列的逆 ...
- C++一些注意点之异常处理
几篇文章:(1)http://blog.csdn.net/daheiantian/article/details/6530318 (2)http://blog.chinaunix.net/uid-21 ...
- 3644 - X-Plosives(水题,并差集)
3644 - X-Plosives A secret service developed a new kind of explosive that attain its volatile proper ...
- 1410 - Consistent Verdicts(规律)
1410 - Consistent Verdicts PDF (English) Statistics Forum Time Limit: 5 second(s) Memory Limit: 32 ...
- ZigBee研究之旅(二)
在学习ZigBee设备CC2530模块时,编程后程序无法运行,但又十分确定程序的真确性的情况下,看看是不是project栏下的option选项配置的有问题,我是经常在这里出问题,一开始找不到原因,特此 ...
- Swift--基本数据类型(一)
不像更多语言中,X不要求你写一个分号(;)在你的代码中的每一个语句后,尽管能够这样做.然而,假设你想在一行中写入多个单独的语句分号是必需的: . 1 let cat = "" ...
- iOS UISearchBar UISearchController
搜索栏的重要性我们就不说了,狼厂就是靠搜索起家的,现在越来越像一匹没有节操的狼,UC浏览器搜索栏现在默认自家的神马搜索,现在不管是社 交,O2O还是在线教育等都会有一个搜索栏的实现,不过彼此实现效果是 ...
- java打印日历
打个日历,写了半天,感情水平真菜, 不过主要是不会数组,明天一定要把数组看了 package demo; import java.util.Scanner; public class Demo { / ...