use*_*451 2 python session pyramid cornice
我使用Cornice制作RESTful网址.
我安装了pyramid-beaker,并__init__.py在doc中指定了我的include .
而已.然后我在我的观点中这样做了:
p_desc = """Service to post session. """
p = Service(name='p',\
path=root+'/1',\
description=register_desc)
g_desc = """Service to get session. """
g = Service(name='g',\
path=root+'/2',\
description=g_desc)
@g.get()
def view2(request):
print request.session
return Response()
@p.post()
def view1(request):
print 'here'
request.session['lol'] = 'what'
request.session.save()
print request.session
return Response()
Run Code Online (Sandbox Code Playgroud)
这是我的结果
>>> requests.post('http://localhost/proj/1')
<Response [200]>
>>> requests.get('http://localhost/proj/2')
<Response [200]>
Starting HTTP server on http://0.0.0.0:6543
here
{'_accessed_time': 1346107789.2590189, 'lol': 'what', '_creation_time': 1346107789.2590189}
localhost - - [27/Aug/2012 18:49:49] "POST /proj/1 HTTP/1.0" 200 0
{'_accessed_time': 1346107791.0883319, '_creation_time': 1346107791.0883319}
localhost - - [27/Aug/2012 18:49:51] "GET /proj/2 HTTP/1.0" 200 4
Run Code Online (Sandbox Code Playgroud)
正如你所看到的,它会回馈新的会话.如何获得相同的会话以便我可以访问相同的数据?
通过发送给客户端的cookie跟踪会话.所以你的'客户'(requests图书馆)需要再次发回该cookie:
resp = requests.post('http://localhost/proj/1')
cookies = resp.cookies
requests.get('http://localhost/proj/2', cookies=cookies)
Run Code Online (Sandbox Code Playgroud)
更好的是使用requests.session设置:
with requests.session() as s:
s.post('http://localhost/proj/1')
s.get('http://localhost/proj/2')
Run Code Online (Sandbox Code Playgroud)