separate cgi_main.py module
authorTero Marttila <terom@fixme.fi>
Sat, 14 Feb 2009 18:40:21 +0200
changeset 66 d2ce81dd5e5d
parent 65 9c85c06ba7d5
child 67 4be5aebe0472
separate cgi_main.py module
cgi_main.py
index.cgi
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/cgi_main.py	Sat Feb 14 18:40:21 2009 +0200
@@ -0,0 +1,34 @@
+#!/usr/bin/python2.5
+
+"""
+    Simple WSGI CGI handler using wsgiref.handlers
+"""
+
+import wsgiref.handlers
+
+def error () :
+    """
+        Dumps out a raw traceback of the current exception to stdout, call from except
+    """
+
+    import traceback, sys
+    
+    # HTTP headers
+    sys.stdout.write('Status: 500 Internal Server Error\r\n')
+    sys.stdout.write('Content-type: text/plain\r\n')
+    sys.stdout.write('\r\n')
+    
+    # exception traceback
+    traceback.print_exc(100, sys.stdout)
+
+def run (app) :
+    """
+        Run CGI request
+    """
+
+    # create handler
+    cgi_handler = wsgiref.handlers.CGIHandler()
+
+    # run once
+    cgi_handler.run(app)
+
--- a/index.cgi	Thu Feb 12 23:03:17 2009 +0200
+++ b/index.cgi	Sat Feb 14 18:40:21 2009 +0200
@@ -1,45 +1,29 @@
 #!/usr/bin/python2.5
-# :set filetype=py encoding=utf8
 
 """
-    Sample CGI implementation
+    Sample CGI main() function.
+
+    This example runs site.SiteModuleCollection('sites')
 """
 
-# CGI handler for WSGI
-import wsgiref.handlers
-
-def cgi_error () :
-    """
-        Dumps out a raw traceback of the current exception to stdout, intended for use from except
+def main () :
     """
-
-    import traceback, sys
-
-    print 'Status: 500 Internal Server Error\r'
-    print 'Content-type: text/plain\r'
-    print '\r'
-
-    traceback.print_exc(100, sys.stdout)
-
-def cgi_main () :
-    """
-        Run in CGI mode
+        Build our wsgi.Application and run
     """
 
     try :
-        from qmsk.web import wsgi, site
-
-        # create handler
-        cgi_handler = wsgiref.handlers.CGIHandler()
+        from qmsk.web import wsgi, cgi_main, site
 
         # create app
         app = wsgi.Application(site.SiteModuleCollection('sites'))
         
         # run once
-        cgi_handler.run(app)
+        cgi_main.run(app)
 
     except :
-        cgi_error()
+        # display error on stdout
+        cgi_main.error()
     
 if __name__ == '__main__' :
-    cgi_main()
+    main()
+