Skip to content

Instantly share code, notes, and snippets.

@cjgiridhar
Created September 1, 2012 17:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save cjgiridhar/3580923 to your computer and use it in GitHub Desktop.
Save cjgiridhar/3580923 to your computer and use it in GitHub Desktop.
Tornado - Redis
<html>
<title>
Redis Example
</title>
<body>
<form action="/login" method="post">
Username: <input type="text" name="username">
Password: <input type="text" name="password">
<input type="submit" value="Submit">
</form>
</body>
</html>
import tornado.web
import redis
from hashlib import md5
class Login(tornado.web.RequestHandler):
def initialize(self):
## Initialize redis
self.r = redis.Redis("localhost")
## Add users
self.r.sadd("users", 'bob')
self.r.set('bob', md5('bob').hexdigest())
self.r.sadd("users", 'bob')
self.r.set('clara', md5('clara').hexdigest())
def get(self):
self.render('login.html')
def post(self):
username = self.get_argument("username")
password = self.get_argument("password")
if self.r.sismember("users", username):
if self.r.get(username) == password:
self.write("Login successful!")
else:
self.write("Login Unsuccesful..")
application = tornado.web.Application([
(r"/login", Login)],
debug=True)
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