使用wsgistate的cromlech会话处理

cromlech.wsgistate的Python项目详细描述


烧杯是烧杯与Cromlech的集成,用于会话管理。

上下文管理器

让我们导入一些帮助程序:

>>> import pytest
>>> from webtest import TestApp
>>> SK = 'session.key'

我们的会话控制器:

>>> from cromlech.wsgistate import WsgistateSession
>>> from cromlech.wsgistate.controlled import WsgistateSession

>>> def simple_app(environ, start_response):
...     """retained visited path, raise exception if path contain 'fail'
...     """
...     with WsgistateSession(environ, SK) as session:
...         path = environ['PATH_INFO']
...         history = session.setdefault('path', [])
...         history.append(path)
...         if path == '/fail':
...             raise ValueError
...     start_response('200 OK', [('Content-type', 'text/plain')])
...     return [', '.join(history)]

然后使用wsgistate middelware运行它:

>>> from cromlech.wsgistate import session_wrapper
>>> wsgi_app = TestApp(session_wrapper(simple_app, session_key=SK))
>>> result = wsgi_app.get('/foo')
>>> result.status
'200 OK'
>>> result.body
'/foo'
>>> result = wsgi_app.get('/bar')
>>> result.status
'200 OK'
>>> result.body
'/foo, /bar'

如果应用程序引发异常,则上下文管理器不会保存会话:

>>> result = wsgi_app.get('/fail')
Traceback (most recent call last):
...
ValueError
>>> result.status
'200 OK'
>>> result.body
'/foo, /bar'

测试事务感知

>>> import transaction
>>> from cromlech.wsgistate import SessionStateException
>>> def transactional_app(environ, start_response):
...
...     with transaction.manager as tm:
...         with WsgistateSession(environ, SK, tm) as session:
...             session['crom'] = 'Pyramid'
...             tm.abort()
...
...     assert not session
...
...     with transaction.manager as tm:
...         with WsgistateSession(environ, SK, tm) as session:
...             session['crom'] = 'Crom'
...
...     with transaction.manager as tm:
...         with WsgistateSession(environ, SK, tm) as session:
...             session['lech'] = 'Lech'
...             sp1 = tm.savepoint()
...
...             session['crom'] = 'Zope'
...             session['lech'] = 'Quack'
...             sp2 = tm.savepoint()
...
...             session['crom'] = 'PHP'
...             sp3 = tm.savepoint()
...
...             sp2.rollback()
...             assert session['crom'] == 'Zope'
...             assert session['lech'] == 'Quack'
...
...             session['crom'] = 'Zorglub'
...             session['lech'] = 'Zimbabwe'
...
...             sp1.rollback()
...
...     # outside of a transaction, the writing is prohibited
...     with pytest.raises(SessionStateException) as e:
...         session['fail'] = True
...
...     assert e.value.message == (
...         "Session's current state disallows writing")
...
...     start_response('200 OK', [('Content-type', 'text/plain')])
...     return [session.get('crom', ''), session.get('lech', '')]
>>> wsgi_app = TestApp(session_wrapper(transactional_app, session_key=SK))
>>> result = wsgi_app.get('/bar')
>>> result.body
'CromLech'

变更日志

0.3b2(2017-03-18)

  • 添加了python3兼容性

0.3b1(2016-11-29)

  • 添加了超时异常并在环境中冒泡,以了解何时 会话已过期。

0.2(2014-08-06)

  • 将本地配置选项转发给decorator,以便进一步 配置

0.1(2013-03-13)

  • 初始版本

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

推荐PyPI第三方库


热门话题
不同窗口中的java视图   java创建SQL插入语句到CSV文件   java效率检查:Opengl动画代码   在clojure中处理Java可选<T>   java理解camel中的输入/输出交换模式行为   对于使用jpackage构建的应用程序,java LSOpenURLsWithRole()失败,错误为10810   多线程Java同步:多重倒计时闩锁   java哪个类应该做这项工作?   java在运行时出现问题。getRuntime()。执行官   java我们不能在GAE中使用集合或集合作为返回类型吗?   amazon web服务返回类型与RequestHandler<Object,String>不兼容。JAVA中的HandlerRequest(对象、上下文)   如何在Java中使用ExecutorService设置任务的超时时间