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 | items test | keys test | values test |
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"}
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))