Python 2.6 - Téléverser un fichier zip - Poster 0.4
-
22-07-2019 - |
Question
Je suis venu ici via cette question: Envoyer un fichier à l'aide de POST à ??partir d'un script Python
Et en gros c'est ce dont j'ai besoin, plus quelques autres.
Outre le fichier zip, des informations supplémentaires sont nécessaires et le POST_DATA ressemble à ceci:
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........................
Est-ce possible avec le module poster 0.4 (et avant que vous ne posiez la question, oui, je suis assez novice en Python ...)
Cordialement, Brian K. Andersen
La solution
L’affiche présente un support multipart basique et avancé.
Vous pouvez essayer quelque chose comme ceci (modifié de la documentation du poster):
# 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()
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow