#!/usr/bin/env python
"""
Inside - Shows inside a site.
License: GPL 2. Share and enjoy!
Author: Sean B. Palmer, http://purl.org/net/sbp/
"""
import cgitb; cgitb.enable()
import sys, os, itertools, robotparser
# # # # # # # # #
#
Error: no %s found!
\n' % overview) serve(200, content) def main(args=None): if args is None: args = sys.argv[1:] path = os.environ.get('REQUEST_URI') or (base + '/') path = path[len(base):] # Directories to ignore--parse robots.txt robotstxt = os.path.join(site, 'robots.txt') if os.path.exists(robotstxt): rp = robotparser.RobotFileParser() rp.set_url(robotstxt) try: rp.read() # @@ this reads borked files too if not rp.can_fetch('*', path): serve(403, "Forbidden by robots.txt
\n") except IOError: serve(500, "Couldn't parse robots.txt
\n") if path == '/': homepage() elif path == '/misc/feedback': serve(404, "Not found!
") else: path = os.path.join(site, path[1:]) # If it's a directory, try to serve the directory index if os.path.isdir(path): for fn in indexen: filename = os.path.join(path, fn) if os.path.isfile(filename): serve(200, open(filename).read(), 'text/plain') break # @@ not strictly necessary else: serve(404, "Not Found (no CGI in directory)
\n") # Check that it's a .cgi or .py file if any(suffixen, lambda ext: path.endswith(ext)): if os.path.isfile(path): serve(200, open(path).read(), 'text/plain') else: serve(404, "Not Found (%s: not a file)
\n" % path) # See if we can get a valid CGI by adding an extension for ext in suffixen: if os.path.isfile(path + ext): serve(200, open(path + ext).read(), 'text/plain') # See if we can get a valid CGI by trimming a trailing slash # @@ could hook this up with metagen to deliver a canonical URI if path.endswith('/'): for ext in suffixen: if os.path.isfile(path.rstrip('/') + ext): serve(200, open(path.rstrip('/') + ext).read(), 'text/plain') # # Check to see if it's a .htaccess file # # @@ Better to allow them to be served normally? # if path.endswith('/htaccess'): # htaccess = path[:-8] + '.htaccess' # if os.path.isfile(htaccess): # serve(200, open(htaccess).read(), 'text/plain') # Otherwise, we can't find anything we want to serve serve(404, "Not Found
\n") if __name__=="__main__": main()