Python: Post Request with image files
Asked Answered
G

1

6

I have a server and I am trying to build a post request to get the data back. I think one way to achieve this is to add the parameters in the header and make the request. But I am getting few errors that I don't understand well enough to go forward.

Html Form

<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 code: I have edited the question based on the answer

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() 

Errors:

    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
Goar answered 18/7, 2012 at 22:23 Comment(1)
Well, for one thing, the image needs to be an image, not a string. I would suggest trying out requests, instead of using urllib, urllib2. docs.python-requests.org/en/latest/index.htmlAmbroseambrosi
E
5

Add a this header:

request.add_header("Content-type", "application/x-www-form-urlencoded; charset=UTF-8")

Also, the image parameter you're sending is a string, not the contents of the image file. You need to b64 encode it

import base64

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

then use encoded_image instead of '~/image.jpg' in raw_params

Errantry answered 19/7, 2012 at 20:53 Comment(3)
So I should add it to the header after request = urllib2.Request(page, params)?Goar
make sure the url you're using in your python code is exactly the same as action attribute in the html form. Looks like you're adding '.html' in python.Errantry
ok, but notice how the url is still not the same, in python you have: page = 'some.site.com:61235' and in html you have: ...<form method="POST" action="some.server.com:61235/imgdigest"... Python is missing the /imgdigest partErrantry

© 2022 - 2024 — McMap. All rights reserved.