Python compressed URL post

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Norman Barker

    #1

    Python compressed URL post

    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.HTTPCon nection.debugle vel = 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.String IO()
    zfile = gzip.GzipFile(m ode = 'wb', fileobj = zbuf, compresslevel = 9)
    zfile.write(buf );
    zfile.close()


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


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


    ## fails here
    f = opener.open(req uest)

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

    compressedstrea m = StringIO.String IO(compressedda ta)
    gzipper = gzip.GzipFile(f ileobj=compress edstream)
    data = gzipper.read()
    print 'Decompressed response length ', len(data)
    print data


    Many thanks,

    Norman Barker

    --
    Take the DOG out to reply
Working...