2012-12-29 55 views
15

我試圖實現GAE的webapp2會話,但似乎很少有關於它的文檔。據http://webapp-improved.appspot.com/api/webapp2_extras/sessions.html,我的步驟如下:GAE webapp2會話:創建和檢查會話的正確過程

1.Configure並添加配置到主應用程序:在登錄處理

config = {} 
config['webapp2_extras.sessions'] = { 
    'secret_key': 'my_secret_key', 
} 
app = webapp2.WSGIApplication([...], config=config) 

2.創建會話

# Delete existent session 
    --> not mention in the tutorial 
# member is found  
self.session_store = sessions.get_store(request=handler.request) 
self.session['account'] = member.account 

3.檢查如果會話存在於我的程序中的各個位置

if self.session['account']: 
    # Session exists 

4.刪除sess離子當用戶註銷

--> not mentioned in the tutorial 

我的問題:

  1. 我收到錯誤消息「...對象有沒有屬性‘會議’」在會話創建過程(步驟2)

  2. 如何在步驟2和步驟4中刪除會話?

  3. 整體會話管理過程是否正確?

謝謝。

回答

5

這可能不是問題的直接答案,但它是我使用gaesessions而不是GAE的webapp2會話找到的解決方案,我希望與以往分享ybody。在這裏,我們去:從https://github.com/dound/gae-sessions

  1. 下載gaesessions通過點擊 「下載ZIP」 按鈕。下載的文件是「gae-sessions-master.zip」。

  2. 解壓文件(目錄「GAE的會話主」將被創建),然後複製目錄「gaessions」到你的應用程序的根目錄下(即,「app.yaml中」是)

  3. 創建一個名爲「appengine_config」的文件。PY」在根目錄中,有如下的內容(複製形式https://github.com/dound/gae-sessions/tree/master/demo):

    from gaesessions import SessionMiddleware 
    
    # Original comments deleted ... 
    # Create a random string for COOKIE_KDY and the string has to 
    # be permanent. "os.urandom(64)" function may be used but do 
    # not use it *dynamically*. 
    # For me, I just randomly generate a string of length 64 
    # and paste it here, such as the following: 
    
    COOKIE_KEY = 'ppb52adekdhD25dqpbKu39dDKsd.....' 
    
    def webapp_add_wsgi_middleware(app): 
        from google.appengine.ext.appstats import recording 
        app = SessionMiddleware(app, cookie_key=COOKIE_KEY) 
        app = recording.appstats_wsgi_middleware(app) 
        return app 
    
  4. 創建時在用戶登錄(可變帳戶是用戶的帳戶)的會話:

    from gaesessions import get_current_session 
    session = get_current_session() 
    if session.is_active(): 
        session.terminate() 
    # start a session for the user (old one was terminated) 
    session['account'] = account 
    
  5. 檢查用戶的會話是否存在,如果是,則返回用戶的帳戶:

    from gaesessions import get_current_session 
    def checkSession(): 
        session = get_current_session() 
        if session.is_active(): 
         return session['account'] 
        return False 
    
  6. ,當用戶註銷刪除會話:

    def logout(): 
        session = get_current_session() 
        if session.is_active(): 
         session.terminate() 
    
  7. 最後,您可以創建一個cron作業,定期清理過期會話:

cron.yaml:

- description: daily session cleanup 
    url: /clean_up_sessions 
    schedule: every day 3:00 
    timezone: ... (Your time zone) 

功能:

from gaesessions import delete_expired_sessions 
class clean_up_sessions(webapp2.RequestHandler): 
    def get(self): 
     while not delete_expired_sessions(): 
      pass 

希望這可以幫助。

+1

爲什麼使用gae-sessions而不是webapp2_extras.sessions? gae會話將自身與幾個會話系統進行比較,但不會與webapp2的會話進行比較。 – Romz

+0

非常感謝,Romz。我不知道有webapp2_extras.sessions。我會試一試。 –

15

這裏是處理程序的一個例子,以及如何使用webapp2的額外會議

main.py與BaseHandler和MainHandler

import webapp2 
from webapp2_extras import sessions 

class BaseHandler(webapp2.RequestHandler):    # taken from the webapp2 extrta session example 
    def dispatch(self):         # override dispatch 
     # Get a session store for this request. 
     self.session_store = sessions.get_store(request=self.request) 

     try: 
      # Dispatch the request. 
      webapp2.RequestHandler.dispatch(self)  # dispatch the main handler 
     finally: 
      # Save all sessions. 
      self.session_store.save_sessions(self.response) 

    @webapp2.cached_property 
    def session(self): 
     # Returns a session using the default cookie key. 
     return self.session_store.get_session() 

class YourMainHandler(BaseHandler): 

    def get(self): 

     .... 
     self.session['foo'] = 'bar' 


    def post(self): 


     foo = self.session.get('foo') 

如果你有一個單獨的login.py:

.... other imports 
import main 

class Login(main.BaseHandler): 

    def get(self): 

     .... 
     self.session['foo'] = 'bar' 


    def post(self): 


     foo = self.session.get('foo') 
+0

非常感謝您的幫助。我仍在掙扎,但沒有運氣。假設我有一個模塊「login.py」,其中包含類Login(webapp2.RequestHandler),用於處理用戶登錄。其中還有一個主模塊「main.py」,其中包含MainPage(BaseHandler)類。我如何更改登錄類?我在login.py中導入main,並將Login Login(webapp2.RequestHandler)更改爲Login(main.BaseHandler)。錯誤消息:'模塊'對象沒有屬性'BaseHandler' –

+0

我已經更新了答案。 – voscausa

+0

這就是我在說的:錯誤消息:'模塊'對象沒有屬性'BaseHandler' –

3

在你RequestHandler覆蓋dispatch:從webapp2_extras進口會話

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稱爲session

@webapp2.cached_property 
def session(self): 
    return self.session_store.get_session(backend="<whatever you want here>") 

當你要訪問會話值,你做self.session[<key>]

當用戶登錄時,你可以調用:

self.auth.get_user_by_password(auth_id, password, remember=True, 
              save_session=True) 

這會照顧擺脫舊的會話,並創造了新的給你,或:至於退出

self.auth.set_session(self.auth.store.user_to_dict(self.user), remember=True) 

,你應該需要致電:

self.auth.unset_session() 
+0

什麼是'self.auth'? –