在 Python 请求中使用 POST 表单数据上传图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29104107/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Upload Image using POST form data in Python-requests
提问by micheal
I'm working with wechat APIs ... here I've to upload an image to wechat's server using this API http://admin.wechat.com/wiki/index.php?title=Transferring_Multimedia_Files
我正在使用微信 API ...在这里我必须使用此 API 将图像上传到微信服务器 http://admin.wechat.com/wiki/index.php?title=Transferring_Multimedia_Files
url = 'http://file.api.wechat.com/cgi-bin/media/upload?access_token=%s&type=image'%access_token
files = {
'file': (filename, open(filepath, 'rb'),
'Content-Type': 'image/jpeg',
'Content-Length': l
}
r = requests.post(url, files=files)
I'm not able to post data
我无法发布数据
采纳答案by kev
From wechat api doc:
来自微信api文档:
curl -F [email protected] "http://file.api.wechat.com/cgi-bin/media/upload?access_token=ACCESS_TOKEN&type=TYPE"
Translate the command above to python:
将上面的命令翻译成python:
import requests
url = 'http://file.api.wechat.com/cgi-bin/media/upload?access_token=ACCESS_TOKEN&type=TYPE'
files = {'media': open('test.jpg', 'rb')}
requests.post(url, files=files)
回答by Yuki Ishikawa
I confronted similar issue when I wanted to post image file to a rest API from Python (Not wechat API though). The solution for me was to use 'data' parameter to post the file in binary data instead of 'files'. Requests API reference
当我想将图像文件从 Python 发布到 rest API(虽然不是微信 API)时,我遇到了类似的问题。我的解决方案是使用“数据”参数以二进制数据而不是“文件”形式发布文件。 请求 API 参考
data = open('your_image.png','rb').read()
r = requests.post(your_url,data=data)
Hope this works for your case.
希望这适用于您的情况。
回答by Jarek
For Rest API to upload images from host to host:
对于 Rest API 将图像从主机上传到主机:
import urllib2
import requests
api_host = 'https://host.url.com/upload/'
headers = {'Content-Type' : 'image/jpeg'}
image_url = 'http://image.url.com/sample.jpeg'
img_file = urllib2.urlopen(image_url)
response = requests.post(api_host, data=img_file.read(), headers=headers, verify=False)
You can use option verify set to False to omit SSL verification for HTTPS requests.
您可以使用选项验证设置为 False 以省略 HTTPS 请求的 SSL 验证。
回答by Pavel Vergeev
import requests
image_file_descriptor = open('test.jpg', 'rb')
filtes = {'media': image_file_descriptor}
url = '...'
requests.post(url, files=files)
image_file_descriptor.close()
Don't forget to close the descriptor, it prevents bugs: Is explicitly closing files important?
不要忘记关闭描述符,它可以防止错误:明确关闭文件重要吗?
回答by Alex Montoya
Use this snippet
使用这个片段
import os
import requests
url = 'http://host:port/endpoint'
with open(path_img, 'rb') as img:
name_img= os.path.basename(path_img)
files= {'image': (name_img,img,'multipart/form-data',{'Expires': '0'}) }
with requests.Session() as s:
r = s.post(url,files=files)
print(r.status_code)
回答by Savvasenok
client.py
客户端.py
files = {
'file': (
os.path.basename('path/to/file'),
open('path/to/file', 'rb'),
'application/octet-stream'
)
}
requests.post(url, files=files)
server.py
服务器.py
@app.route('/', methods=['POST'])
def index():
picture = request.files.get('file')
picture.save('path/to/save')
return 'ok', 200