Python 2.6 - 上传zip文件 - 海报0.4

时间:2009-06-25 06:43:29

标签: python upload file-upload urllib2 zipfile

我是通过这个问题来到这里的: Send file using POST from a Python script

总的来说,这就是我所需要的,还有一些额外的东西。

除了zipfile之外,还需要附加信息,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........................

海报0.4模块是否可以实现这一点(在你问之前,是的,我对Python很新......)

亲切的问候, Brian K. Andersen

1 个答案:

答案 0 :(得分:4)

海报具有基本和先进的多部分支持 您可以尝试这样的事情(从海报文档修改):

# 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()