How do I serve an image from a python script?

haaserd haaserd at yahoo.com
Thu Jun 7 22:35:07 EDT 2001


Hi,

I am trying to serve an image from a Python script using Apache under
Windows.  My simple HTML statement:

    <img src="ShowPict.py?fn=xxx"  alt="a picture">

...and my simple script:

#!c:/Python20/python.exe
import sys

def pix():
    n = 'c:/My Pictures/Sample.jpg'
    p = open(n,'rb')
    apix = p.read()
    p.close()
    sys.stdout.write("content-type: image/jpeg\n\n")
    sys.stdout.write(apix)
    sys.stdout.flush()

if __name__ == '__main__':
    pix()

As you can see I am ignoring the CGI fields for the moment and am just trying
to serve up an image.  Through debugging statements that I have removed, I
know the program crashes on the statement
    sys.stdout.write(apix)
if the picture is over 200,000 bytes.

On small images (10k), the script runs to completion, but no image appears.
What am I doing wrong?

TIA,

Roger Haase




More information about the Python-list mailing list