Bootstrap

python request file upload_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://www.cnblogs.com/blogs-1024/p/12494942.html

;