Permalink
Browse files

edit

  • Loading branch information...
1 parent 5170ba1 commit ae59485a6a421c4f1b13b08a599f3a2bf73656e7 @anandology anandology committed with anandology Feb 18, 2008
Showing with 41 additions and 0 deletions.
  1. +41 −0 cookbook/sessions.md
View
@@ -0,0 +1,41 @@
+---
+layout: default
+title: Sessions
+---
+
+# Sessions
+
+### Problem
+
+How to use sessions in web.py.
+
+### Solution
+
+The `web.session` module provides session support. Here is a simple application to count using sessions.
+
+ import web
+ urls = (
+ "/count", "count",
+ "/reset", "reset"
+ )
+ app = web.application(urls, locals())
+ session = web.session.Session(app, web.session.DiskStore('sessions'), initializer={'count': 0})
+
+ class count:
+ def GET(self):
+ session.count += 1
+ return str(session.count)
+
+ class reset:
+ def GET(self):
+ session.kill()
+ return ""
+
+ if __name__ == "__main__":
+ app.run()
+
+The session object is loaded with the session data before handling the request and saves the session data after handling the request, if modified.
+
+The optional `initializer` argument to Session specifies the initial session.
+
+

0 comments on commit ae59485

Please sign in to comment.