http post goes into $_REQUEST instead into $_FILES

Piet van Oostrum piet at vanoostrum.org
Fri Aug 16 16:51:36 EDT 2013


cerr <ron.eggler at gmail.com> writes:

> Hi,
>
> I have a Python script that is executing an http POST to transfer a file from the client to the server. I have achieved this with below code:

<snip>

> but my problem is, the data gets posted to the sever but arrives in
> the `$_REQUEST` array and I'm expected to post stuff so that it
> arrives in the `$_FILES` array (this is a LAMP server). How do I need
> to modify my code to deliver it correctly?

MultipartPostHandler only works like that if you give it a real file.

Like:

myfile = open('test.txt')
data = {'file': myfile}
response = opener.open(url, data)

'file' will be the key in the $_FILES array.

If you want to 'fake' a file by supplying your own contents and fake
filename, you can better use the code from Doug Hellmann:
http://pymotw.com/2/urllib2/#uploading-files

If you don't want to use his StringIO trick you could even add a new
method add_fake_file like this:

    def add_fake_file(self, fieldname, filename, body, mimetype='text/plain'):
        """Add a fake file to be uploaded."""
        self.files.append((fieldname, filename, mimetype, body))
        return

and then use it like:

    form.add_fake_file('file', 'test1.txt', 'This is a test.')

-- 
Piet van Oostrum <piet at vanoostrum.org>
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]



More information about the Python-list mailing list