google app engine python运行时会话

vishnu的Python项目详细描述


python wsgi应用程序的会话。

DownloadsLatest Versionbuild status

功能

  • 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文件夹。

配置

会话配置

以下参数可用于会话配置。

parameterrequireddefaulttypedescription
^{tt6}$yes^{tt7}$stringSecret used for HMAC signature, must be at least 32 characters.
^{tt8}$no^{tt5}$stringName to use for cookie.
^{tt10}$no^{tt7}$stringKey used to encrypt cookie data, if omitted then data will not be encrypted.
^{tt12}$no^{tt13}$boolOnly send this cookie over SSL
^{tt14}$noN/AstringThe domain to set the cookie for, it omitted will use domain cookie was served from.
^{tt15}$no^{tt16}$stringThe path to set the cookie for, if omitted it will default to ^{tt16}$
^{tt18}$no^{tt13}$stringA http-only cookie cannot be accessed by client-side APIs, such as JavaScript
^{tt20}$no^{tt21}$boolAutomatically save the session when a value is set.
^{tt22}$noN/AintegerHow long until session/cookie expires, it omitted it will last for the length of the browser session.
^{tt23}$yesN/AbackendSee 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

parameterrequireddefaulttype
^{tt24}$no^{tt25}$string
^{tt26}$no^{tt27}$integer
fromvishnu.backendimportPyMemcacheconfig=Config(secret="your_secret",backend=PyMemcache(host="memcache.host",port=11222))

Python咬伤
parameterrequireddefaulttype
^{tt24}$no^{tt25}$string
^{tt26}$no^{tt27}$integer
fromvishnu.backendimportPythonMemcachedconfig=Config(secret="your_secret",backend=PythonMemcached())

重新发现

parameterrequireddefaulttype
^{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

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java使用Jackson解析非同构JSON对象数组   java为什么'Stream<T>::<A>toArray(IntFunction<A[]>)`接受没有绑定的类型参数A?   java在JavaFX2.0中获取给定布局中的节点大小?   java双链接列表创建节点   java使用HashMap添加、删除和查找   java中push_back(C++)的等效方法是什么?   java在Jetty中运行servlet时获得HTTP 500   用java显示包含图像和文本的页面的最简单文档格式   swing从选项卡窗格Java中的不同选项卡访问数据   字符串Java帮助检查登录类使用。CSV文件   java Struts 1.2.9动作链接   包含max元素的java列表   currentNode上的jcr Java空检查   在Android中使用OpenNLP的POSTaggerMe时出现java NullPointerException   java Logback只将消息记录到syslog一次   如何用Java编写构造函数的API文档   java从gallery中获取所有图像并存储在阵列中   java Maven:将外部jar文件夹添加到类路径