1.
from tornado.web import Application, RequestHandler
from tornado.ioloop import IOLoop
import json
class IndexHandler(RequestHandler):
def get(self):
print (self.request)
json_str = {"username": "admin", "password": "123123"}
self.write(json.dumps(json_str))
if __name__ == "__main__":
app = Application([(r"/", IndexHandler)])
app.listen(8000)
IOLoop.current().start()
在浏览器输入127.0.0.1得到结果
{"username": "admin", "password": "123123"}
2. tornado 接收post请求,发送json内容 实现代码如下:
# -*- coding:utf-8 -*- from tornado.web import Application, RequestHandler from tornado.ioloop import IOLoop from tornado.escape import json_decode class IndexHandler(RequestHandler): def post(self,*args,**kwargs): data=json_decode(self.request.body) self.write(data) if __name__ == "__main__": app = Application([(r"/", IndexHandler)]) app.listen(8000) IOLoop.current().start()
默认的ip:port 为127.0.0.1:8000