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

上一篇:20192111 实验三《Python程序设计》实验报告


下一篇:Redis单机版以及集群版的安装搭建以及使用