Python: 使用图像文件进行POST请求

6

我有一台服务器,想要构建一个POST请求来获取数据。我认为实现这个的一种方式是在头部添加参数并进行请求。但是我遇到了一些错误,我不太理解,无法继续前进。

HTML表单

<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
  </head>
  <body>
     <form method="POST" action="http://some.server.com:61235/imgdigest" enctype="multipart/form-data">
        quality:<input type="text" name="quality" value="2"><br>
        category:<input type="text" name="category" value="1"><br>
        debug:<input type="text" name="debug" value="1"><br>
        image:<input type="file" name="image"><br>
        <input type="submit" value="Submit">
     </form>
  </body>
</html>

Python代码:我已根据答案编辑了问题

import urllib, urllib2
import base64

if __name__ == '__main__':
    page = 'http://some.site.com:61235/'
    with open("~/image.jpg", "rb") as image_file:
        encoded_image = base64.b64encode(image_file.read())
    raw_params = {'quality':'2','category':'1','debug':'0', 'image': encoded_image}
    params = urllib.urlencode(raw_params)
    request = urllib2.Request(page, params)
    request.add_header("Content-type", "application/x-www-form-urlencoded; charset=UTF-8")
    page = urllib2.urlopen(request)
    info = page.info() 

错误:

    page = urllib2.urlopen(request)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 126, in urlopen
    return _opener.open(url, data, timeout)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 406, in open
    response = meth(req, response)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 519, in http_response
    'http', request, response, code, msg, hdrs)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 444, in error
    return self._call_chain(*args)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 378, in _call_chain
    result = func(*args)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 527, in http_error_default
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 404: Not Found

首先,图片需要是一个图片,而不是一个字符串。我建议尝试使用requests,而不是使用urllib、urllib2。http://docs.python-requests.org/en/latest/index.html - sberry
1个回答

5
添加这个标头:
request.add_header("Content-type", "application/x-www-form-urlencoded; charset=UTF-8")

此外,您发送的图像参数是一个字符串,而不是图像文件的内容。您需要对其进行b64编码。
import base64

with open("image.jpg", "rb") as image_file:
    encoded_image = base64.b64encode(image_file.read())

然后在raw_params中使用encoded_image而不是'~/image.jpg'


那么我应该在 request = urllib2.Request(page, params) 后将其添加到头部吗? - add-semi-colons
1
请确保您在Python代码中使用的URL与HTML表单中的操作属性完全相同。看起来您在Python中添加了“.html”。 - Ricardo Villamil
好的,但请注意URL仍然不同,在Python中您有: page = 'http://some.site.com:61235/' 而在HTML中您有: ...<form method="POST" action="http://some.server.com:61235/imgdigest"... Python缺少/imgdigest部分。 - Ricardo Villamil

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接