python - requests上传文件MultipartEncoder

1. requests_toolbelt

先安装requests_toolbelt

pip install requests_toolbelt

上传实例

import requests
from requests_toolbelt import MultipartEncoder

upload_url = 'https://your/upload/url'
payload = {
    'file': ('test.jpg', open('test.jpg', 'rb'), 'image/jpeg')
}
m = MultipartEncoder(payload)

headers = {
    'Content-Type': m.content_type,
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36',
}

res = requests.post(url, headers=headers, data=m, verify=False)
print(res.json())
posted @ 2023-11-29 10:28  wstong  阅读(417)  评论(0编辑  收藏  举报