Created
September 1, 2012 17:25
-
-
Save cjgiridhar/3580923 to your computer and use it in GitHub Desktop.
Tornado - Redis
This file contains 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
<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> |
This file contains 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.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