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

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

问题描述

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

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.配置并添加配置到主应用程序:

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.在登录处理程序中创建会话

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.检查我的程序中的不同位置是否存在会话

3.Check if a session exists at various locations in my program

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

4.用户退出时删除会话

4.Delete session when user logs out

--> not mentioned in the tutorial

我的问题:

  1. 我在会话创建过程中收到错误消息...对象没有属性'会话'"(第 2 步)

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

如何删除第 2 步和第 4 步中的会话?

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

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

Is the overall session management process correct?

谢谢.

推荐答案

这可能不是问题的直接答案,但它是我使用 gaesessions 而不是 GAE 的 webapp2 session 找到的解决方案,我想与大家分享.我们开始:

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. https://github.com/dound/gae-sessions 单击下载 ZIP"按钮.下载的文件是gae-sessions-master.zip".

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

解压文件(会创建gaessions"目录),将gaessions"目录复制到你的应用根目录(即app.yaml"所在的位置)

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)

在根目录创建一个名为appengine_config.py"的文件,内容如下(复制表格https://github.com/dound/gae-sessions/tree/master/demo):

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

  • 在用户登录时创建会话(变量 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
    

  • 检查用户的会话是否存在,如果存在,返回用户的账号:

  • 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
    

  • 在用户注销时删除会话:

  • Delete the session when the user logs out:

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

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

  • 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)
    

    功能:

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

    希望这会有所帮助.

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

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