GAE webapp2会话:创建和检查会话的正确过程 [英] GAE webapp2 session: the correct process of creating and checking sessions

查看:115
本文介绍了GAE webapp2会话:创建和检查会话的正确过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图实现GAE的webapp2会话,但似乎很少有关于它的文档。根据 http://webapp-improved.appspot.com/api/webapp2_extras/sessions.html,我的步骤如下:

1.配置并添加配置到主应用程序:

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

2.在登录处理程序中创建会话

 #删除现有会话
- >没有在教程中提到
#成员被发现
self.session_store = sessions.get_store(request = handler.request)
self.session ['account'] = member.account

3.检查程序中各个位置是否存在会话

  if self.session ['account']:
#Session存在

4.用户注销时删除会话

   - >在本教程中未提及

我的问题:

<在会话创建过程中,我收到了错误信息... object has no attribute'session'(第2步)
li>

如何在步骤2和步骤4中删除会话?

  • 整个会话管理过程是否正确?

  • >

    谢谢。

    解决方案

    直接回答这个问题,但这是我使用gaesessions而不是GAE的webapp2会话找到的解决方案,我想与大家分享。在这里,我们去: https://github.com/dound/gae-sessions 点击下载ZIP按钮。下载的文件是gae-sessions-master.zip。

  • 解压文件(将创建一个目录gae-sessions-master),并将目录gaessions复制到应用程序的根目录(即app.yaml所在的位置)。


  • 创建一个名为appengine_config的文件。 py放在根目录下,并带有以下内容(复制形式 https:// github .com / dound / gae-sessions / tree / master / demo ):来自gaesessions的

     导入SessionMiddleware 

    #原始评论已删除...
    #为COOKIE_KDY创建一个随机字符串,字符串必须为
    #为永久字符串。可以使用os.urandom(64)函数,但
    #不能动态使用它*。
    #对于我而言,我只是随机生成一个长度为64
    #的字符串并粘贴到此处,如下所示:

    COOKIE_KEY ='ppb52adekdhD25dqpbKu39dDKsd .....'

    def webapp_add_wsgi_middleware(app):
    from google.appengine.ext.appstats导入记录
    app = SessionMiddleware(app,cookie_key = COOKIE_KEY)
    app =记录。 appstats_wsgi_middleware(app)
    返回应用程序


  • 在用户登录时创建会话(变量账户是用户的账户):

      from gaesessions import get_current_session 
    session = get_current_session()
    如果session.is_active():
    session.terminate()
    #为用户启动一个会话(旧的被终止)
    session ['account'] =账户


  • 检查用户的会话是否存在,如果是,返回用户帐户: p>

      from gaesessions import get_current_session 
    def c heckSession():
    session = get_current_session()
    if session.is_active():
    return session ['account']
    return False


     

    def logout():
    session = get_current_session()
    如果session.is_active():
    session.terminate()

  • 最后,您可以创建一个cron作业来定期清理过期的会话:

  • ol>

    cron.yaml:

       - 描述:每日会话清理
    网址:/ clean_up_sessions
    时间表:每天3:00
    时区:...(您的时区)

    函数:

      from gaesessions import delete_expired_sessions $ b $ class clean_up_sessions(webapp2.RequestHandler) :
    def get(self):
    而不是delete_expired_sessions():
    传递

    希望这有助于。


    I tried to implement GAE's webapp2 session, but there seems very little documentation about it. According to http://webapp-improved.appspot.com/api/webapp2_extras/sessions.html, my steps are as follows:

    1.Configure and add config to the main application:

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

    2.Create session in the login handler

    # 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.Check if a session exists at various locations in my program

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

    4.Delete session when user logs out

    --> not mentioned in the tutorial
    

    My questions:

    1. I got error message " ... object has no attribute 'session'" during the session creation process (Step 2)

    2. How do I delete a session in steps 2 and 4?

    3. Is the overall session management process correct?

    Thanks.

    解决方案

    This may not be a direct answer to the question, but it is a solution I found using gaesessions instead of GAE's webapp2 session and I would like to share with everybody. Here we go:

    1. Download gaesessions from https://github.com/dound/gae-sessions by clicking "Download ZIP" button. The downloaded file is "gae-sessions-master.zip".

    2. Unzip the file (a directory "gae-sessions-master" will be created), and copy the directory "gaessions" to the root directory of your application (i.e., where "app.yaml" is)

    3. Create a file called "appengine_config.py" in the root directory, with the following content (copied form 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. Create a session when a user logs in (variable account is the user's account):

      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. Check if the user's session exists, if yes, return user's account:

      from gaesessions import get_current_session
      def checkSession():
          session = get_current_session()
          if session.is_active():
              return session['account']
          return False
      

    6. Delete the session when the user logs out:

      def logout():
          session = get_current_session()
          if session.is_active():
              session.terminate()
      

    7. Finally, you may create a cron job to clean expired sessions periodically:

    cron.yaml:

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

    Function:

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

    Hope this helps.

    这篇关于GAE webapp2会话:创建和检查会话的正确过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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