From: Dodo on
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 )



Dorian
From: Gabriel Genellina on
En Sat, 01 May 2010 07:52:01 -0300, Dodo <dodo_do_not_wake_up(a)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

From: Dodo on
Le 01/05/2010 12:52, Dodo a écrit :
> 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 )
>
>
>
> Dorian
Anyone?
From: sam on
On May 7, 7:33 am, Dodo <dodo_do_not_wake...(a)yahoo.Fr> wrote:
> Le 01/05/2010 12:52, Dodo a écrit :
>
>
>
>
>
> > 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 )
>
> > Dorian
>
> Anyone?

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