Utility classes to make working with appengine easier.
Home Project Page Group Wiki Downloads

Cookie Session

This is an example of use the Cookie Write for sessions. For more information on session, see it's primary page here.

Welcome to the sessions demo. You've viewed this page 1 times during this session. Your session id is There are 5 objects in your session data.

iteration test
  • testKey2
  • 3
  • unicode_key
  • testKey
  • viewCount
  • items test
  • testKey2
  • 3
  • unicode_key
  • testKey
  • viewCount
  • keys test
  • testKey2
  • 3
  • unicode_key
  • testKey
  • viewCount
  • values test
  • test2
  • test3
  • unicode_value
  • test
  • 1

  • You can View this page again, you can Set flash data, pr you can Delete your session and start over.

    Memcache stats: {'hits': 2452L, 'items': 7L, 'bytes': 7826L, 'oldest_item_age': 8470L, 'misses': 5033L, 'byte_hits': 5297871L}

    Session str: {"testKey2" = "test2", "3" = "test3", "unicode_key" = "unicode_value", "testKey" = "test", "viewCount" = "1"}

    webapp class

    class CookieSessionPage(webapp.RequestHandler):
      def get(self):
        self.sess = sessions.Session(writer="cookie")
        if self.request.get('deleteSession') == "true":
            self.sess.delete()
            print "Location: /cookiesession\n\n"
        elif self.request.get('setflash') == "true":
            self.sess['flash'] = 'You set a flash message! <a href="/cookiesession">Refresh this page</a> and this message is gone!'
            print "Location: /cookiesession\n\n"
        else:
            keyname = 'testKey'
            self.sess[keyname] = "test"
            self.sess[keyname + '2'] = "test2"
            self.sess[3] = "test3"
            if not 'viewCount' in self.sess:
                self.sess['viewCount'] = 1
            else:
                self.sess['viewCount'] = int(self.sess['viewCount']) + 1
            self.sess[u"unicode_key"] = u"unicode_value"
            session_length = len(self.sess)
            self.memcacheStats = memcache.get_stats()
            template_values = {
                'sess': self.sess,
                'sess_str': str(self.sess),
                'session_length': session_length,
                'memcacheStats': self.memcacheStats
            }
            path = os.path.join(os.path.dirname(__file__), 'templates/cookie_session-new.html')
            self.response.out.write(template.render(path, template_values))