Python:使用图像文件发布请求

标签 python post request urllib2 urllib

我有一个服务器,我正在尝试构建一个发布请求以取回数据。我认为实现此目的的一种方法是在 header 中添加参数并发出请求。但是我得到的错误很少,我不太了解以继续前进。

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

最佳答案

添加一个标题:

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'

关于Python:使用图像文件发布请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11551268/

相关文章:

python - 绘制方程显示一个圆

python - 使用向后兼容 (MacOS10.9+) 的 MacOS 10.15 (Catalina) 创建 Python C 扩展

javascript - 将 cURL 转换为 JQuery.ajax 请求

forms - Symfony 表单 EntityType::class - 如何使用它来编辑表单中的数据 (3.2.13)

python - 具有分层交叉验证的多个性能指标

python - 如何在html文件中加载css文件已经在django中扩展了另一个html文件(base.html)

ruby - RestClient post请求中传递参数

java - 如何使用 Visual Basic 向需要字符串的服务器发出 POST 请求?

Python HTTP POST 请求发送两次

model-view-controller - 使用 spring 3 注释,jsr303 既没有获取 BindingResult 也没有获取 bean 名称 'dataForm' 的普通目标对象