The cgi module

This module provides a number of support functions and classes for common gateway interface (CGI) scripts. Among other things, it can parse CGI form data.

Here’s a simple CGI script that returns a list of a files in a given directory (relative to the root directory specified in the script).

Example: Using the cgi module
# File:

import cgi
import os, urllib

ROOT = "samples"

# header
print "text/html"

query = os.environ.get("QUERY_STRING")
if not query:
    query = "."

script = os.environ.get("SCRIPT_NAME", "")
if not script:
    script = ""

print "<html>"
print "<head>"
print "<title>file listing</title>"
print "</head>"
print "</html>"

print "<body>"

    files = os.listdir(os.path.join(ROOT, query))
except os.error:
    files = []

for file in files:
    link = cgi.escape(file)
    if os.path.isdir(os.path.join(ROOT, query, file)):
        href = script + "?" + os.path.join(query, file)
        print "<p><a href='%s'>%s</a>" % (href, cgi.escape(link))
        print "<p>%s" % link

print "</body>"
print "</html>"


<title>file listing</title>
<p><a href=''>web</a>

A Django site. rendered by a django application. hosted by webfaction.