简体   繁体   中英

How to use beaker with GAE

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM