POST一个多部分编码文件python

时间:2017-06-16 12:48:01

标签: python post python-requests

在发出POST请求时,我无法从网站获得有用的回复。

import requests

data = {'image':  ("biloha.jpg", open("biloha.jpg", "rb"), 'image/jpeg')}
r = requests.post("http://basic.photofunia.com/categories/all_effects/worker-by-the-billboard/",
                      data=data)
print(r.url)

我得到回应: http://basic.photofunia.com/categories/all_effects/worker-by-the-billboard?e=resource_not_found

但我必须得到这样的东西:

Server: "nginx"
Date: "Fri, 16]un 2017 12:39:12 GMT"
Content-Type: "text/html"
Transfer-Encoding: "chunked"
Connection: "close"
Pragma: "no—cache"
X-Powered-By: "PFEngine/13"
Cache-Control: "no—store"
Expires: "Mon, 01 Jan 1997 12:00:00 GMT"
Location: "/results/5943d170846d7843628b45c7"

1 个答案:

答案 0 :(得分:1)

要发送多部分表单数据,您必须传递files参数,以便上传文件。

import requests
import sys

data = {'image':  ("biloha.jpg", open(sys.argv[1], "rb"), 'image/jpeg')}
r = requests.post("http://basic.photofunia.com/categories/all_effects/worker-by-the-billboard/",
                      files=data)
print(r.url)