Changeset 8259 in genesis


Ignore:
Timestamp:
Aug 8, 2010, 11:21:39 AM (14 years ago)
Author:
rick
Message:
  • Embedded webserver for easy usage
  • Command line options allowing more easy debugging
File:
1 edited

Legend:

Unmodified
Added
Removed
  • nodes/gformat.py

    r8258 r8259  
    2929def print_title(nodelist):
    3030  """ Main overview page """
    31   items = {'root' : os.environ['SCRIPT_NAME'] }
     31  items = {'root' : "." }
    3232  print """
    3333<html>
     
    4343  <body>
    4444    <center>
    45     <form type="GET" action="">
     45    <form type="GET" action="%(root)s">
    4646      <input type="hidden" name="action" value="update">
    4747      <input type="submit" value="Update Configuration Database (SVN)">
     
    331331  form = cgi.FieldStorage()
    332332  if form.getvalue("action") == "update":
    333     print "Refresh: 5; url=%s" % os.environ['SCRIPT_NAME']
     333    print "Refresh: 5; url=."
    334334    print "Content-type:text/plain\r\n\r\n",
    335335    print "[INFO] Updating subverion, please wait..."
     
    354354    assert False, "Invalid option"
    355355
     356
     357
     358def usage():
     359  print """Usage: %s <standalone [port] |test [test arguments]>
     360Examples:
     361\tstandalone                   =  Run configurator webserver [default port=8000]
     362\ttest CNodeRick dnsmasq.conf  =  Receive output of CGI script
     363\t                                for arguments CNodeRick/dnsmasq.conf
     364"""
     365  exit(0)
     366
     367
     368
    356369# Allow easy hacking using the CLI
    357370if not os.environ.has_key('PATH_INFO'):
    358   os.environ['PATH_INFO'] = "/".join(sys.argv[1:])
    359   os.environ['SCRIPT_NAME'] = __file__
    360 
    361 process_cgi_request()
     371  if len(sys.argv) < 2:
     372    usage()
     373
     374  if sys.argv[1] == "standalone":
     375    import SocketServer
     376    import CGIHTTPServer
     377    try:
     378      PORT = int(sys.argv[2])
     379    except (IndexError,ValueError):
     380      PORT = 8000
     381
     382    class MyCGIHTTPRequestHandler(CGIHTTPServer.CGIHTTPRequestHandler):
     383      """ Serve this CGI from the root of the webserver """
     384      def is_cgi(self):
     385        if "favicon" in self.path:
     386          return False
     387
     388        self.cgi_info = (__file__, self.path)
     389        self.path = ''
     390        return True
     391    handler = MyCGIHTTPRequestHandler
     392    httpd = SocketServer.TCPServer(("", PORT), handler)
     393    httpd.server_name = 'localhost'
     394    httpd.server_port = PORT
     395   
     396    print "serving at port", PORT
     397    httpd.serve_forever()
     398  elif sys.argv[1] == "test":
     399    os.environ['PATH_INFO'] = "/".join(sys.argv[2:])
     400    os.environ['SCRIPT_NAME'] = __file__
     401    process_cgi_request()
     402  else:
     403    usage()
     404else:
     405  process_cgi_request()
Note: See TracChangeset for help on using the changeset viewer.