Trouble sending / receiving compressed data (using zlib) as HTTP POSTto server (in django)

S

subeen

Hi,

I am trying to send compressed data to a server written in django. But
it shows error while decompressing the data in the server. After some
experiment I found that the server is not receiving the exact data I
am sending.

data = 'hello, this is a test message this is another message'
data = zlib.compress(data)
# now it looks like: x��H����Q(��,V�D�����������T�p^~IFjL�e
# length is 45

in django (view) I receive it:
data = request.POST['data']
# now it looks like: x����Q(�,V�D�������^~IFjL�e
# length is 34

Can anybody help me understand the issue and how to get over?


thanks,
Subeen.
http://love-python.blogspot.com
 
P

Piet van Oostrum

subeen said:
s> Hi,
s> I am trying to send compressed data to a server written in django. But
s> it shows error while decompressing the data in the server. After some
s> experiment I found that the server is not receiving the exact data I
s> am sending.
s> data = 'hello, this is a test message this is another message'
s> data = zlib.compress(data)
s> # now it looks like: x��H����Q(��,V�D����.�������T�p^~IFj.L.�.e
s> # length is 45

Note: you can't just paste binary data in the message and expect
something sensible. Better use the result of 'print data'.
s> in django (view) I receive it:
s> data = request.POST['data']
s> # now it looks like: x����Q(�,V�D���.����^~IFj.L.�.e
s> # length is 34
s> Can anybody help me understand the issue and how to get over?

How did you post the data? If you post binary data you should indicate
this with a proper mime type, like application/octet-stream. Otherwise
it might be interpreted as text which it isn't.
 
S

subeen

Thanks for your response.
How did you post the data? If you post binary data you should indicate
this with a proper mime type, like application/octet-stream. Otherwise
it might be interpreted as text which it isn't.
--
I am trying to send the data using following code:
....
opener = urllib2.build_opener()

opener.addheaders = [

('User-Agent', 'python'),
('Content-Type', 'application/octet-stream'),
]
data = zlib.compress(data)
params = urllib.urlencode({'uid': uid, 'reqid': rid, 'data':
data})
usock = opener.open(url, params)
resp = usock.read()

usock.close()
....

regards,
Subeen.
 

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,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top