[Tutor] noob python cgi

Tyler Smith tyler.smith at mail.mcgill.ca
Sun Mar 16 03:35:28 CET 2008


I'm writing a webpage as a learning exercise. My first objective is to
allow myself to upload files to directories in /images/, and have cgi
scripts automatically generate the pages that will allow users to
navigate through the images.

I have a very basic prototype that does what I want, at least for a
website served by Apache on my laptop (not web-accessible). Any
comments or feedback on the sanity of this approach, or ways to
improve it generally would be appreciated! I understand what I've
done, but I have no idea what other approaches there might be. I'd
like to avoid a complicated CMS at this point, as I want to learn
about the basics of serving up webpages.

Three files follow. First, the html index page, followed by the
gallery picker, followed by the thumbnail displayer.



<title>My home page</title>
<p>Only one thing to do - visit the <a href="http://localhost/tycgi-bin/gallery.py">galleries</a>.


gallery picker:
#! /usr/bin/python

import cgitb; 

import cgi, os

print """Content-type: text/html

<H1>Select a gallery:</H1>
<form action="./thumbs.py" method="post">
<select name=gallery>
for gal in os.listdir("../images/"):
   print '<option>%s' % gal

print """<p><input type=submit value="Go">

#! /usr/bin/python

import cgitb; 

import cgi, os

form = cgi.FieldStorage()
gal = form["gallery"].value

print """Content-type: text/html


print '<TITLE>%s</TITLE>' % gal
<H1>%s gallery:</H1>""" % gal

for file in os.listdir("".join(("../images/", gal))):
   print '<p><img src="http://localhost/~tyler/images/%s/%s">' % (gal, file)

print """</BODY></HTML>"""

More information about the Tutor mailing list