Re: [tornado] How to serve binary and text data

580 views
Skip to first unread message

Russ Weeks

unread,
Sep 23, 2012, 5:06:28 PM9/23/12
to python-...@googlegroups.com
Hi, Sai,

If you really have to do this, Base64 encoding[1] is probably the easiest.  It will add a 33% overhead to your transfered data (6 bytes of raw data become 8 bytes of encoded data).  Depending on your use case, this could be acceptable.

Alternatively, maybe you could structure your response like:

response = {'text_url': url_of_png_image}

The client would have to make a second request for the image data, but the server could provide the image data with no overhead.

-Russ


On Sun, Sep 23, 2012 at 12:48 PM, Sai Kumar <sai...@gmail.com> wrote:
Hey

I have a tornado http server running, and I have a use case where I need to serve text and binary data (image) in a single response. What is the best way to do that

I create the following dictionary and need to send the following to the client.

response = {'textid' : binary_data_of_png_image}

I tried self.write(response), and it failed saying ( UnicodeDecodeError: 'utf8' codec can't decode byte 0x89 in position 0: invalid start byte ), because image data / binary data is not json encoded.

What is the best way to serve such data, so that it can be read by other clients (clients are not necessarily python clients, it is a objective-c client)

Sai Kumar

unread,
Sep 25, 2012, 1:36:51 AM9/25/12
to python-...@googlegroups.com, rwe...@newbrightidea.com
Hey Russ

Thanks for your reply. Not sure, what the best practice is, when it comes to serving text and binary data, in a response.

But, now that I think more about it, it makes more sense to send {'text_url' : 'url_of_image' or 'filename_of_image'}, and subsequent requests to server would only involve serving binary data, which is image.

Also, what is the preferred practice, when it comes to sending text and binary data. I am sure, it depends, but what is the most commonly used approach

Is it

1. Sending base64 encoded data
or
2. Sending URI and making subsequent requests to get binary data for those URIs

Ronan Amicel

unread,
Sep 25, 2012, 5:07:30 AM9/25/12
to python-...@googlegroups.com, rwe...@newbrightidea.com
Le 25 sept. 2012 à 07:36, Sai Kumar <sai...@gmail.com> a écrit :

> Also, what is the preferred practice, when it comes to sending text and binary data. I am sure, it depends, but what is the most commonly used approach
>
> Is it
>
> 1. Sending base64 encoded data
> or
> 2. Sending URI and making subsequent requests to get binary data for those URIs

The 2nd approach would also allow you to cache images, in case they're reused.

--
Ronan Amicel

Reply all
Reply to author
Forward
0 new messages