CGI python 3 write RAW BINARY
Antoine Pitrou
solipsis at pitrou.net
Wed Apr 28 19:45:59 EDT 2010
Le Wed, 28 Apr 2010 23:54:07 +0200, Dodo a écrit :
> Help! this is driving me crazy lol
> I want to print raw binary data to display an image file BUT
> python3 outputs b'<binary data>' instead of <binary data>.... so the
> browser can't read the image!!
>
> f = open("/some/path/%s" % x, 'rb')
> print(f.read())
print() implicitly converts its arguments to str (i.e. unicode strings)
and then writes them to sys.stdout, which is a text IO wrapper.
If you want to bypass the unicode layer, you have to use
sys.stdout.buffer instead.
That is:
sys.stdout.buffer.write(f.read())
Regards
Antoine.
More information about the Python-list
mailing list