开发者

How to use beaker with GAE

开发者 https://www.devze.com 2023-03-04 03:08 出处:网络
Hi I\'m going to use an own session object and I\'m trying to apply beaker with python. Can you tell me how to use it with google ap开发者_Go百科p engine? I\'ve got the following code and then I\'m un

Hi I'm going to use an own session object and I'm trying to apply beaker with python. Can you tell me how to use it with google ap开发者_Go百科p engine? I've got the following code and then I'm unsure how to proceed:

    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)


As the documentation says:

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

In Google App Engine you can access the beaker session dictonary object from a WebHandler with:

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

the session is a Python dictionary where you can basically put data with:

session['somekey'] = 'foo'

or get data using:

my_var = session['somekey']

A simple Counter example would be something like this:

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号