当前位置: 首页 > news >正文

视觉学习(七)---Flask 框架下接口调用及python requests 实现json字符串传输

在项目实施过程中需要与其他系统进行接口联调,将图像检测的结果传递给其他系统接口,进行逻辑调用。这中间的过程可以通过requests库进行实现。

1.安装requests库
pip install requests
2.postman 接口测试

我们先通过postman 了解下接口调用,通过postman新增一个接口:
新增Collection --> 选中Collection,右键Add request --> 选择请求方式 POST/GET…, 填写URL --> raw --> 选择 JSON 格式 -->填写请求数据内容 --> Send, 得到请求结果
在这里插入图片描述

通过以上操作,我们可以总结出:request请求需要 url,data, 明确请求方式。

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, json=data)#以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,headers=headers)#判断是否异常,响应码是否是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)
http://www.lryc.cn/news/126021.html

相关文章:

  • unity编写树形结构的文件管理页面
  • 基于单片机的家用智能浇灌系统
  • Solr的入门使用
  • css鼠标样式 cursor: pointer
  • 【解决】Kafka Exception thrown when sending a message with key=‘null‘ 异常
  • 中心极限定理 简明教程
  • 商城-学习整理-基础-库存系统(八)
  • 【C++ 学习 ⑬】- 详解 list 容器
  • 设计模式十五:命令模式(Command Pattern)
  • FPGA GTP全网最细讲解,aurora 8b/10b协议,HDMI视频传输,提供4套工程源码和技术支持
  • 用dcker极简打包java.jar镜像并启动
  • 设计模式——创建型
  • iTOP-i.MX8M开发板添加USB网络设备驱动
  • 分类预测 | MATLAB实现GAPSO-LSSVM多输入分类预测
  • JMeter 的并发设置教程
  • 数据治理有哪些产品
  • windows安装go,以及配置工作区,配置vscode开发环境
  • 第五章nginx负载均衡
  • MATLAB计算一组坐标点的相互距离(pdist、squareform、pdist2函数)
  • 我国农机自动驾驶系统需求日益增长,北斗系统赋能精准农业
  • 防雷检测行业应用完整解决方案
  • 16.4 【Linux】特殊文件与程序
  • qrcode.react生成二维码
  • ETF套利及交易者如何进行套利的
  • 了解异或的好处和用途
  • vue函数式组件
  • Idea Live Template 功能总结
  • 场景入门12----构造脚本搭建栅栏和石头墙
  • 零基础看懂免费开源的Stable Diffusion
  • Zmq适配Win7 SP0 / Win XP/ Win 2k