I was wondering if anyone know of any example code or tutorials on implementing a login/signup page in Tornado? Ive seen the examples that come with it, but they seem very facebook/oauth centric.
Tornado is a Python web framework and asynchronous networking library, originally developed at FriendFeed.
Tornado is a Python web framework and asynchronous network library, originally developed at FriendFreed. Tornado uses non-blocking network-io. Due to this, it can handle thousands of active server connections. It is a saviour for applications where long polling and a large number of active connections are maintained.
If using Ubuntu, open a terminal and type python . Then import tornado and the write command tornado. version . You will get output like '4.2.
Here's a simple example handler, which needs a login.html template containing a username/password form. I don't have a sign up example, but it's quite similar, on the post you validate the input and insert the user record rather than authenticating.
class BaseHandler(tornado.web.RequestHandler):
def get_login_url(self):
return u"/login"
def get_current_user(self):
user_json = self.get_secure_cookie("user")
if user_json:
return tornado.escape.json_decode(user_json)
else:
return None
class LoginHandler(BaseHandler):
def get(self):
self.render("login.html", next=self.get_argument("next","/"))
def post(self):
username = self.get_argument("username", "")
password = self.get_argument("password", "")
# The authenticate method should match a username and password
# to a username and password hash in the database users table.
# Implementation left as an exercise for the reader.
auth = self.db.authenticate(username, password)
if auth:
self.set_current_user(username)
self.redirect(self.get_argument("next", u"/"))
else:
error_msg = u"?error=" + tornado.escape.url_escape("Login incorrect.")
self.redirect(u"/login" + error_msg)
def set_current_user(self, user):
if user:
self.set_secure_cookie("user", tornado.escape.json_encode(user))
else:
self.clear_cookie("user")
class LogoutHandler(BaseHandler):
def get(self):
self.clear_cookie("user")
self.redirect(u"/login)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With