Reputation: 677
Hi i want send via socket xml like this.
<root>
<image ID='2'>
<![CDATA[ binary data with image ]]>
</image>
</root>
I have problem because image is binary data and other part is string. I am sending multiple images and i need to have ID.
The main problem is what to do with binary data and string data. I tried to convert image to str but i cant revert this.
Upvotes: 1
Views: 1115
Reputation: 28405
Just connect the socket as binary - it is anyway and you probably don't care about newline conversion anyway.
Upvotes: 0
Reputation: 29804
A useful way to embed binary in xml
is base64-encoding it. This is the approach XAML
uses to send small images for example. You may do like this somewhere in your code:
import base64
img = open('some.png',rb').read()
base64.b64encode(img)
# append it to your buffer
And on the other side:
#get the img portion in the buffer
import base64
img = base64.b64decode(fetched_img)
# write it to disk or whatever
This is the standard/usual way to treat binary files inside XML
.
Using base64
is very simple, this is a example in the interpreter:
In [1]: import base64
In [4]: base64.b64encode('example')
Out[4]: 'ZXhhbXBsZQ=='
In [5]: base64.b64decode('ZXhhbXBsZQ==')
Out[5]: 'example'
You can read the docs here.
Hope this helps!
Upvotes: 2