使用requests的post方法传输文件的几种形式

[python] 2024-05-04 圈点368

摘要:使用requests的post方法传输文件,requests post文件的几种形式

使用requests的post方法传输文件,requests post文件的几种形式。

import requests

#形式1
url = 'http://a.com/dopost'
files = {"files":open('t.xls', 'rb')}
response = requests.post(url,files = files)
print(response.text)
#形式2
url = 'http://a.com/dopost'
files = {'file': ('t.xls', open('t.xls', 'rb'), 'application/vnd.ms-excel', {'Expires': '0'})}
r = requests.post(url, files=files)
r.text
#形式3
url = 'http://a.com/dopost'
files = {'file': ('t.csv', 'bb.csv')}
response = requests.post(url, files=files)
response.text


requests  

感谢反馈,已提交成功,审核后即会显示