Python - 接口文件上传
引入Module
import requests,sys,os
import os.path as op
mPath = op.join(op.dirname(__file__),'..','common')
sys.path.append(mPath) #添加模块路径
import excelHandle
from requests_toolbelt import MultipartEncoder
路径拼接
# 定位相对路径的文件 ../../interfaceFiles
filePath = os.path.join(os.path.split(__file__)[0], "..", "..", "interfaceFiles")
处理数据
_key = ['taskIds', 'filename', 'filesPath', '', '', '', '']
_value = ['178626139458322436', 'tempIntergface.xlsx', './tempIntergface.xlsx', '', '', '', '']
k01 = [elem for elem in _key if elem] # 去除空值
dat = dict(zip(k01,_value)) # 准备数据字典
dat01 = dat.copy()
dat01['file'] = (_vFilename, open(fileParh, 'rb'), 'application/octet-stream') # 准备文件数据
dat['form_data'] = MultipartEncoder(fields=dat01) # 准备 被发送的数据
发送请求
head['Content-Type'] = dat['form_data'].content_type
resp = requests.post(url10,data=dat['form_data'],headers=head)
print (resp)
知识点总结
01 相对路径处理
02 参数list合并处理
03 headers 的’Conten-Type’ 替换.