0

I'm working on uploading an mp3 file to a webhost (which i have no control over) and after a related question on StackOverflow, i noticed that the host does support Gzip encoded uploads.

In python, how would i be able to encode my stream (which i get from open(filename)) and send that to the server?

def upSong(fileName):
    datagen, headers = multipart_encode({"mumuregularfile_0": open(fileName, "rb")})

    uploadID = math.floor(random.random()*1000000)
    request = urllib2.Request("http://upload0.mumuplayer.com:443/?browserID=" + browserID + "&browserUploadID=" + str(uploadID), datagen, headers)

    urllib2.urlopen(request).read()

This is the code that does the uploading, being fairly new to python i have no idea how i would tackle a problem like this, but google have provided no answer.

4

1 回答 1

2

看看内置的 gzip 库:文档在这里:http ://docs.python.org/library/gzip.html

您可以避免使用 StringIO 字符串文件对象创建任何额外的文件

也许像这种方法:

import gzip, StringIO

def upSong(fileName):
    with open(filename, 'rb') as f
        stringf = StringIO.StringIO()
        zipper = gzip.GzipFile(mode = "wb", fileobj=stringf)
        zipper.write(f.read())
        zipper.close()
        datagen, headers = multipart_encode({"mumuregularfile_0": stringf)})

        uploadID = math.floor(random.random()*1000000)  
        request = urllib2.Request("http://upload0.mumuplayer.com:443/?browserID=" + browserID + "&browserUploadID=" + str(uploadID), datagen, headers)

        urllib2.urlopen(request).read()

没有测试过,所以可能有错误...

于 2012-06-09T21:31:30.210 回答