google-app-engine - GAE webapp2 session : the correct process of creating and checking sessions

标签 google-app-engine session webapp2

我试图实现 GAE 的 webapp2 session ,但似乎没有关于它的文档。根据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.在登录处理程序中创建 session

# 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.检查 session 是否存在于我程序的不同位置

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

4.用户注销时删除session

--> not mentioned in the tutorial

我的问题:

  1. 我在 session 创建过程(第 2 步)期间收到错误消息“...对象没有属性‘session’”

  2. 如何在步骤 2 和 4 中删除 session ?

  3. 整个 session 管理流程是否正确?

谢谢。

最佳答案

这是处理程序的示例以及如何使用 webapp2 额外 session

带有 BaseHandler 和 MainHandler 的 main.py

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

关于google-app-engine - GAE webapp2 session : the correct process of creating and checking sessions,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14078054/

相关文章:

php - 使用发布数据设置 session

python - 谷歌应用引擎: how to delete sessions

java - Google App Engine 中的自有关系不起作用

google-app-engine - 平台即服务,可同时处理数万个长期网络连接

google-app-engine - 任务名称在 AppEngine 中的逻辑删除时间有多长?

javascript - Salesforce api session 过期,如何自动重新连接

google-app-engine - 我的应用程序在没有我自己的 SSL 证书的情况下使用 Google App Engine 是否安全

c# - 保持 ASP.NET session 打开/事件

python - 获取完整的原始 http 请求(包含 header 和正文)

python - 在 Google App Engine (python) 中从 Google Cloud Storage 读取文件时出现内存泄漏