How do I serve an image from a Python Script?

William Park opengeometry at yahoo.ca
Fri Jun 8 17:22:24 EDT 2001


On Fri, Jun 08, 2001 at 01:25:06PM -0700, haaserd wrote:
> 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?

Download a JPEG image that works.  Then, take a look at what kind of
headers you get.  For example, my Apache gives me
    HTTP/1.1 200 OK
    Date: Fri, 08 Jun 2001 21:12:06 GMT
    Server: Apache/1.3.14 (Unix)
    Last-Modified: Wed, 03 Jul 1996 06:18:15 GMT
    ETag: "18621-916-31da10a7"
    Accept-Ranges: bytes
    Content-Length: 2326
    Connection: close
    Content-Type: image/gif

-- 
William Park, Open Geometry Consulting, <opengeometry at yahoo.ca>
8 CPU cluster, (Slackware) Linux, Python, LaTeX, Vim, Mutt, Sc.




More information about the Python-list mailing list