Pregunta

Vine aquí a través de esta pregunta: Enviar archivo usando POST desde un script de Python

Y, en general, es lo que necesito, más algunos adicionales.

Además del archivo zip, se necesita información adicional y el POST_DATA se parece a esto:

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........................

¿Es esto de alguna manera posible con el módulo póster 0.4 (y antes de preguntar, sí, soy bastante nuevo en Python ...)

Saludos cordiales, Brian K. Andersen

¿Fue útil?

Solución

Poster tiene soporte multiparte básico y avanzado.
Puede intentar algo como esto (modificado de la documentación del póster):

# 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()
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top