Trying to post multipart form data in python, won't post
Asked Answered
H

2

4

I'm fairly new to python, so I apologize in advance if this is something simple I'm missing. I'm trying to post data to a multipart form in python. The script runs, but it won't post. I'm not sure what I'm doing wrong.

import urllib, urllib2
from poster.encode import multipart_encode
from poster.streaminghttp import register_openers

def toqueXF():
    register_openers()
    url = "http://localhost/trunk/admin/new.php"
    values = {'form':open('/test.pdf'),
              'bandingxml':open('/banding.xml'),
              'desc':'description'}
    data, headers = multipart_encode(values)
    request = urllib2.Request(url, data, headers)
    response = urllib2.urlopen(request)
    the_page = response.read()
    print the_page

When I call this, the print gives me the html of the page, as if I ran "urllib2.urlopen(url)" and didn't post any data:

<form enctype="multipart/form-data" action="" method="post">
    <p><input type="hidden" name="MAX_FILE_SIZE" value="1000000000" /></p>
    <p>Select PDF file to create form from: <input name="form" type="file" /></p>
    <p>(Optional): Select banding XML file: <input name="bandingxml" type="file" /></p>
    <p>Enter description of form: <input name="desc" type="text"/><br/></p>
    <p><input type="submit" value="Upload form" /></p>
</form>

poster is to encode the data to multipart/form data and can be found here: http://atlee.ca/software/poster/index.html

I found the code to use poster here: Using MultipartPostHandler to POST form-data with Python

If anyone's curious, I'm trying to automatically post a pdf and xml banding file after they're generated for queXF (an opensource optical mark recognition software). http://quexf.sourceforge.net/

Holarctic answered 27/7, 2011 at 19:38 Comment(4)
'bandingxml':'/banding.xml' - do you need to use open('/banding.xml') on this line, to match the line above?Lodmilla
I do need the open; however, I've tried it with and without the open. It still doesn't postHolarctic
Did you try adding MAX_FILE_SIZE & its value to your values? Server may be expecting it.Mikimikihisa
I just tried it, no success. I was playing around with it, and I think adding headers['User-Agent'] = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)' fixed it.Holarctic
H
1
import urllib, urllib2
from poster.encode import multipart_encode
from poster.streaminghttp import register_openers

def queXF():
    register_openers()
    url = "http://lilix2/trunk/admin/new.php"
    values = {'form':open('test.pdf'),
          'bandingxml':open('banding.xml'),
          'desc':'description'}
    data, headers = multipart_encode(values)
    headers['User-Agent'] = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
    request = urllib2.Request(url, data, headers)
    request.unverifiable = True
    response = urllib2.urlopen(request)
    the_page = response.read()

Adding headers['User-Agent'] and request.unverifiable = True seems to have fixed it.

Holarctic answered 28/7, 2011 at 15:52 Comment(0)
H
-2

Try using the requests library. Docs on posting multipart file are here: http://docs.python-requests.org/en/latest/user/quickstart/#post-a-multipart-encoded-file

Haskel answered 21/12, 2012 at 7:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.