bottle-session 0.2 : Python Package Index

bottle-session 0.2

Redis based sessions for bottle.

Latest Version: 0.3

  1. Bottle Sessions with Redis
  2. ==========================
  3.  
  4. Bottle_session is a session manager for the Bottle microframework that uses a
  5. cookie to maintain your web session and stores a hash associated with that
  6. cookie using the redis key-value store. It is designed as a simple Bottle
  7. plugin.
  8.  
  9. Installation
  10. ------------
  11. Install using either pip or easy_install:
  12.  
  13. $ pip install bottle-session
  14.  
  15. or you can download the latest version from bitbucket:
  16.  
  17. $ git clone https://devries@bitbucket.org/devries/bottle-session.git
  18. $ cd bottle-session
  19. $ python setup.py install
  20.  
  21. Requirements
  22. ------------
  23. In order to use bottle-session you must have the both the redis and of course
  24. bottle modules installed. I recommend also installing pycrypto, although it is
  25. not required. If pycrypto is installed, then the pycrypto random number
  26. generator is used to generate session cookies, otherwise python's internal
  27. random number generator is used.
  28.  
  29. Using Bottle-session
  30. --------------------
  31. The first requirement is that you import the bottle_session module:
  32.  
  33. :::python
  34. import bottle_session
  35. import bottle
  36.  
  37. Next, initialize the plugin:
  38.  
  39. :::python
  40. app = bottle.app()
  41. plugin = bottle_session.SessionPlugin(cookie_lifetime=600)
  42. app.install(plugin)
  43.  
  44. The `cookie_lifetime` parameter is the lifetime of the cookie in seconds, if
  45. the lifetime is set to **None** it will last 1 week. The `SessionPlugin` class
  46. initializer takes several optional parameters:
  47.  
  48. - `host` is the host for the redis instance. It defaults to `localhost`.
  49. - `port` is the port for the redis instance. It defaults to `6379`.
  50. - `db` is the redis database number. It defaults to `0`.
  51. - `cookie_name` is the name of the session cookie. It defaults to
  52. `bottle.session`.
  53. - `keyword` is the plugin keyword. It defaults to `session`.
  54.  
  55. To use the plugin, just add the keyword (`session` by default) to the routed
  56. method:
  57.  
  58. :::python
  59. @bottle.route('/')
  60. def index(session):
  61. user_name = session.get('name'):
  62. if user_name is not None:
  63. return "Hello, %s"%user_name
  64. else:
  65. return "I don't recognize you."
  66.  
  67. @bottle.route('/set/:user_name')
  68. def set_name(session,user_name=None):
  69. if user_name is not None:
  70. session['name']=user_name
  71. return "I recognize you now."
  72. else:
  73. return "What was that?"
  74.  
  75. bottle.debug(True)
  76. bottle.run(app=app,host='localhost',port=8888)
  77.  
  78. In this example you can set the `name` property of the session cookie to Chris
  79. by visiting the `http://localhost:8888/set/Chris` and then that value is
  80. retrieved when you visit `http://localhost:8888/`.
  81.  
  82. Using Bottle-session and Bottle-redis
  83. -------------------------------------
  84. If you are using redis for sessions you are likely using redis to store other
  85. data as well, and likely use the bottle-redis plugin. You can use both plugins
  86. together, and you can even get them to use the same connection pool.
  87. Initialize them by creating a connection pool which you attach to each plugin
  88. object before installing them into the bottle application as shown below:
  89.  
  90. :::python
  91. #!/usr/bin/env python
  92. import bottle_session
  93. import bottle_redis
  94. import bottle
  95. import redis
  96. from datetime import datetime
  97.  
  98. app = bottle.app()
  99. session_plugin = bottle_session.SessionPlugin()
  100. redis_plugin = bottle_redis.RedisPlugin()
  101.  
  102. connection_pool = redis.ConnectionPool(host='localhost', port=6379)
  103.  
  104. session_plugin.connection_pool = connection_pool
  105. redis_plugin.redisdb = connection_pool
  106. app.install(session_plugin)
  107. app.install(redis_plugin)
  108.  
  109. @bottle.route('/')
  110. def index(session,rdb):
  111. rdb.incr('visitors')
  112. visitor = rdb.get('visitors')
  113. last_visit = session['visit']
  114. session['visit'] = datetime.now().isoformat()
  115.  
  116. return 'You are visitor %s, your last visit was on %s'%(visitor,last_visit)
  117.  
  118. bottle.debug(True)
  119. bottle.run(app=app,host='localhost',port=8888)
  120.  
  121. Acknowledgments
  122. ---------------
  123. Thanks to Marcel Hellkamp and the bottle community for the framework and to
  124. Sean M. Collins whose bottle-redis package in bottle-extras served as the
  125. inspiration for this bottle plugin.

