2012-12-29 58 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'? –