从
http://docs.python-requests.org/en/latest/user/quickstart/#post-a-multipart-encoded-file
请求使上传多段编码文件变得非常简单:
>>> r = requests.post('http://httpbin.org/post', files={'report.xls': open('report.xls', 'rb')})
而已。我不是在开玩笑 – 这是一行代码。文件已发送。让我们检查:
>>> r.text
{
"origin": "179.13.100.4",
"files": {
"report.xls": ""
},
"form": {},
"url": "http://httpbin.org/post",
"args": {},
"headers": {
"Content-Length": "3196",
"Accept-Encoding": "identity, deflate, compress, gzip",
"Accept": "*/*",
"User-Agent": "python-requests/0.8.0",
"Host": "httpbin.org:80",
"Content-Type": "multipart/form-data; boundary=127.0.0.1.502.21746.1321131593.786.1"
},
"data": ""
}