views:

436

answers:

1

Hi Folks,

I came here via this question: http://stackoverflow.com/questions/68477/send-file-using-post-from-a-python-script

And by and large it's what I need, plus some additional.

Besides the zipfile som additional information is needed and the POST_DATA looks something like this:

POSTDATA =-----------------------------293432744627532
Content-Disposition: form-data; name="categoryID"

1
-----------------------------293432744627532
Content-Disposition: form-data; name="cID"

-3
-----------------------------293432744627532
Content-Disposition: form-data; name="FileType"

zip
-----------------------------293432744627532
Content-Disposition: form-data; name="name"

Kylie Minogue
-----------------------------293432744627532
Content-Disposition: form-data; name="file1"; filename="At the Beach x8-8283.zip"
Content-Type: application/x-zip-compressed

PK........................

Is this somehow possible with the poster 0.4 module (and before you ask, yes, I'm fairly new to Python...)

Kind regards, Brian K. Andersen

+3  A: 

Poster has basic and advanced multipart support.
You may try something like this (modified from poster documentation):

# test_client.py
from poster.encode import multipart_encode
from poster.streaminghttp import register_openers
import urllib2

# Register the streaming http handlers with urllib2
register_openers()

# headers contains the necessary Content-Type and Content-Length
# datagen is a generator object that yields the encoded parameters
datagen, headers = multipart_encode({
    'categoryID' : 1,
    'cID'        : -3,
    'FileType'   : 'zip',
    'name'       : 'Kylie Minogue',
    'file1'      : open('At the Beach x8-8283.zip')
})

# Create the Request object
request = urllib2.Request("http://localhost:5000/upload_data", datagen, headers)

# Actually do the request, and get the response
print urllib2.urlopen(request).read()
Shirkrin