Python接口開發(fā)實(shí)現(xiàn)步驟詳解
一、操作步驟
1. 導(dǎo)入:import flask,json2. 實(shí)例化:api = flask.Flask(__name__) 3. 定義接口訪問路徑及訪問方式:@api.route(’/index’,methods=[’get/post/PUT/DELETE’]) 4. 定義函數(shù),注意需與路徑的名稱一致,設(shè)置返回類型并支持中文:def index(): return json.dumps(ren,ensure_ascii=False)5. 三種格式入?yún)⒃L問接口:5.1 url格式入?yún)ⅲ篺lask.request.args.get(’id’)5.2 form-data格式入?yún)ⅲ簆wd = flask.request.values.get(’pwd’)5.3 josn格式入?yún)ⅲ簆wd = flask.request.json.get(’pwd’)6. 啟動(dòng)服務(wù):api.run(port=8888,debug=True,host=’127.0.0.1’),開啟服務(wù)之后,就可以通過ip+端口+路徑+入?yún)⒃L問接口
二、源碼舉例
#!/usr/bin/python3# encoding:utf-8import flask,json# 實(shí)例化api,把當(dāng)前這個(gè)python文件當(dāng)作一個(gè)服務(wù),__name__代表當(dāng)前這個(gè)python文件api = flask.Flask(__name__) # ’index’是接口路徑,methods不寫,默認(rèn)get請求 @api.route(’/index’,methods=[’get’]) # get方式訪問def index(): ren = {’msg’:’成功訪問首頁’,’msg_code’:200} #json.dumps 序列化時(shí)對中文默認(rèn)使用的ascii編碼.想輸出中文需要指定ensure_ascii=False return json.dumps(ren,ensure_ascii=False)#post入?yún)⒃L問方式一: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’:’成功訪問文章’,’msg_code’:200} else: ren = {’msg’:’找不到文章’,’msg_code’:400} else: ren = {’msg’:’請輸入文章id參數(shù)’,’msg_code’:-1} return json.dumps(ren,ensure_ascii=False)#post入?yún)⒃L問方式二: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問方式二: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訪問地址
運(yùn)行結(jié)果
* Serving Flask app 'restful' (lazy loading)* Environment: productionWARNING: 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: 249-915-285* Running on http://127.0.0.1:8888/ (Press CTRL+C to quit)
三、postman訪問接口
get方式,無參數(shù)訪問接口
post方式,url格式入?yún)⒃L問接口
post方式,form-data格式入?yún)⒃L問接口
post方式,josn格式入?yún)⒃L問接口
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. asp畫中畫廣告插入在每篇文章中的實(shí)現(xiàn)方法2. 讓chatgpt將html中的圖片轉(zhuǎn)為base64方法示例3. 使用純HTML的通用數(shù)據(jù)管理和服務(wù)4. ASP編碼必備的8條原則5. asp中Request.ServerVariables的參數(shù)集合6. WMLScript腳本程序設(shè)計(jì)第1/9頁7. CSS Hack大全-教你如何區(qū)分出IE6-IE10、FireFox、Chrome、Opera8. PHP反序列化漏洞實(shí)例深入解析9. ASP基礎(chǔ)知識Command對象講解10. PHP session反序列化漏洞深入探究
