Created
August 26, 2012 12:06
-
-
Save cjgiridhar/3478215 to your computer and use it in GitHub Desktop.
Tornado - Escape - JSON
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> | |
JSON Example | |
</title> | |
<body> | |
<FORM ACTION="/blog" METHOD=POST> | |
Title: <input type="text" name="title"> | |
Author: <input type="text" name="author"> | |
<input type="submit" value="post" name="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 tornado.escape | |
import tornado.httpclient | |
class Blog(tornado.web.RequestHandler): | |
def get(self): | |
self.render('jsonform.html') | |
def post(self): | |
data_json = tornado.escape.json_encode(self.request.arguments) | |
self.write(data_json) | |
## handles client request | |
class Language(tornado.web.RequestHandler): | |
def get(self): | |
data = {'py':'Python', 'js':'JavaScript'} | |
self.set_header('Content-Type', 'text/javascript') | |
self.write(tornado.escape.json_encode(data)) | |
application = tornado.web.Application([ | |
(r"/blog", Blog), | |
(r"/lang", Language), | |
], 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