Reputation: 828
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.
Upvotes: 0
Views: 257
Reputation: 81
Have a look at the builtin gzip library: documentation is here: http://docs.python.org/library/gzip.html
You can avoid having to create any extra files by using a StringIO string file object
Maybe something like this approach:
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()
Not tested this, so there could be mistakes...
Upvotes: 2