bottle-session 0.2 : Python Package Index的更多相关文章

  1. django-cookieless 0.7 : Python Package Index

    django-cookieless 0.7 : Python Package Index django-cookieless 0.7 Download django-cookieless-0.7.ta ...

  2. 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 ...

  3. pyrailgun 0.24 : Python Package Index

    pyrailgun 0.24 : Python Package Index pyrailgun 0.24 Download pyrailgun-0.24.zip Fast Crawler For Py ...

  4. 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 ...

  5. bottle-session 0.3 : Python Package Index

    bottle-session 0.3 : Python Package Index bottle-session 0.3

  6. graphterm 0.40.1 : Python Package Index

    graphterm 0.40.1 : Python Package Index graphterm 0.40.1 Downloads ↓ A Graphical Terminal Interface ...

  7. 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 ...

  8. 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 ...

  9. Install OpenCV 3.0 and Python 2.7+ on Ubuntu

    为了防止原文消失或者被墙,转载留个底,最好还是去看原贴,因为随着版本变化,原贴是有人维护升级的 http://www.pyimagesearch.com/2015/06/22/install-Open ...

随机推荐

  1. win7 x64 驱动

    原文:win7 x64 驱动 从x86转x64 1.编译环境要为x64 2.修改inf文件 [Manufacturer] %MfgName%=Mfg0,NT,NTia64,NTAMD64 [Mfg0] ...

  2. jsp中forward和redirect的区别(转)

    一.调用方式 我们知道,在servlet中调用转发.重定向的语句如下: request.getRequestDispatcher("new.jsp").forward(reques ...

  3. linux配置nfs服务

    简单介绍: unix/linux系统一种远程文件文件夹共享的服务,能够把某一个远程的文件文件夹共享到本地,进而像操作本地文件一样,操作这个远程的文件夹. 比如:a主机作为服务端,共享出来test1这个 ...

  4. if语句求三个数中最大的

    Console.WriteLine("请输入第一个数:"); int a = Convert.ToInt32( Console.ReadLine()); Console.Write ...

  5. MYSQL - php 使用 localhost 无法连接数据库

    php 使用 localhost 无法连接数据库,而使用127.0.0.1却能连接成功. 可能原因: 系统hosts文件未提供127.0.0.1到localhost的解析.解决方法(以win7系统为例 ...

  6. 解决 Xcode7 中多个模拟器的办法

    转自: http://www.oschina.net/code/snippet_196012_50574 1.关闭xcode 2.终端输入 sudo killall -9 com.apple.Core ...

  7. Oracle同义词 synonyms

    Oracle中的同义词: 总结:简单的一句话,Oracle中不同用户的表一般都只能够自己的所属的用户可以用,如果不想通过授权的方式授权给其他用户使用,那么创建表的时候在表名的前面加上 synonyms ...

  8. 使用ownCloud在Linux安装你的个人云服务

    ownCloud是一个免费开源的软件,用于为分享文件,日历,联系人,书签和个人音频/视频.非常容易安装和管理. 前提 在这篇教程里我使用CentOS 6.5 minimal server来安装ownC ...

  9. WebApp模版并运行

    WebApp模版并运行 ASP.NET Core 运行原理剖析1:初始化WebApp模版并运行 核心框架 ASP.NET Core APP 创建与运行 总结 之前两篇文章简析.NET Core 以及与 ...

  10. 使用Maven打包项目并上传到Linux服务器

    Maven打包: 项目右键Run as-->Maven build...-->  出来下面的界面,注意红色部分的填写,Goals填写package表示打包,下面的Skip Tests表示打 ...