嗨,我将使用自己的 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/