Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@cjgiridhar
Created August 13, 2012 16:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cjgiridhar/3342401 to your computer and use it in GitHub Desktop.
Save cjgiridhar/3342401 to your computer and use it in GitHub Desktop.
Tornado - Authentication
import tornado.ioloop
import tornado.web
class Main(tornado.web.RequestHandler):
def get_current_user(self):
return self.get_secure_cookie("user")
def get(self):
if not self.current_user:
self.redirect("/login")
return
username = self.current_user
self.write('Hi there, '+ username)
class Login(Main):
def get(self):
self.render('auth.html')
def post(self):
self.set_secure_cookie("user", self.get_argument("username"))
self.redirect("/")
application = tornado.web.Application([
(r"/", Main),
(r"/login", Login),
(r"/(style\.css)",tornado.web.StaticFileHandler, {"path": "./css/"}),
],debug=True, cookie_secret="61oETzKXQAGaYdkL5gEmGeJJFuYh7EQnp2XdTP1o/Vo=")
if __name__ == "__main__":
application.listen(8888)
tornado.ioloop.IOLoop.instance().start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment