[Tutor] noob python cgi

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


Hi,

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.

Thanks!

Tyler

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

</body>
</html>

gallery picker:
#! /usr/bin/python

import cgitb; 
cgitb.enable()

import cgi, os

print """Content-type: text/html

<HTML><HEAD>
<TITLE>Galleries</TITLE>
</HEAD>
<BODY>
<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">
</form>
</BODY></HTML>"""

thumbviewer:
#! /usr/bin/python

import cgitb; 
cgitb.enable()

import cgi, os

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

print """Content-type: text/html

<HTML><HEAD>"""

print '<TITLE>%s</TITLE>' % gal
print"""</HEAD>
<BODY>
<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