Python compressed URL post

N

Norman Barker

Hi,

I have spent most of the day on this so any help would be appreciated.

I have set up mod_deflate in Apache so that any input marked
content-type gzip from the client is automatically decompressed before
being forwarded on to the server-side java servlet.

The idea is that I compress a file on the client write it to the stream
and then it reaches the servlet decompressed through Apache. At the
moment when I try to open the URL it fails with an internal server
error, which I guess is because my code is wrong

import httplib, urllib2, StringIO, gzip

httplib.HTTPConnection.debuglevel = 1
filename = 'd:/myproject/schemas/samples/request.xml'
file = open(filename, 'r')

buf = file.read()

print 'decompressed request buffer size is ', len(buf)
zbuf = StringIO.StringIO()
zfile = gzip.GzipFile(mode = 'wb', fileobj = zbuf, compresslevel = 9)
zfile.write(buf);
zfile.close()


print 'compressed request file size is ', len(zbuf.getvalue())


request =
urllib2.Request('http://localhost/cocoon/compressed.xml',zbuf.getvalue())
opener = urllib2.build_opener()
request.add_header('User-Agent', 'A User')
request.add_header('Accept-Encoding', 'gzip, deflate')
request.add_header('Content-Type', 'application/xml')
request.add_header('Content-Encoding', 'gzip')
request.add_header('Content-Length', str(len(zbuf.getvalue())))


## fails here
f = opener.open(request)

compresseddata = f.read()
print 'Compressed response length ', compresseddata.size

compressedstream = StringIO.StringIO(compresseddata)
gzipper = gzip.GzipFile(fileobj=compressedstream)
data = gzipper.read()
print 'Decompressed response length ', len(data)
print data


Many thanks,

Norman Barker
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,828
Latest member
LauraCastr

Latest Threads

Top