Tornado的同步API写法举例实现GET/POST/DELETE请求+Tornado获取post请求中的json数据(转载)
下面的實驗主要來自[1][2],但是對實驗2的代碼進(jìn)行了修改,修改過程參考了[3]
#---------------------------------------------------實驗1-------------------------------------------------------------------
from tornado.web import Application, RequestHandler from tornado.ioloop import IOLoopclass HelloHandler(RequestHandler):def get(self):self.write({'message': 'hello world'})def make_app():urls = [("/", HelloHandler)]return Application(urls, debug=True)if __name__ == '__main__':app = make_app()app.listen(3000)IOLoop.instance().start()python test1.py
**************************************************************
GET請求測試:
瀏覽器打開http://127.0.0.1:3000/
GET請求結(jié)果:
#---------------------------------------------------實驗2-----------------獲取post請求中的json數(shù)據(jù)--------------------------------------------------
from tornado.web import Application, RequestHandler from tornado.ioloop import IOLoop import json items = ['csdn']class TodoItems(RequestHandler):def get(self):self.write({'items': items})class TodoItem(RequestHandler):def post(self):# items.append(self.request.body)print("-----------------進(jìn)入post----------------")print(self.request)print("--------------self.write之前------------------")jsonbyte = self.request.bodyjsonstr = jsonbyte.decode('utf8') #解碼,二進(jìn)制轉(zhuǎn)為字符串print('Json字符串:', jsonstr)jsonobj = json.loads(jsonstr) #將字符串轉(zhuǎn)為json對象# self.write({'message': self.request.body})self.write(jsonobj)def make_app():urls = [("/", TodoItems),("/api/item/", TodoItem)]return Application(urls, debug=True)if __name__ == '__main__':app = make_app()app.listen(3000)IOLoop.instance().start()python test2.py
**************************************************************
GET請求測試:
瀏覽器打開:
http://127.0.0.1:3000/
GET請求結(jié)果:
**************************************************************
POST請求測試:
curl -H "Content-Type: application/json" -X POST -d '{"user_id": "123", "coin":100, "success":1, "msg":"OK!" }' "http://127.0.0.1:3000/api/item/"
POST請求結(jié)果:
{"user_id": "123", "coin": 100, "success": 1, "msg": "OK!"}
#----------------------------------------實驗3-----------------------------curl發(fā)送delete請求---------------------------------------------------------
from tornado.web import Application, RequestHandler from tornado.ioloop import IOLoop import jsonitems = []class TodoItems(RequestHandler):def get(self):self.write({'items': items})class TodoItem(RequestHandler):def post(self, _):items.append(json.loads(self.request.body))self.write({'message': 'new item added'})def delete(self, id):global itemsnew_items = [item for item in items if item['id'] is not int(id)]items = new_itemsself.write({'message': 'Item with id %s was deleted' % id})def make_app():urls = [("/", TodoItems),(r"/api/item/([^/]+)?", TodoItem)]return Application(urls, debug=True)if __name__ == '__main__':app = make_app()app.listen(3000)IOLoop.instance().start()python test3.py
***********************************************
GET請求測試:
瀏覽器打開http://127.0.0.1:3000/
GET請求測試結(jié)果:
***********************************************
POST請求測試:
curl -H "Content-Type: application/json" -X POST -d '{"user_id": "123", "coin":100, "success":1, "msg":"OK!" }' "http://127.0.0.1:3000/api/item/342"
POST請求測試結(jié)果:
{"message": "new item added"}
***********************************************
DELETE請求測試:
curl -v -X DELETE 127.0.0.1:3000/api/item/342
DELETE請求測試結(jié)果:
{"message": "Item with id 342 was deleted"}
#---------------------------------------------------------結(jié)束--------------------------------------------------------------------------
Reference:
[1]How to build a REST API in Python with Tornado (part 1)
[2]How to build a REST API in Python with Tornado (part 2)
[3]tornado獲取客戶端以json字符串提交的數(shù)據(jù)
總結(jié)
以上是生活随笔為你收集整理的Tornado的同步API写法举例实现GET/POST/DELETE请求+Tornado获取post请求中的json数据(转载)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。