python:post接口、发送multipart/form-data格式请求

时间:2024-03-19 11:14:14

抓到的请求如下图:请求包含了文件及键值对

python:post接口、发送multipart/form-data格式请求

# -*- coding: utf-8 -*-
import os
import requests
import time

dirpath = r"D:\***"
filenames = os.listdir(dirpath)

jg = r'D:\***.txt'
fi = open(jg, 'w')
for filename in filenames:
    portion = os.path.splitext(filename)  # 将文件名和缀名分成俩部分
    if portion[1] == '.jpg' or portion[1] == '.png':
        filepath = os.path.join(dirpath, filename)

        # 接口
        url = 'https://***/***'
        files={'topn':(None,'2'),'image_file':('file',open(filepath,'rb'),'image/jpeg')}
        r = requests.post(url,files=files)
        resp = r.json()
        print(resp)

        fi.write(str(filename) + ':' + str(resp) + '\n')
fi.close()
files={'topn':(None,'2'),'image_file':('file',open(filepath,'rb'),'image/jpeg')}中的键对应抓包的form Data中name对应的值