在GAE上坚持Webapp2会话 [英] Persisting Webapp2 Sessions on GAE

查看:128
本文介绍了在GAE上坚持Webapp2会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在GAE上运行时,我无法将会话变量保存在webapp2会话存储中。我已经创建了一个超级简单的应用程序来重现这个问题:

$ p $ class MainHandler(webapp2.RequestHandler):

def post(self):

session = self.session_store.get_session()

previous_value = session.get(myvalue)
self.response .write(previous_value)

def get(self):

session = self.session_store.get_session()

previous_value = session.get( myvalue)
self.response.write(previous_value)

session [myvalue] =Hi!+(previous_value if previous_value else)

#这是webapp2会话工作所需的
def dispatch(self):

#获取此请求的会话存储。
self.session_store = sessions.get_store(request = self.request)

try:
super(MainHandler,self).dispatch()

最后:
#保存所有会话。
self.session_store.save_sessions(self.response)

这里的前提是 get 请求设置会话变量,而 post 只是写入响应对象。



我的理解是,在执行 get 请求之后,会话会持续 myvalue 。之后,如果我执行 post 请求,那么会再次获得请求, myvalue 应该仍然存在,尽管我没有在 post 处理程序中再次设置它。唉,情况并非如此:

 >>> cookies =无
>>> (10):
... r = requests.get(http:// localhost:11282 /,cookies = cookies)
... cookies = r.cookies
... print r.text
...
None
嗨!
嗨!嗨!
嗨!嗨!嗨!
嗨!嗨!嗨!嗨!
嗨!嗨!嗨!嗨!嗨!
嗨!嗨!嗨!嗨!嗨!嗨!
嗨!嗨!嗨!嗨!嗨!嗨!嗨!
嗨!嗨!嗨!嗨!嗨!嗨!嗨!嗨!
嗨!嗨!嗨!嗨!嗨!嗨!嗨!嗨!嗨!
>>>
>>> cookies =无
>>> (10):
... r = requests.get(http:// localhost:11282 /,cookies = cookies)
... cookies = r.cookies
... print r.text
... r = requests.post(http:// localhost:11282 /,cookies = cookies)
... cookies = r.cookies
... print r.text
...
None
嗨!

嗨!

嗨!

嗨!

嗨!

嗨!

嗨!

嗨!

嗨!

嗨!


解决方案

由于我对网络技术和标准相当陌生,我似乎错过了一些东西:

  def save_session(self,response):
如果self.session为None或not self.session.modified:
return

这是执行 SecureCookie 。由于cookie没有被保存到响应中,如果它没有被修改,我猜测每个标准/协议,后端不负责再次发送cookie,因为它应该已经在客户端上了。




这证明了它(切换到请求.Session()):

 >>> s = requests.Session()
>>>我在范围(5):
... r = s.get(http:// localhost:11282 /)
... print r.text
... r = s.post(http:// localhost:11282 /)
... print r.text
...
None
POST
Hi !
POST
嗨!嗨!
POST
嗨!嗨!嗨!
POST
嗨!嗨!嗨!嗨!
POST
>>>


I am having trouble persisting my session variables in a webapp2 session store, while running on GAE. I have created a super simple app to reproduce the issue:

class MainHandler(webapp2.RequestHandler):

    def post(self):

        session = self.session_store.get_session()

        previous_value = session.get("myvalue")
        self.response.write(previous_value)

    def get(self):

        session = self.session_store.get_session()

        previous_value = session.get("myvalue")
        self.response.write(previous_value)

        session["myvalue"] = "Hi! " + (previous_value if previous_value else "")

    # this is needed for webapp2 sessions to work
    def dispatch(self):

        # Get a session store for this request.
        self.session_store = sessions.get_store(request=self.request)

        try:
            super(MainHandler, self).dispatch()

        finally:
            # Save all sessions.
            self.session_store.save_sessions(self.response)

The premise here is that the get request sets a session variable while the post simply writes to the response object.

My understanding is that after performing the get request, the session persists myvalue. After that, if I perform a post request, then a get request again, myvalue should still be there, even though I didn't set it again in the post handler. Alas, that is not the case:

>>> cookies = None
>>> for i in range(10):
...     r = requests.get("http://localhost:11282/", cookies=cookies)
...     cookies = r.cookies
...     print r.text
... 
None
Hi! 
Hi! Hi! 
Hi! Hi! Hi! 
Hi! Hi! Hi! Hi! 
Hi! Hi! Hi! Hi! Hi! 
Hi! Hi! Hi! Hi! Hi! Hi! 
Hi! Hi! Hi! Hi! Hi! Hi! Hi! 
Hi! Hi! Hi! Hi! Hi! Hi! Hi! Hi! 
Hi! Hi! Hi! Hi! Hi! Hi! Hi! Hi! Hi! 
>>> 
>>> cookies = None
>>> for i in range(10):
...     r = requests.get("http://localhost:11282/", cookies=cookies)
...     cookies = r.cookies
...     print r.text
...     r = requests.post("http://localhost:11282/", cookies=cookies)
...     cookies = r.cookies
...     print r.text
... 
None
Hi! 
None
Hi! 
None
Hi! 
None
Hi! 
None
Hi! 
None
Hi! 
None
Hi! 
None
Hi! 
None
Hi! 
None
Hi!

解决方案

Since I am fairly new to web technologies and standards, I seem to have missed something:

def save_session(self, response):
    if self.session is None or not self.session.modified:
        return

This is the implementation of SecureCookie. Since the cookie is not saved to the response if it is not modified, I am guessing that per standard/protocol, the backend is not responsible for sending the cookie again, as it should already be on the client. It would be great to find a reference to this.


This proves it (switching to requests.Session()):

>>> s = requests.Session()
>>> for i in range(5):
...     r = s.get("http://localhost:11282/")
...     print r.text
...     r = s.post("http://localhost:11282/")
...     print r.text
... 
None
POST
Hi! 
POST
Hi! Hi! 
POST
Hi! Hi! Hi! 
POST
Hi! Hi! Hi! Hi! 
POST
>>> 

这篇关于在GAE上坚持Webapp2会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