import cgi
import os, urllib

ROOT = "mybot"

# header
print "text/html"
print

query = os.environ.get("QUERY_STRING")
if not query:
    query = "."

script = os.environ.get("SCRIPT_NAME", "")
if not script:
    script = "cgi-example-1.py"

 
print "<html><head><title>mybot9</title></head>"
print "<STYLE> BODY, H1, H2, H3, H4, P, LI, A, B, TD, TEXTAREA { FONT-FAMILY: ARIAL, SANS-SERIF }</STYLE>"
print "<body>"
print "<p>"
print "<form name='me' method='get' action='' enctype='text/plain'>"
print "mybot"
print "<input type='text' size='60' value=''>&nbsp;<input type='submit' value='Submit'>"
print "</form>"


try:
    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))
    else:
        print "<p>%s" % link

print "</body>"
print "</html>"

import CGIHTTPServer
import BaseHTTPServer

class Handler(CGIHTTPServer.CGIHTTPRequestHandler):
    cgi_directories = ["/cgi"]

PORT = 8000

httpd = BaseHTTPServer.HTTPServer(("", PORT), Handler)
print "serving at port", PORT
httpd.serve_forever()

