您好,登錄后才能下訂單哦!
這篇文章主要講解了“怎么利用python寫(xiě)api接口”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“怎么利用python寫(xiě)api接口”吧!
導(dǎo)入:import flask,json
實(shí)例化:api = flask.Flask(name)
定義接口訪問(wèn)路徑及訪問(wèn)方式:@api.route(’/index’,methods=[‘get/post/PUT/DELETE’])
定義函數(shù),注意需與路徑的名稱一致,設(shè)置返回類型并支持中文:def index(): return json.dumps(ren,ensure_ascii=False)
三種格式入?yún)⒃L問(wèn)接口:
5.1 url格式入?yún)ⅲ?code>flask.request.args.get(‘id’)
5.2 form-data格式入?yún)ⅲ?code>pwd = flask.request.values.get(‘pwd’)
5.3 josn格式入?yún)ⅲ?code>pwd = flask.request.json.get(‘pwd’)
啟動(dòng)服務(wù):api.run(port=8888,debug=True,host=‘127.0.0.1’)
,開(kāi)啟服務(wù)之后,就可以通過(guò)ip+端口+路徑+入?yún)⒃L問(wèn)接口
#!/usr/bin/python3 # encoding:utf-8 import flask,json # 實(shí)例化api,把當(dāng)前這個(gè)python文件當(dāng)作一個(gè)服務(wù),__name__代表當(dāng)前這個(gè)python文件 api = flask.Flask(__name__) # 'index'是接口路徑,methods不寫(xiě),默認(rèn)get請(qǐng)求 @api.route('/index',methods=['get']) # get方式訪問(wèn) def index(): ren = {'msg':'成功訪問(wèn)首頁(yè)','msg_code':200} #json.dumps 序列化時(shí)對(duì)中文默認(rèn)使用的ascii編碼.想輸出中文需要指定ensure_ascii=False return json.dumps(ren,ensure_ascii=False) #post入?yún)⒃L問(wèn)方式一:url格式參數(shù) @api.route('/article',methods=['post']) def article(): #url格式參數(shù)?id=12589&name='lishi' id = flask.request.args.get('id') if id: if id == '12589': ren = {'msg':'成功訪問(wèn)文章','msg_code':200} else: ren = {'msg':'找不到文章','msg_code':400} else: ren = {'msg':'請(qǐng)輸入文章id參數(shù)','msg_code':-1} return json.dumps(ren,ensure_ascii=False) #post入?yún)⒃L問(wèn)方式二:from-data(k-v)格式參數(shù) @api.route('/login',methods=['post']) def login(): #from-data格式參數(shù) usrname = flask.request.values.get('usrname') pwd = flask.request.values.get('pwd') if usrname and pwd: if usrname =='test' and pwd =='123456': ren = {'msg':'登錄成功','msg_code':200} else: ren = {'msg':'用戶名或密碼錯(cuò)誤','msg_code':-1} else: ren = {'msg':'用戶名或密碼為空','msg_code':1001} return json.dumps(ren,ensure_ascii=False) #post入?yún)⒃L問(wèn)方式二:josn格式參數(shù) @api.route('/loginjosn',methods=['post']) def loginjosn(): #from-data格式參數(shù) usrname = flask.request.json.get('usrname') pwd = flask.request.json.get('pwd') if usrname and pwd: if usrname =='test' and pwd =='123456': ren = {'msg':'登錄成功','msg_code':200} else: ren = {'msg':'用戶名或密碼錯(cuò)誤','msg_code':-1} else: ren = {'msg':'用戶名或密碼為空','msg_code':1001} return json.dumps(ren,ensure_ascii=False) if __name__ == '__main__': api.run(port=8888,debug=True,host='127.0.0.1') # 啟動(dòng)服務(wù) # debug=True,改了代碼后,不用重啟,它會(huì)自動(dòng)重啟 # 'host='127.0.0.1'別IP訪問(wèn)地址
運(yùn)行結(jié)果:
* Serving Flask app 'monitor' (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: on
* Restarting with stat
* Debugger is active!
* Debugger PIN: 991-833-116
* Running on http://127.0.0.1:8888/ (Press CTRL+C to quit)
127.0.0.1 - - [16/Jan/2022 14:05:53] "POST /login?usrname=test&pwd=123456 HTTP/1.1" 200 -
127.0.0.1 - - [16/Jan/2022 14:08:34] "GET /index HTTP/1.1" 200 -
請(qǐng)求方式:
使用postman測(cè)試接口是否可行
如:
url:127.0.0.1:8888/login
參數(shù):usrname=test;pwd=123456
獲取請(qǐng)求參數(shù)的幾種方法:
flask.request.form.get("key", type=str, default=None) 獲取表單數(shù)據(jù), flask.request.args.get("key") 獲取get請(qǐng)求參數(shù), flask.request.values.get("key") 獲取所有參數(shù)。
感謝各位的閱讀,以上就是“怎么利用python寫(xiě)api接口”的內(nèi)容了,經(jīng)過(guò)本文的學(xué)習(xí)后,相信大家對(duì)怎么利用python寫(xiě)api接口這一問(wèn)題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。