360网站兼容模式,网站编辑培训学校,WordPress dux修改,wordpress主题制作全过程新手必看在项目实施过程中需要与其他系统进行接口联调#xff0c;将图像检测的结果传递给其他系统接口#xff0c;进行逻辑调用。这中间的过程可以通过requests库进行实现。
1.安装requests库
pip install requests2.postman 接口测试
我们先通过postman 了解下接口调用#xff0…在项目实施过程中需要与其他系统进行接口联调将图像检测的结果传递给其他系统接口进行逻辑调用。这中间的过程可以通过requests库进行实现。
1.安装requests库
pip install requests2.postman 接口测试
我们先通过postman 了解下接口调用通过postman新增一个接口 新增Collection -- 选中Collection,右键Add request -- 选择请求方式 POST/GET…, 填写URL -- raw -- 选择 JSON 格式 --填写请求数据内容 -- Send, 得到请求结果
通过以上操作我们可以总结出request请求需要 urldata, 明确请求方式。
3. 通过requests库进行json 数据传输
import requests
import jsondef send_data():#urlurl http://www.example.com/api/users#上传data数据json 串data {username: user1, password: password1}#post请求response requests.post(url, jsondata)#以json格式打印返回数据print(json.loads(response.content))4.实例应用
import json
import requests
import datetimedef send_data(url,data):#一般接口调用需要加上请求头文件告诉接口数据传输为json格式headers {Content-Type:application/json}try:#post请求response requests.post(url,data,headersheaders)#判断是否异常响应码是否是200判断网络连接的状态response.raise_for_status()#打印返回数据processed_data json.loads(response.content)print(processed_data,processed_data)except requests.exceptions.RequestException as e:print(error,e)# 异常处理的另一种方式# if response.status_code 200:# processed_data json.loads(response.content)# print(processed_data, processed_data)# else:# print(error, response.status_code)def test():url http://xxx.xx.xx.xx:xxxx/api/v1/xxx/xxxxx #写自己实际的接口路径now_time datetime.datetime.now().strftime(%Y-%m-%d %H:%M:%S) #获取时间wms_data{time: now_time,vehicleid: 192.168.9.201,type: A,code: 5001,}send_data(url, wms_data)