新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)Python教程:Python如何開發(fā)api
python如何開發(fā)API

使用Python開發(fā)API,以下是Get方法實現(xiàn)(代碼中已經(jīng)注釋明白)
# coding:utf-8
import json
from url.parse import parse_qs
from wsgiref.simple_server import make_server
dic_t = {"test1":'Hello',##此處定義一個字典
"test2":'Hi'}##用于返回網(wǎng)址中的參數(shù)對應值
def application(environ, start_response):
start_response('200 OK', [('Content-Type','text/html')])
params = parse_qs(environ['QUERY_STRING'])
name = params['name'][0]## 得到網(wǎng)址中的參數(shù)
try:
dic = {name: dic_t[name]}##字典查值并返回為字典
except:
KeyError:dic = {name:"KeyError"}##如果字典中沒有,則返回‘KeyError’
return[json.dumps(dic)]## 網(wǎng)頁返回值
if__name__ =="__main__":
port =5088##自定義開啟的端口
httpd = make_server("0.0.0.0", port, application)
print"serving http on port {0}...".format(str(port))
httpd.serve_forever()程序運行后,網(wǎng)址中輸入 http://127.0.0.1:5088/?name=test2進行訪問,得到對應數(shù)據(jù)。
API(Application Programming Interface,應用程序接口)是一些預先定義的函數(shù),或指軟件系統(tǒng)不同組成部分銜接的約定。 目的是提供應用程序與開發(fā)人員基于某軟件或硬件得以訪問一組例程的能力,而又無需訪問原碼,或理解內(nèi)部工作機制的細節(jié)。
更多技術(shù)請關(guān)注Python視頻教程。
文章名稱:創(chuàng)新互聯(lián)Python教程:Python如何開發(fā)api
URL分享:http://fisionsoft.com.cn/article/cdggjog.html


咨詢
建站咨詢
