Created
February 24, 2014 20:53
-
-
Save hansent/9196808 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tornado.ioloop | |
import tornado.web | |
import tornado.autoreload | |
class LoginHandler(tornado.web.RequestHandler): | |
def post(self): | |
userid = self.get_argument('userid') | |
password = self.get_argument('password') | |
if (userid == password): | |
self.set_cookie('login', userid) | |
self.redirect("/profile") | |
else: | |
self.redirect("/") | |
class LogoutHandler(tornado.web.RequestHandler): | |
def get(self): | |
self.clear_cookie('login') | |
self.redirect("/") | |
class ProfileHandler(tornado.web.RequestHandler): | |
def prepare(self): | |
self.userid = self.get_cookie('login') | |
if self.userid is None: | |
self.redirect("/") | |
def get(self): | |
self.write("you are logged in as: " + self.userid) | |
class DashboardHandler(ProfileHandler): | |
def get(self): | |
self.write("this is your dashboard: " + self.userid) | |
class SuperSecretHandler(ProfileHandler): | |
def get(self): | |
self.write("Super Secret. " + self.userid + " eyes only!!") | |
application = tornado.web.Application( | |
[ | |
(r"/login", LoginHandler), | |
(r"/logout", LogoutHandler), | |
(r"/profile", ProfileHandler), | |
(r"/dashboard", DashboardHandler), | |
(r"/secret", SuperSecretHandler), | |
(r"/(.*)", tornado.web.StaticFileHandler, | |
{"path": "static", "default_filename": "index.html"}) | |
], debug=True | |
) | |
if __name__ == "__main__": | |
application.listen(8888) | |
io_loop = tornado.ioloop.IOLoop.instance() | |
tornado.autoreload.start(io_loop) | |
io_loop.start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment