saving octet-stream png file
Terry Reedy
tjreedy at udel.edu
Fri Aug 19 14:03:30 EDT 2016
On 8/19/2016 1:10 PM, Larry Martell wrote:
> I have some python code (part of a django app) that processes a
> request that contains a png file. The request is send with
> content_type = 'application/octet-stream'
An 'octet' is a byte of 8 bits. So the content is a stream of bytes and
MUST NOT be decoded as unicode text.
> In the python code I want to write this data to a file and still have
> it still be a valid png file.
>
> The data I get looks like this:
>
> u'\ufffdPNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x01\ufffd\x00\x00\x01\ufffd
> ......'
The data you got looked like b'...PNG...' where the *ascii* codes for
"PNG' identify it as a png byte stream. It was mistakenly decoded to
unicode text by something. Png bytes must be decoded, when decoded, to
a png image. You want to write the bytes to a file exactly as received,
without decoding.
> If I try and write that to a file it fails with a UnicodeEncodeError.
> If I write it with encode('utf8') it writes the file, but then it's no
> longer a valid png file.
The data ceased representing a png image as soon as wrongfully decoded
as unicode text.
--
Terry Jan Reedy
More information about the Python-list
mailing list