String Literal to Blob

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Sun Apr 13 05:03:49 CEST 2008


En Sat, 12 Apr 2008 16:11:20 -0300, Victor Subervi  
<victorsubervi at gmail.com> escribió:
> On Fri, Apr 11, 2008 at 2:05 PM, Steve Holden <steve at holdenweb.com>  
> wrote:
>
>> Then browse to the URL this program serves and you will see the image
>> (assuming you are still sending the image/jpeg content type).
>
> Well, as I mentioned before, I am sending text/html because the page,  
> like
> almost all web pages, has a whole lot more content than just images. Or,
> perhaps you are suggesting I build my pages in frames, and have a frame  
> for
> every image. Unsightly!

(Oh my...!) Thanks to Helloween (the metal band) I was in the right mood  
to write a small example. Simple, bare, raw code: no fancy HTML, no  
templates, no web frameworks, using the embedded sqlite engine. It works  
with Python 2.5 out of the box. Each functional piece is in its own file,  
to emphasize the one-document-per-request rule. It is a photo album, one  
can upload pictures with title and description, and later see them one at  
a time.

How to run: Create a temporary folder. Create a cgi-bin subdirectory, and  
copy the four scripts there. In the parent directory, execute: python -m  
CGIHTTPServer
Using your browser, go to http://localhost:8000/cgi-bin/album.py?init=1  
(the init parameter is to create the database; you can omit it once it's  
done).

Note that there are TWO scripts involved when you want to display a  
picture: showpic.py returns the HTML page, containing tags like this:
<img src='getpic.py?id=%d' alt='%s'>" % (picid, cgi.escape(title))
(note the src attribute) and getpic.py returns the picture itself.

Note also that this is just a demo; for a real web application I would  
choose one of the many existing frameworks; or WSGI, or mod_python,  
finally cgi. And my scripts have absolutely no check for errors at all,  
and the HTML is not even valid.

I hope you finally get the idea of how to serve dynamic content...

-- 
Gabriel Genellina
-------------- next part --------------
A non-text attachment was scrubbed...
Name: upload.py
Type: application/octet-stream
Size: 2325 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20080413/e2283bd4/attachment.obj>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: album.py
Type: application/octet-stream
Size: 1228 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20080413/e2283bd4/attachment-0001.obj>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: getpic.py
Type: application/octet-stream
Size: 499 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20080413/e2283bd4/attachment-0002.obj>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: showpic.py
Type: application/octet-stream
Size: 726 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20080413/e2283bd4/attachment-0003.obj>


More information about the Python-list mailing list