""" Minimal CGI publisher --- Magnus Lie Hetland, 1999 Usage: In a python script running as a cgi-program, import the publish- function: from MinimalCGIPublisher import publish And at the end of the script, call the publish() function. Arguments: - function: Optional argument showing which function will be called if none is present in the URL (see below) - arglist: Positional parameters (strings) restricting which arguments from the URL will be passed on. - named: Named parameters. Only the parameter "type" is used, if present. It gives the MIME type of the result string from the published function. (All published functions should return a string, representing the resulting document.) If the module "egg.cgi" with the method "ni" is published at www.spam.org it can be called like this: http://www.spam.org/egg.cgi If no method was given in the call to publish(), we could call it like this: http://www.spam.org/egg.cgi/ni Named parameters could be added thus: http://www.spam.org/egg.cgi/ni?x=1&y=2 If more arguments are given than the function expects, and no arglist is given in the call to publish(), an exception will occur. In that case a minimal error page will be displayed. """ import cgi, os def publish(function=None,*arglist,**named): params = cgi.FieldStorage() paramdict = {} for key in params.keys(): if not arglist or key in arglist: paramdict[key] = params[key].value if os.environ.has_key('PATH_INFO'): import __main__ function = getattr(__main__,os.environ['PATH_INFO'][1:]) try: result = apply(function, (), paramdict) except: print "Content-type: text/html\n" print "
" import sys, traceback sys.stderr = sys.stdout traceback.print_exc() else: type = named.has_key("type") and named["type"] or "text/html" print "Content-type: %s\n" % type print str(result)