2015-08-29 28 views
0

我對App Engine並不陌生,但我剛開始使用它的Python堆棧。我使用自己的Python使用webapp2和Cygwin。我試圖實現自定義身份驗證,依靠會話..我遵循使用webapp2-extras模塊啓用會話的衆多示例。我有以下相關代碼:錯誤序列化與webapp2_extras的GAE會話

import webapp2 

from webapp2_extras import sessions 

class BaseHandler(webapp2.RequestHandler): 
    def dispatch(self): 
     self.session_store = sessions.get_store(request=self.request) 
     try: 
      webapp2.RequestHandler.dispatch(self) 
     finally: 
      self.session_store.save_sessions(self.response) 

    @webapp2.cached_property 
    def session(self): 
     return self.session_store.get_session(self.response, backend='datastore') 

class CounterHandler(BaseHandler): 
    def get(self): 
     cnt_key = 'cnt' 
     if cnt_key in self.session: 
      cnt = int(self.session[cnt_key]) 
     else: 
      cnt = 0 
     cnt += 1 
     self.session[cnt_key] = str(cnt) 

     self.response.headers['Content-Type'] = 'text/plain' 
     self.response.write("Hello, world!") 

這種失敗與以下堆棧跟蹤:

Traceback (most recent call last): 
    File "/cygdrive/c/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1535, in __call__ 
    rv = self.handle_exception(request, response, e) 
    File "/cygdrive/c/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1529, in __call__ 
    rv = self.router.dispatch(request, response) 
    File "/cygdrive/c/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1278, in default_dispatcher 
    return route.handler_adapter(request, response) 
    File "/cygdrive/c/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1102, in __call__ 
    return handler.dispatch() 
    File "/cygdrive/c/Documents and Settings/dev/My Documents/proj/easytime_gae/src/handlers.py", line 11, in dispatch 
    self.session_store.save_sessions(self.response) 
    File "/cygdrive/c/google_appengine/lib/webapp2-2.5.2/webapp2_extras/sessions.py", line 420, in save_sessions 
    session.save_session(response) 
    File "/cygdrive/c/google_appengine/lib/webapp2-2.5.2/webapp2_extras/appengine/sessions_ndb.py", line 117, in save_session 
    response, self.name, {'_sid': self.sid}, **self.session_args) 
    File "/cygdrive/c/google_appengine/lib/webapp2-2.5.2/webapp2_extras/sessions.py", line 423, in save_secure_cookie 
    value = self.serializer.serialize(name, value) 
    File "/cygdrive/c/google_appengine/lib/webapp2-2.5.2/webapp2_extras/securecookie.py", line 48, in serialize 
    signature = self._get_signature(name, value, timestamp) 
    File "/cygdrive/c/google_appengine/lib/webapp2-2.5.2/webapp2_extras/securecookie.py", line 103, in _get_signature 
    signature.update('|'.join(parts)) 
TypeError: sequence item 0: expected string, Response found 

錯誤消失,如果我禁用寫入會話對象行,離開從完整的會話閱讀:

 # self.session[cnt_key] = str(cnt) 

任何幫助將非常感激。

回答

1

get_session的第一個參數是名稱,如果您想更改默認值。你正在傳遞一個響應對象,這是錯誤的。

@webapp2.cached_property 
def session(self): 
    return self.session_store.get_session(backend='datastore') 
+0

謝謝,就是這樣! –

相關問題