Python 2.6中 - 上传zip文件 - 海报0.4
-
22-07-2019 - |
题
我来到这里,通过这个问题: 使用POST从Python脚本发送文件
和总的来说这是我所需要的,再加上一些额外的。
除了需要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........................
这是某种程度上可能与海报0.4模块(你问之前,是的,我是相当新的Python的...)
此致 布赖恩K.安德森
解决方案
海报具有基本和高级多部分支撑。结果 你可以尝试这样的事情(从海报文档修改):
# 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()
不隶属于 StackOverflow