本篇文章為大家展示了如何在flask應用中使用多個http頭并借助PUT實現(xiàn)POST提交數(shù)據(jù),內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
公司主營業(yè)務:做網(wǎng)站、成都網(wǎng)站制作、移動網(wǎng)站開發(fā)等業(yè)務。幫助企業(yè)客戶真正實現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競爭能力。創(chuàng)新互聯(lián)建站是一支青春激揚、勤奮敬業(yè)、活力青春激揚、勤奮敬業(yè)、活力澎湃、和諧高效的團隊。公司秉承以“開放、自由、嚴謹、自律”為核心的企業(yè)文化,感謝他們對我們的高要求,感謝他們從不同領域給我們帶來的挑戰(zhàn),讓我們激情的團隊有機會用頭腦與智慧不斷的給客戶帶來驚喜。創(chuàng)新互聯(lián)建站推出連云免費做網(wǎng)站回饋大家。
實驗目的:在flask應用中使用多個http頭并借助PUT,POST提交數(shù)據(jù)
源代碼:
__author__ = 'hochikong' from flask import Flask,request from flask.ext.restful import Resource,Api,reqparse app = Flask(__name__) api = Api(app) todos = {'task':'get the list of docker'} parser = reqparse.RequestParser() parser.add_argument('name',type=str,help='get the name') parser.add_argument('auth-token',type=str,help='put the token here',location='headers') class TodoSimple(Resource): def get(self,todo_id): args = parser.parse_args() if args['auth-token'] == 'thisismytoken': return {todo_id:todos[todo_id]} else: return {'error':'token error'},401 def put(self,todo_id): args = parser.parse_args() if args['auth-token'] == 'thisismytoken': todos[todo_id] = request.json.get('data') return todos,201 else: return {'status':'error'},401 class GetName(Resource): def post(self): args = parser.parse_args() name = args['name'] #name = {} #name['ac'] = args['name'] #name = request.json.get('name') return {'yourame':name} api.add_resource(TodoSimple,'/todo/<string:todo_id>') api.add_resource(GetName,'/getname') if __name__ == '__main__': app.run(debug=True)
測試的核心是TodoSimple類,測試的是PUT方法
測試工具由單純的curl換成了firefox的REST client(話說,直接用curl我也真沒什么耐心去用),安裝firefox擴展參考此:http://www.blogjava.net/paulwong/archive/2014/04/19/412688.html
設計要求:要在HTTP請求中,包含content-type:application/json和自定義的auth-token頭,另外請求體是一個json文檔,flask應用應該懂得處理其中的數(shù)據(jù)
代碼分析:
class TodoSimple(Resource): def get(self,todo_id): args = parser.parse_args() if args['auth-token'] == 'thisismytoken': return {todo_id:todos[todo_id]} else: return {'error':'token error'},401 def put(self,todo_id): args = parser.parse_args() if args['auth-token'] == 'thisismytoken': todos[todo_id] = request.json.get('data') return todos,201 else: return {'status':'error'},401
從我前一篇blog中,我知道可以用reqparse解析json數(shù)據(jù),在TodoSimple類中,解析auth-token頭是使用reqparse進行的,但我們要在前面定義該參數(shù):
parser.add_argument('auth-token',type=str,help='put the token here',location='headers')
如果我們不使用reqparse解析,我們可以用flask自帶的request模塊解析:
todos[todo_id] = request.json.get('data')
而我的請求是這樣的:
在這個工具中,Body不用再像在curl中那樣在花括號外還要加引號,定義http頭也方便多了。
通過request.json.get(),flask應用就可以解析多個http頭和json數(shù)據(jù)了。
對應的curl請求為:
hochikong@hochikong-P41T-D3:~$ curl -i -X PUT -H 'Content-Type:application/json' -H 'auth-token:thisismytoken' -d '{"data":"hello world"}' http://localhost:5000/todo/abc HTTP/1.0 201 CREATED Content-Type: application/json Content-Length: 68 Server: Werkzeug/0.10.1 Python/2.7.6 Date: Sat, 18 Apr 2015 15:10:46 GMT { "abc": "hello world", "task": "get the list of docker" }
好,這次實驗基本成功,下一步就是把各種資源分布在不同的python文件中,組織好代碼的放置
研究過程中遇到的坑:
啟用debug=True后,啟動flask應用,提示錯誤:
ImportError: No module named _winreg
后來參考這位仁兄的文章修改了six.py(http://www.cnblogs.com/lvzwq/p/4267850.html),
改成這樣:
if attr in ("__file__", "__name__") and self.mod not in sys.modules: # raise AttributeError raise AttributeError(attr) try: _module = self._resolve() except ImportError: raise AttributeError(attr) # _module = self._resolve() value = getattr(_module, attr) setattr(self, attr, value) return value
于是就沒問題了,這是一個bug
上述內(nèi)容就是如何在flask應用中使用多個http頭并借助PUT實現(xiàn)POST提交數(shù)據(jù),你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
分享名稱:如何在flask應用中使用多個http頭并借助PUT實現(xiàn)POST提交數(shù)據(jù)
本文地址:http://muchs.cn/article34/pdgdse.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供定制網(wǎng)站、網(wǎng)站收錄、、面包屑導航、Google、ChatGPT
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)