Skip to content

Instantly share code, notes, and snippets.

@cjgiridhar
Created August 26, 2012 12:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save cjgiridhar/3478215 to your computer and use it in GitHub Desktop.
Save cjgiridhar/3478215 to your computer and use it in GitHub Desktop.
Tornado - Escape - JSON
<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>
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