![图片[1]-Python如何开发api-uusu优素-乐高,模型,3d打印,编程](http://uusucn.zbbe.cn/wp-content/uploads/2024/01/5e8703373d3ac649.jpg)
Python如何开发api
使用Python开发API,以下是Get方法实现(代码中已经注释明白)
#coding:utf-8
importjson
fromurl.parseimportparse_qs
fromwsgiref.simple_serverimportmake_server
dic_t={"test1":'Hello',##此处定义一个字典
"test2":'Hi'}##用于返回网址中的参数对应值
defapplication(environ,start_response):
start_response('200OK',[('Content-Type','text/html')])
params=parse_qs(environ['QUERY_STRING'])
name=params['name'][0]##得到网址中的参数
try:
dic={name:dic_t[name]}##字典查值并返回为字典
except:
KeyError:dic={name:"KeyError"}##如果字典中没有,则返回‘KeyError’
return[json.dumps(dic)]##网页返回值
if__name__=="__main__":
port=5088##自定义开启的端口
httpd=make_server("0.0.0.0",port,application)
print"servinghttponport{0}...".format(str(port))
httpd.serve_forever()
程序运行后,网址中输入 http://127.0.0.1:5088/?name=test2进行访问,得到对应数据。

API(Application Programming Interface,应用程序接口)是一些预先定义的函数,或指软件系统不同组成部分衔接的约定。 目的是提供应用程序与开发人员基于某软件或硬件得以访问一组例程的能力,而又无需访问原码,或理解内部工作机制的细节。
原文来自:https://www.py.cn© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END


















































暂无评论内容