I'm new to Tornado and web services in general. In my application, I've Qt/c++ client and python Tornado on server side. The Qt client sends commands in the form of text messages. On server side the 'on_message' method receives the message, parses it and calls the relevant script to generate .png image. Now, I want to send this image back to client along with the short description of the image. How do I do this on server and client side? Pointer to any online example would be also helpful. Thanks.
Asked
Active
Viewed 3,047 times
1 Answers
5
You can encode the image into Base64 format and send the message in JSON format, together with the description.
On your server you do
import base64
ws_client.write_message({
"img": base64.b64encode(img_data),
"desc": img_description,
})
And on your client, you parse the JSON string and decode the Base64 encoded image to get the data.
-
Thanks for the reply. I have Qt/C++ on client side. How do I decode this on client side? – gaj Oct 09 '15 at 09:15
-
I am not that familiar with Qt or C++, but it seem Qt has a [JSON parser](http://doc.qt.io/qt-5/qjsondocument.html#fromJson) and you can get a Base64 decoding function [here](http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c). – gre_gor Oct 09 '15 at 09:37