3

I make a server use django and I wanna post a compressed string then decompress the string in django. My OS is Ubuntu14.04 and my python's version is 2.7.6. My django response function like these:

# coding=utf-8

import json
from django.http import HttpResponse
import zlib

def first_page(request):
    result = {
        "title": u"bye"
    }
    try:
        param = request.POST["content"]
        a = param.encode("utf-8")
        param = zlib.decompress(a)
        result["result"] = param
    except Exception, e:
        print "error in line 21"
        print e
    result = json.dumps(result)
    response = HttpResponse(result, content_type="application/json")
    return response

Then I write a test case to test the function,the function's url is "music_main_page", my test code like these:

# coding=utf-8

__author__ = 'lizhihao'


import zlib
import httplib
import urllib

httpClient = None
try:
    a = "hello world! what are you doing!"
    a = zlib.compress(a)
    params = urllib.urlencode(
        {
            "content": a
        }
    )
    headers = {
        "Content-type": "application/x-www-form-urlencoded",
        "Accept": "text/plain"
    }
    httpClient = httplib.HTTPConnection("localhost", 8000, timeout=30)
    httpClient.request("POST", "/music_main_page", params, headers)
    response = httpClient.getresponse()
    print response.read()
except Exception, e:
    print e
finally:
    if httpClient:
        httpClient.close()

The program throw an exception:Error -2 while preparing to decompress data: inconsistent stream state,how to fix the bug?

0

1 Answer 1

1

I bet it has something to do with encoding. Try converting the unicode string you obtain from request.POST["content"] into a byte string before decompression (do .encode('latin-1') instead of .encode('utf-8'), in other words).

This fixed it for me. I was too lazy to reproduce your bug on a full Django project though, I used this to put your string through approximate request parsing stages:

>>> zlib.decompress(
...     bytes_to_text(
...         urlparse.parse_qsl(
...             urllib.urlencode({"content":
...                 zlib.compress("hello world! what are you doing!")
...             })
...         )[0][1].decode('iso-8859-1'), 'utf-8'
...     ).encode('utf-8')
... )

(Where bytes_to_text is this one.)

What do you get if you use browser form instead of a script?


In any case though, perhaps you shouldn’t send compressed data in POSTed form contents. It’s intended for clear unicode text, which is what’s screwing things up from what I can see.

Instead you could just send compressed bytes as-is, and use request.body to read the data and then decompress. Or, even better, set things up so that your server-side gzip compression works.

Sign up to request clarification or add additional context in comments.

2 Comments

I just use this function bytes_to_text(), then I can decompress the string.
I shouldn’t need to call bytes_to_text() because it’s done by Django when you access request.POST["content"]… I only put it there to illustrate what processing form parameters go through when submitted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.