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?