Python 2.6 - 上传zip文件 - Poster 0.4
我通过这个问题找到了这里:从Python脚本使用POST发送文件
总体来说,这正是我需要的,另外还有一些额外的信息。
除了zip文件之外,还需要一些额外的信息,POST_DATA看起来像这样:
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........................
请问用poster 0.4模块实现这个有可能吗?(在你问之前,我得说,我对Python还挺陌生的…)
祝好,
Brian K. Andersen
1 个回答
4
Poster这个工具支持基本和高级的多部分内容上传。
你可以试试下面这个例子(这个例子是根据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()