python - 如何在 GAE 中使用烧杯

标签 python google-app-engine session beaker

嗨,我将使用自己的 session 对象,并且我正在尝试使用 python 应用烧杯。你能告诉我如何将它与谷歌应用引擎一起使用吗?我有以下代码,但我不确定如何继续:

    session_opts = {
        'session.cookie_expires': True,
        'session.type': 'ext:google',
        'session.key': 'mykey.beaker.session.id',
    }

    def main():
        logging.getLogger().setLevel(logging.DEBUG)

        application = webapp.WSGIApplication([(...
... handlers          ],debug=True)
        application = SessionMiddleware(application, session_opts)

        util.run_wsgi_app(application)

最佳答案

作为documentation说:

Once the SessionMiddleware is in place, a session object will be made available as beaker.session in the WSGI environ.

在 Google App Engine 中,您可以通过以下方式从 WebHandler 访问烧杯 session 字典对象:

session = self.request.environ['beaker.session']

session 是 Python dictionary您基本上可以在其中放置数据:

session['somekey'] = 'foo'

或使用以下方式获取数据:

my_var = session['somekey']

一个简单的计数器示例如下:

class MainPage(webapp.RequestHandler):
  def get(self):
    session = self.request.environ['beaker.session']

    if 'counter' in session:
       counter = session['counter'] + 1
       session['counter'] = counter
    else:
       session['counter'] = 1

    self.response.out.write('counter: %d' % counter)

关于python - 如何在 GAE 中使用烧杯,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5935549/

相关文章:

python - python 如何有效地排序时间

python - 需要在App Engine的Python环境中调用ldap

google-app-engine - Mux 和 http.HandleFunc 都适用于 Google App Engine 上的 helloworld 端点

javascript - 意外 session 破坏

php - 数组返回空值

PHP MySQL : remember previous RAND()

python - 如何在 python 中模拟异常调用?

python - Python中nan和 'nan'的区别

python - 如何在 python 中将类似于 [8, 200, 1, 16] 的字节格式转换为类似于 b'\x08\xc8\x01\x10\xc8 的字节格式

google-app-engine - 环境变量 GOOGLE_APPLICATION_CREDENTIALS 必须包含字符串