cgi script to study tarballs

Will Ware wware at alum.mit.edu
Thu Apr 25 00:21:33 EDT 2002


The following is a CGI script that peeks into gzipped tarballs
and lets you study the files in them. I've set this up on my
server and you can waste lotsa CPU on my server by sniffing
around this URL:
http://willware.net:8080/cgi-bin/studycode.py?tarball=http://www.python.org/ftp/python/src/py152.tgz
This script is incredibly slow with large tarballs, but it
does work.

###########################################################
#!/usr/bin/python
import string, sys, os

def ouch(arg):
    print "Content-type: text/html\n"
    print "<HTML><TITLE>Ouch</TITLE>"
    print "<BODY BGCOLOR=\"WHITE\">"
    print "<H1>Ouch: %s</H1>" % repr(arg)
    print "Don't try to call this CGI script directly."
    print "</BODY></HTML>"
    sys.exit(0)

Env = os.environ

try:
    for thing in string.split(Env['QUERY_STRING'], '&'):
        try:
            a, b = string.split(thing, '=')
            Env[a] = b
        except:
            pass

except KeyError:
    ouch(1)

if Env.has_key('tarball'):
    tarball = Env['tarball']
else:
    ouch(2)

if Env.has_key('file'):
    file = Env['file']
else:
    file = None

url = "http://willware.net:8080" + Env["REQUEST_URI"]

if file == None:
    # overview of the whole tarball
    print "Content-type: text/html\n"
    print "<HTML><TITLE>File list for %s</TITLE>" % tarball
    print "<BODY BGCOLOR=\"WHITE\">"
    print "<H1>File list for %s</H1>" % tarball

    print "<ul>"
    inf = os.popen("lynx -source %s | gunzip | tar tf -" % tarball)
    for L in inf.readlines():
        L = L[:-1]
        if L[-1] == "/":
            print '<li>' + L
        else:
            print ('<li><a href="%s&file=%s">%s</a>' %
                   (url, L, L))
    inf.close()
    print "</ul>"

    print "</BODY></HTML>"
    sys.exit(0)

else:
    # overview of the whole tarball
    print "Content-type: text/html\n"
    print "<HTML><TITLE>%s from %s</TITLE>" % (file, tarball)
    print "<BODY BGCOLOR=\"WHITE\">"
    print "<H1>%s <i>from</i> %s</H1>" % (file, tarball)

    cmd = ("lynx -source %s | gunzip | tar -O -x -f - %s" %
           (tarball, file))
    print "<pre>"
    inf = os.popen(cmd)
    i = 1
    print "<pre>"
    for L in inf.readlines():
        print ("%5d " % i) + L[:-1]
        i = i + 1
    print "</pre>"

    print "</BODY></HTML>"
    sys.exit(0)
###########################################################



More information about the Python-list mailing list