Dynamic image creation for the web...

Mike Meyer mwm at mired.org
Sun Aug 28 19:09:20 EDT 2005


Tompa <tompa1969 at yahoo.com> writes:
>> The other thing you may need to check is the HTTP header of the
>> generated image. 
> If possible I'd rather separate the HTTP/HTML-stuff from image creation.
> I'd like to have an HTML file that refers to a py-file that creates images 
> which are returned somehow (sys.stdout or something else in memory) and 
> incorporated within the HTTP-response.

The only relation between HTTP and HTML is that most full pages
retrieved by HTTP are HTML. If you're going to use HTTP to fetch the
image, *something* has to deal with generating the HTTP headers.

The best you can do is write a wrapper that deals with the HTTP
headers then invokes your program to send just the image.

>> It should be possible to create an HTTP response from
>> your create_image.py script (as opposed to just an image) with a MIME
>> type of image/jpeg and manually insert the binary image data in the
>> response body...
> Yes, I believe so too. Something like this, as suggested by Benjamin:
> sys.stdout.write('Status: 200 OK\r\n')
> sys.stdout.write('Content-type: image/gif\r\n')
> sys.stdout.write('\r\n')
> im.save(sys.stdout, "GIF")
>
> But it does not work for some reason!?

You know, it's *really* hard to figure out why something doesn't work
if you don't know what happens when you try it. What happens when you
try fetching the URL for the python script directly? How about if you
telnet to the web server and do a HEAD on that URL? And so on...

       <mike
-- 
Mike Meyer <mwm at mired.org>			http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.



More information about the Python-list mailing list