使用POST脚本从Python脚本发送文件 [英] Send file using POST from a Python script
本文介绍了使用POST脚本从Python脚本发送文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有一种方法可以使用POST脚本从Python脚本发送文件? 解决方案
从 http://docs.python-requests.org/en/最新/ user / quickstart /#post-a-multipart-encoded-file
请求使上传Multipart编码文件变得非常简单:
>>> ('report.xls','rb')作为f:r = requests.post('http://httpbin.org/post',files = {'report.xls':f})
就是这样。我不是在开玩笑 - 这是一行代码。文件已发送。让我们来看看:
>>> r.text
{
origin:179.13.100.4,
files:{
report.xls:< censored ... binary。 ..data>中
,
form:{},
url:http://httpbin.org/post,
args:{},
header:{
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:
}
Is there a way to send a file using POST from a Python script?
解决方案
From http://docs.python-requests.org/en/latest/user/quickstart/#post-a-multipart-encoded-file
Requests makes it very simple to upload Multipart-encoded files:
>>> with open('report.xls', 'rb') as f: r = requests.post('http://httpbin.org/post', files={'report.xls': f})
That's it. I'm not joking - this is one line of code. File was sent. Let's check:
>>> r.text
{
"origin": "179.13.100.4",
"files": {
"report.xls": "<censored...binary...data>"
},
"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": ""
}
这篇关于使用POST脚本从Python脚本发送文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文