CGI python 3 write RAW BINARY

Gary Herron gherron at islandtraining.com
Wed Apr 28 18:08:07 EDT 2010


Dodo wrote:
> 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())
>
>
>
> any idea?
> Dorian


Huh???  In what universe does printing raw binary data display an image?

But it that's what you want -- don't use print (which formats objects 
for human reading), and don't use standard output (which is a text 
stream in Python3).   Instead, open your own stream in binary mode, and 
write (not print) to that stream.





More information about the Python-list mailing list