google app engine python运行时会话
vishnu的Python项目详细描述
python wsgi应用程序的会话。
功能
- python wsgi应用程序的基于cookie的会话
- 可为以下cookie设置配置
- 域
- 路径
- 安全
- 仅限http
- 过期(超时)
- 用于验证cookie未被篡改的hmac签名
- 自动保存选项,可在修改会话值时随时保存该选项
- 使用AES对cookie数据进行可选加密
- 每个会话的自定义超时
安装
vishnu可以在PyPi上使用,我们建议通过pip安装。
pip install vishnu
还存在以下额外的安装,其中包括所需的后端库作为一个要求。
pip install vishnu[pymemcache] pip install vishnu[python-memcached] pip install vishnu[redis]
如果您使用的是google app engine,我们建议通过pip作为vendored package安装。
pip install -t lib vishnu
编辑appengine_config.py文件并将库目录提供给vendor.add()方法。
fromgoogle.appengine.extimportvendorvendor.add('lib')
或者下载您的preferred tagged release,您应该包括的是vishnu文件夹。
配置
会话配置
以下参数可用于会话配置。
parameter | required | default | type | description |
---|---|---|---|---|
^{tt6}$ | yes | ^{tt7}$ | string | Secret used for HMAC signature, must be at least 32 characters. |
^{tt8}$ | no | ^{tt5}$ | string | Name to use for cookie. |
^{tt10}$ | no | ^{tt7}$ | string | Key used to encrypt cookie data, if omitted then data will not be encrypted. |
^{tt12}$ | no | ^{tt13}$ | bool | Only send this cookie over SSL |
^{tt14}$ | no | N/A | string | The domain to set the cookie for, it omitted will use domain cookie was served from. |
^{tt15}$ | no | ^{tt16}$ | string | The path to set the cookie for, if omitted it will default to ^{tt16}$ |
^{tt18}$ | no | ^{tt13}$ | string | A http-only cookie cannot be accessed by client-side APIs, such as JavaScript |
^{tt20}$ | no | ^{tt21}$ | bool | Automatically save the session when a value is set. |
^{tt22}$ | no | N/A | integer | How long until session/cookie expires, it omitted it will last for the length of the browser session. |
^{tt23}$ | yes | N/A | backend | See backends configuration |
会话配置的示例。
fromvishnu.sessionimportConfigfromvishnu.backendimportRedisconfig=Config(secret="your_secret",backend=Redis())
wsgi中间件
要使用vishnu,必须将其作为中间件添加到wsgi应用程序中。
fromvishnu.backendimportRedisfromvishnu.middlewareimportSessionMiddlewarefromvishnu.sessionimportConfigmy_config=Config(secret="your_secret",backend=Redis())app=SessionMiddleware(app=wsgi_app,config=my_config)
后端
谷歌应用程序引擎(memcache)
fromvishnu.backendimportGoogleAppEngineMemcacheconfig=Config(secret="your_secret",backend=GoogleAppEngineMemcache())
谷歌应用引擎(ndb)
fromvishnu.backendimportGoogleAppEngineNDBconfig=Config(secret="your_secret",backend=GoogleAppEngineNDB())
pymemcache
parameter | required | default | type |
---|---|---|---|
^{tt24}$ | no | ^{tt25}$ | string |
^{tt26}$ | no | ^{tt27}$ | integer |
fromvishnu.backendimportPyMemcacheconfig=Config(secret="your_secret",backend=PyMemcache(host="memcache.host",port=11222))
Python咬伤
parameter | required | default | type |
---|---|---|---|
^{tt24}$ | no | ^{tt25}$ | string |
^{tt26}$ | no | ^{tt27}$ | integer |
fromvishnu.backendimportPythonMemcachedconfig=Config(secret="your_secret",backend=PythonMemcached())
重新发现
parameter | required | default | type |
---|---|---|---|
^{tt24}$ | no | ^{tt25}$ | string |
^{tt26}$ | no | ^{tt35}$ | integer |
^{tt36}$ | no | ^{tt37}$ | integer |
fromvishnu.backendimportRedisconfig=Config(secret="your_secret",backend=Redis(host="redis.host",port=6421,db=0))
设置自定义超时
每个会话都使用服务器配置中指定的默认超时,但如果希望特定会话与此不同,可以执行以下操作。
session=vishnu.get_session()session.timeout=3600session.save()
超时时间以秒为单位。要将超时设置为在此会话结束时过期,可以使用vishnu.session.TIMEOUT_SESSION常量。
session=vishnu.get_session()session.timeout=vishnu.session.TIMEOUT_SESSIONsession.save()
清理过期会话(仅限google app engine ndb后端)
将以下内容添加到cron处理程序。
fromvishnu.utilimportgae_ndb_delete_expired_sessionswhilenotgae_ndb_delete_expired_sessions():pass
通过以秒为单位传递值dormant_for,可以更改删除过期会话后的时间段。还可以使用limit参数更改每个调用要删除的会话数。
fromvishnu.utilimportgae_ndb_delete_expired_sessionswhilenotgae_ndb_delete_expired_sessions(dormant_for=3600,limit=100):pass