一、需求:
利用python写demo,上传图片,调用接口,并返回数据
二、requests上传图片参数:
res=requests.request("POST",url, data=None, files=files)
三、demo示例:
import json
import osimport requests
import timestr_d = {"b": "b_image", "d": "b_image", "f": "f_image","l": "l_image", "r": "r_image", "u": "t_image"}# 文件夹地址
filepath = r"C:\Users\admin\Desktop\room1"
pathDir = os.listdir(filepath)# 传入的数据
dit = {}
for i, allDir in enumerate(pathDir):for l, d in enumerate(str_d):if allDir[0] == d:path = filepath + "\\" + allDir# 每张图片数据dit[str_d[d]] = (path, open(path, "rb"), "image/png", {})# 测试时间
start = time.process_time()# 调用接口网址:网址是假的,如果使用,请修改网址
url = 'https://baidu.com/panorama/image/'# data如果没有,可以不写
res = requests.post(url, files = dit)
obj = json.loads(res.text)
string = json.dumps(obj, ensure_ascii=False)
end = time.process_time()# 计算接口调用时间
if res.status_code == 200:print(end - start)# 返回数据
print(string)