Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

CGI python 3 write RAW BINARY

474 views
Skip to first unread message

Dodo

unread,
Apr 28, 2010, 5:54:07 PM4/28/10
to
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

Gary Herron

unread,
Apr 28, 2010, 6:08:07 PM4/28/10
to pytho...@python.org


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.


Antoine Pitrou

unread,
Apr 28, 2010, 7:45:59 PM4/28/10
to pytho...@python.org

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.

Dodo

unread,
Apr 29, 2010, 6:53:53 AM4/29/10
to

@Gary : How do I reply to a http request then?

@Antoine : It not sys.stdout.buffer.write but sys.stdout.write()
instead. But it still doesn't work, now I have empty content

#!/usr/bin/python3
import cgi, os, sys
form = cgi.FieldStorage()
all = os.listdir("/some/path/with/files/")
for x in all:
if x.find( form['id'].value ) != -1:
ext = x.split(".")[-1]
print("Content-type:image/%s\n\n" % ext)
f = open("/some/path/with/files/%s" % x, 'rb')
sys.stdout.write( f.read() )
f.close()
break

Dorian

Antoine Pitrou

unread,
Apr 29, 2010, 11:07:55 AM4/29/10
to pytho...@python.org
Le Thu, 29 Apr 2010 12:53:53 +0200, Dodo a écrit :
>
> @Antoine : It not sys.stdout.buffer.write but sys.stdout.write()
> instead. But it still doesn't work, now I have empty content

Let me insist: please use sys.stdout.buffer.write().
You'll also have to call sys.stdout.flush() before doing so.


Dodo

unread,
Apr 29, 2010, 12:33:08 PM4/29/10
to
Oh, I tested on my windows machine avec sys.stdout.buffer.write() didn't
work.
I just tested on my linux server, and it works....

So, let's modify the script

sys.stdout.buffer.write( f.read() )
sys.stdout.flush()

Now I have:
malformed header from script. Bad header=ÿØÿà: show.py
and it tells me Internal Server error :(

I can't see the error messages (traceback)
any idea to do so?

Dorian

Antoine Pitrou

unread,
Apr 29, 2010, 4:21:32 PM4/29/10
to pytho...@python.org
Le Thu, 29 Apr 2010 18:33:08 +0200, Dodo a écrit :
> Oh, I tested on my windows machine avec sys.stdout.buffer.write() didn't
> work.
> I just tested on my linux server, and it works....
>
> So, let's modify the script
>
> sys.stdout.buffer.write( f.read() )
> sys.stdout.flush()

Sorry, I should have been clearer. You have to flush sys.stdout before
using sys.stdout.buffer, such that the unicode layer doesn't keep any
unwritten data. So this should be:

sys.stdout.flush()
sys.stdout.buffer.write( f.read() )

Regards

Antoine.

Dodo

unread,
Apr 29, 2010, 5:37:32 PM4/29/10
to

....I don't get a thing.
Now with the fix :
All browsers shows a different thing, but not the image!
http://ddclermont.homeip.net/misc/python/

If I save it to computer :
* Windows image viewer won't read it
* Irfanview can read it without problems

Dorian

PS : ça commence sérieusement à m'énerver

Dodo

unread,
Apr 29, 2010, 5:43:16 PM4/29/10
to

Ok, I just checked, the CRC of the file in the server is not the same
the in the browser
I don't understand how the data could be modified...

Dorian

Antoine Pitrou

unread,
Apr 30, 2010, 11:52:36 AM4/30/10
to pytho...@python.org
Le Thu, 29 Apr 2010 23:37:32 +0200, Dodo a écrit :
> ....I don't get a thing.
> Now with the fix :
> All browsers shows a different thing, but not the image!
> http://ddclermont.homeip.net/misc/python/
>
> If I save it to computer :
> * Windows image viewer won't read it
> * Irfanview can read it without problems

Did you set the content-type and content-length in the HTTP headers?
Can you post your code?


Dodo

unread,
May 1, 2010, 6:52:01 AM5/1/10
to
I didn't know about content-lenght
Here's the new code (I used a fixed image patch to make sure this is not
the source of the problem)


#!/usr/bin/python3
import cgi, sys, cgitb
cgitb.enable()

f = open("/home/dodo/54.jpg", "rb")
data = f.read()
l = len(data)
f.close()

print("Content-type:image/jpg\nContent-length:%d\n\n" % l)

sys.stdout.flush()
sys.stdout.buffer.write( data )

Dorian

Gabriel Genellina

unread,
May 5, 2010, 1:09:01 AM5/5/10
to pytho...@python.org
En Sat, 01 May 2010 07:52:01 -0300, Dodo <dodo_do_n...@yahoo.fr>
escribi�:

> Le 30/04/2010 17:52, Antoine Pitrou a �crit :
>> Le Thu, 29 Apr 2010 23:37:32 +0200, Dodo a �crit :


>>> ....I don't get a thing.
>>> Now with the fix :
>>> All browsers shows a different thing, but not the image!
>>> http://ddclermont.homeip.net/misc/python/
>>>
>>> If I save it to computer :
>>> * Windows image viewer won't read it
>>> * Irfanview can read it without problems
>>
>> Did you set the content-type and content-length in the HTTP headers?
>> Can you post your code?
>>
>>
> I didn't know about content-lenght
> Here's the new code (I used a fixed image patch to make sure this is not
> the source of the problem)
>
>
> #!/usr/bin/python3
> import cgi, sys, cgitb
> cgitb.enable()
>
> f = open("/home/dodo/54.jpg", "rb")
> data = f.read()
> l = len(data)
> f.close()
>
> print("Content-type:image/jpg\nContent-length:%d\n\n" % l)
>
> sys.stdout.flush()
> sys.stdout.buffer.write( data )

Computers are dumb. You have to speak to them very slow and clearly in
order to be understood :)

You need a space after those ':'. The correct media type for JPEG images
is image/jpeg. And (the important thing) you have one more \n than
necessary. Each header field finishes with \n; an empty line (just \n)
marks the end of all headers; the body [your image] must follow
immediately.

#!/usr/bin/python3
import cgi, sys, cgitb
cgitb.enable()

f = open("/home/dodo/54.jpg", "rb")
data = f.read()
l = len(data)
f.close()

print("Content-Type: image/jpeg\nContent-Length: %d\n" % l)

sys.stdout.flush()
sys.stdout.buffer.write( data )

(Probably, a better way would be to replace all those \n with \r\n, and
not use print at all, but the above code is good enough).

--
Gabriel Genellina

Dodo

unread,
May 7, 2010, 10:33:13 AM5/7/10
to

Anyone?

sam

unread,
May 21, 2010, 5:23:09 AM5/21/10
to

Yes for windows you need to call this before you write it out. And
yes its totally retarded that you have to do this. The whole point of
python is that I don't have to worry about the particular system its
running on.

def __SetStdOutForWindowsToBinaryMode():
# Without this method a windows server will translate '\n'
into '\r\n'
# even for binary output. This probably isn't needed on linux
servers
# so the exception will be caught harmlessly.
# At some point we may have to worry about switching this mode
*back* to original
# (see http://bytes.com/topic/python/answers/30987-cgi-proplem-displaying-image)
try:
import msvcrt,os
msvcrt.setmode( 1, os.O_BINARY ) # 1 = stdout; use 0 for
stdin
except ImportError:
pass


0 new messages