对Python实现简单的API接口实例讲解
作者:IT和尚 时间:2023-11-20 03:27:04
get方法
代码实现
# coding:utf-8
import json
from urlparse import parse_qs
from wsgiref.simple_server import make_server
# 定义函数,参数是函数的两个参数,都是python本身定义的,默认就行了。
def application(environ, start_response):
# 定义文件请求的类型和当前请求成功的code
start_response('200 OK', [('Content-Type', 'text/html')])
# environ是当前请求的所有数据,包括Header和URL,body,这里只涉及到get
# 获取当前get请求的所有数据,返回是string类型
params = parse_qs(environ['QUERY_STRING'])
# 获取get中key为name的值
name = params.get('name', [''])[0]
no = params.get('no', [''])[0]
# 组成一个数组,数组中只有一个字典
dic = {'name': name, 'no': no}
return [json.dumps(dic)]
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()
请求实例
post方法
代码实现
# coding:utf-8
import json
from wsgiref.simple_server import make_server
# 定义函数,参数是函数的两个参数,都是python本身定义的,默认就行了。
def application(environ, start_response):
# 定义文件请求的类型和当前请求成功的code
start_response('200 OK', [('Content-Type', 'application/json')])
# environ是当前请求的所有数据,包括Header和URL,body
request_body = environ["wsgi.input"].read(int(environ.get("CONTENT_LENGTH", 0)))
request_body = json.loads(request_body)
name = request_body["name"]
no = request_body["no"]
# input your method here
# for instance:
# 增删改查
dic = {'myNameIs': name, 'myNoIs': no}
return [json.dumps(dic)]
if __name__ == "__main__":
port = 6088
httpd = make_server("0.0.0.0", port, application)
print "serving http on port {0}...".format(str(port))
httpd.serve_forever()
请求实例
疑问
怎么实现请求的路径限制?
怎么限制接口调用方的headers?
来源:https://blog.csdn.net/u013040887/article/details/78895323
标签:Python,API,接口
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
win10系统下Anaconda3安装配置方法图文教程
2022-08-06 23:01:49
![](https://img.aspxhome.com/file/2023/6/82926_0s.png)
python 获取图片分辨率的方法
2022-02-19 12:03:56
Python通过poll实现异步IO的方法
2023-07-17 04:08:52
微信小程序开发注意指南和优化实践(小结)
2024-04-17 10:38:05
![](https://img.aspxhome.com/file/2023/7/136217_0s.jpg)
SQL server使用自定义函数以及游标
2011-11-03 17:26:27
mysql中如何查看表空间
2024-01-27 00:25:25
![](https://img.aspxhome.com/file/2023/2/112312_0s.png)
分享网站群发站内信数据库表设计
2024-01-14 09:54:28
keras做CNN的训练误差loss的下降操作
2023-09-03 07:41:07
如何通过Django使用本地css/js文件
2022-04-28 22:04:33
asp如何准确地获得一个整数?
2010-07-07 12:25:00
vue中el-table两个表尾合计行联动同步滚动条实例代码
2023-07-02 17:08:56
![](https://img.aspxhome.com/file/2023/0/139950_0s.gif)
SQL Server Table中XML列的操作代码
2011-11-03 17:18:16
使用 Osql 工具管理 SQL Server 桌面引擎 (MSDE 2000)应用介绍
2020-07-01 22:12:59
golang gorm模型结构体的定义示例
2024-04-23 09:35:24
python argparse模块通过后台传递参数实例
2021-05-08 04:13:17
![](https://img.aspxhome.com/file/2023/4/121144_0s.jpg)
Python爬取12306车次信息代码详解
2022-05-19 03:42:00
![](https://img.aspxhome.com/file/2023/3/103333_0s.png)
python select.select模块通信全过程解析
2022-05-23 13:13:10
![](https://img.aspxhome.com/file/2023/3/101493_0s.jpg)
使用Python来编写HTTP服务器的超级指南
2022-12-28 20:40:24
![](https://img.aspxhome.com/file/2023/2/90652_0s.png)
Python中你所不知道的星号 * 用法
2022-08-06 15:25:43
SQL Server从安装到建库为新手寻找捷径
2009-01-13 13:22:00