Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.