博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
bottle-session 0.2 : Python Package Index
阅读量:7113 次
发布时间:2019-06-28

本文共 4163 字,大约阅读时间需要 13 分钟。

bottle-session 0.2

Redis based sessions for bottle.

Latest Version:

Bottle Sessions with Redis==========================Bottle_session is a session manager for the Bottle microframework that uses acookie to maintain your web session and stores a hash associated with thatcookie using the redis key-value store. It is designed as a simple Bottleplugin.Installation------------Install using either pip or easy_install:    $ pip install bottle-sessionor you can download the latest version from bitbucket:    $ git clone https://devries@bitbucket.org/devries/bottle-session.git    $ cd bottle-session    $ python setup.py installRequirements------------In order to use bottle-session you must have the both the redis and of coursebottle modules installed. I recommend also installing pycrypto, although it isnot required. If pycrypto is installed, then the pycrypto random numbergenerator is used to generate session cookies, otherwise python's internalrandom number generator is used.Using Bottle-session--------------------The first requirement is that you import the bottle_session module:    :::python    import bottle_session    import bottleNext, 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, ifthe lifetime is set to **None** it will last 1 week. The `SessionPlugin` classinitializer 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 routedmethod:    :::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 Chrisby visiting the `http://localhost:8888/set/Chris` and then that value isretrieved 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 otherdata as well, and likely use the bottle-redis plugin. You can use both pluginstogether, and you can even get them to use the same connection pool.Initialize them by creating a connection pool which you attach to each pluginobject 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 toSean M. Collins whose bottle-redis package in bottle-extras served as theinspiration for this bottle plugin.

转载地址:http://gyghl.baihongyu.com/

你可能感兴趣的文章
在存储过程中编写正确的事务处理代码(SQL Server 2000 & 2005)
查看>>
Android 控件在布局中按比例放置[转]
查看>>
内核通知链 学习笔记 【转】
查看>>
Input Method of Win32 System
查看>>
count(*) VS count(X)
查看>>
MS ASP.Net Ajax 服务端扩展
查看>>
android102 查询,插入联系人
查看>>
数据库邮件
查看>>
adstrtal.sh报超时错误 ERROR : Timed out( 100000 ): Interrupted Exception
查看>>
一个前端工程师的基本修养
查看>>
ZT:三十个好习惯
查看>>
.Net开发笔记(七)使用组件编程
查看>>
ASP.NET企业开发框架IsLine FrameWork系列之八--AppLogProvider日志框架(下)
查看>>
DataBase异常状态:Recovery Pending,Suspect,估计Recovery的剩余时间
查看>>
一个android版本的rss阅读器--明天补充实现过程,先上图
查看>>
WPF TreeView
查看>>
HTML: 仿写一个财经类静态的网页
查看>>
POJ 3979 分数减法【数学问题的探讨】
查看>>
HashSet
查看>>
C#读写config配置文件
查看>>