#! /usr/bin/env python """Starts lighttpd with a give document root. Optional script argument can be specified to run it as fastcgi. Examples: Start Lighttpd with current working dir as document-root. $ lightty code.py http://0.0.0.0:8080 ... Start Lighttpd with run.py as fastcgi. $ lightty code.py -p 9000 -s run.py http://0.0.0.0:9000 ... """ __author__ = "Anand Chitipothu " import sys import os import string from optparse import OptionParser CONF = """ server.modules = ("mod_fastcgi", "mod_rewrite", "mod_accesslog", "mod_redirect") server.document-root = "$root" index-file.names = ( "index.php", "index.html", "index.htm" ) $fastcgi mimetype.assign = ( ".text" => "text/plain", ".txt" => "text/plain", ".css" => "text/css", ".js" => "text/javascript", ".conf" => "text/plain", ".c" => "text/plain", ".py" => "text/plain", ".html" => "text/html", ".htm" => "text/html", ".xml" => "text/xml", ".gif" => "image/gif", ".jpg" => "image/jpeg", ".jpeg" => "image/jpeg", ".png" => "image/png", ".zip" => "application/zip", ".gz" => "application/x-gzip" ) #server.errorlog = "/dev/tty" #accesslog.filename = "/dev/tty" server.event-handler = "freebsd-kqueue" # needed on OS X server.port = $port """ FASTCGI_TEMPLATE = """ fastcgi.server = ( "/$script" => (( "socket" => ".fastcgi.$script.$pid.socket", "bin-path" => "$root/$script", "max-procs" => 5, "bin-environment" => ( "REAL_SCRIPT_NAME" => "" ), "check-local" => "disable" )) ) url.rewrite-once = ( "^/favicon.ico$$" => "/static/favicon.ico", "^/static/(.*)$$" => "/static/$$1", "^/(.*)$$" => "/$script/$$1" ) """ def make_config(root, script, port): pid = os.getpid() if script: fastcgi = string.Template(FASTCGI_TEMPLATE).substitute(locals()) else: fastcgi = "" return string.Template(CONF).substitute(locals()) def setup_path(): paths = ["/usr/sbin", "/usr/local/sbin"] path = os.getenv('PATH') + ":" + ":".join(paths) os.putenv("PATH", path) def main(): options = parse_args() config = make_config(options.root, options.script, options.port) if options.debug: print config sys.exit(0) conf_file = ".lightty_%s.conf" % os.getpid() f = open(conf_file, 'w') f.write(config) f.close() print "http://0.0.0.0:%s/" % options.port print setup_path() os.system("lighttpd -D -f " + conf_file) os.unlink(conf_file) def parse_args(): parser = OptionParser("lightty [-p PORT] [-s SCRIPT] [-d] [document_root]") pwd = os.getcwd() parser.add_option("-p", "--port", dest="port", default=8080, type="int", help="server port (default: %default)") parser.add_option("-s", "--script", dest="script", default=None, help="fastcgi script") parser.add_option("-d", "--debug", action="store_true", dest="debug", default=False, help="print config and exit") (options, args) = parser.parse_args() if args: options.root = args[0] else: options.root = os.getcwd() return options if __name__ == "__main__": main()