I would like to send images with socketserver of python 3 and I have a problem of encoding. Do I need to send it in bytes or in text? And do I need to open my image with the 'b' option and to transform it in a particular format?
With previous version of python I could do :
image = open('my_image.jpg', 'rb')
image_data = image.read()
image.close()
socketData = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socketData.connect((ADDRESS,PORT))
socketData.sendall(image_data)
But with python 3, it doesn't work anymore. There are encoding and codecs problems and I don't really understand the using of encode(), decode() or str().
Do you have any idea how I can implement it?
Thanks
Like I wrote above, your code works as-is currently. To add a header to the data that you want to send over the socket, you first need to know its encoding. Assuming that it is UTF-8, you can do the following:
header = "Header!\n"
header_bytes = bytes(header, 'utf-8')
socketData.sendall(header_bytes + image_data)
bytes
type can only be concatenated with bytes
in Python 3.