Image serving using python

waldekO osuchw at ecn.ab.ca.no.spam
Wed Oct 4 21:38:14 EDT 2000


Following is a sample I found in some FAQ.  I do not remember which one.
It seems that you mixed up print and sys.stdout.write statements and this
confused your browser.  This script is from a Windows machine that is why I
use -u option at the top.
8<-----------------------------------------------------------
#!python -u
import sys
image = open('mine.gif','rb').read()
sys.stdout.write('Content-type: image/gif\n\n')
sys.stdout.write(image)
8<----------------------------------------------------------------------
waldekO

<jzalesky at my-deja.com> wrote in message news:8rgj92$vio$1 at nnrp1.deja.com...
> I'm trying to use python scripts to serve images on a web server using
> the cgi library interface.  The python script is of the form:
>
> "http://server/directory/getimage.py?imgid=61"
>
> After opening and getting the image into a string variable
> called 'pic', I
> call the following function:
>
> def output_picture(pic)
>     print "Accept-Ranges: bytes"
>     print "Content-Length: ", len(pic)
>     print "Connection: close"
>     print "Content-Type: image/gif"
>     print
>
>     sys.stdout.write(pic)
>
> I know that the picture in 'pic' is correct because 'len(pic)' returns
> the correct length of the file and I've checked its contents against
> the original file.  However the browser does not seem to be displaying
> it at all.
>
> Does anyone know what I might be doing wrong?  If so, let me know.  Or
> if anyone knows of another way to do this, please let me know.
>
> Thanks.
>
> Jon
>
>
> Sent via Deja.com http://www.deja.com/
> Before you buy.





More information about the Python-list mailing list