Python3: requests实现文件上传(对应postman form-data)

#coding:utf-8
from urllib3 import encode_multipart_formdata
import requests

url = "http://127.0.0.1/Pass-01/index.php"
data = {}
headers = {}
filename = 'name.png'  #上传至服务器后,用于存储文件的名称
filepath = r'C:\Users\master\Desktop\pp.jpg'  #当前要上传的文件路径
proxies = {
    "http": "http://127.0.0.1:8080",
    "https": "http://127.0.0.1:8080",
}
####
data['upload_file'] = (filename, open(filepath, 'rb').read())
data['submit']="提交"
encode_data = encode_multipart_formdata(data)
data = encode_data[0]
headers['Content-Type'] = encode_data[1]
# r = requests.post(url, headers=headers, data=data, timeout=5)
r = requests.post(url, headers=headers, data=data, proxies=proxies, timeout=5)
print(r.status_code)

  上面的代码,我已经试验成功。

代码的初始版本来源于:https://blog.csdn.net/u013511989/article/details/80422734

posted on 2020-03-14 22:45  thisismynickname  阅读(1781)  评论(0编辑  收藏  举报

导航