Created
June 17, 2015 07:41
-
-
Save erantapaa/c2280b555a3d071d500c to your computer and use it in GitHub Desktop.
start a static web server with lighttpd
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# | |
# Provision a static lighttpd server in $HOME/.lighttpd/config-<port> | |
import os | |
import re | |
import sys | |
from os.path import expanduser | |
CONF_TEMPLATE=''' | |
# A lighttpd.conf template for serving static files. | |
server.document-root = "{{document_root}}" | |
server.port = {{port}} | |
dir-listing.activate = "enable" | |
index-file.names = ( "index.php", "index.html", "index.lighttpd.html" ) | |
mimetype.use-xattr = "disable" | |
## | |
## mimetype mapping | |
## | |
mimetype.assign = ( | |
".pdf" => "application/pdf", | |
".sig" => "application/pgp-signature", | |
".spl" => "application/futuresplash", | |
".class" => "application/octet-stream", | |
".ps" => "application/postscript", | |
".torrent" => "application/x-bittorrent", | |
".dvi" => "application/x-dvi", | |
".gz" => "application/x-gzip", | |
".pac" => "application/x-ns-proxy-autoconfig", | |
".swf" => "application/x-shockwave-flash", | |
".tar.gz" => "application/x-tgz", | |
".tgz" => "application/x-tgz", | |
".tar" => "application/x-tar", | |
".zip" => "application/zip", | |
".mp3" => "audio/mpeg", | |
".m3u" => "audio/x-mpegurl", | |
".wma" => "audio/x-ms-wma", | |
".wax" => "audio/x-ms-wax", | |
".ogg" => "application/ogg", | |
".wav" => "audio/x-wav", | |
".gif" => "image/gif", | |
".jpg" => "image/jpeg", | |
".jpeg" => "image/jpeg", | |
".png" => "image/png", | |
".xbm" => "image/x-xbitmap", | |
".xpm" => "image/x-xpixmap", | |
".xwd" => "image/x-xwindowdump", | |
".css" => "text/css", | |
".html" => "text/html", | |
".htm" => "text/html", | |
".js" => "text/javascript", | |
".asc" => "text/plain", | |
".c" => "text/plain", | |
".cpp" => "text/plain", | |
".log" => "text/plain", | |
".conf" => "text/plain", | |
".text" => "text/plain", | |
".txt" => "text/plain", | |
".spec" => "text/plain", | |
".dtd" => "text/xml", | |
".xml" => "text/xml", | |
".mpeg" => "video/mpeg", | |
".mpg" => "video/mpeg", | |
".mov" => "video/quicktime", | |
".qt" => "video/quicktime", | |
".avi" => "video/x-msvideo", | |
".asf" => "video/x-ms-asf", | |
".asx" => "video/x-ms-asf", | |
".wmv" => "video/x-ms-wmv", | |
".bz2" => "application/x-bzip", | |
".tbz" => "application/x-bzip-compressed-tar", | |
".tar.bz2" => "application/x-bzip-compressed-tar", | |
".rpm" => "application/x-rpm", | |
".json" => "application/json", | |
# make the default mime type application/octet-stream. | |
"" => "application/octet-stream", | |
) | |
''' | |
def read_file(path): | |
with open (path) as f: | |
contents = f.read() | |
return contents | |
def write_file(path, contents): | |
with open(path, "w") as f: | |
f.write(contents) | |
def render_template(mu_template, vars): | |
"""Render a mu-template with values from a hash.""" | |
def lookup(match): | |
return vars.get(match.group(1), '') | |
return re.sub('{{(\w+?)}}', lookup, mu_template) | |
def make_config(port, docroot): | |
"""Return the config file for a specific port and docroot.""" | |
vars = { 'document_root': docroot, 'port': str(port) } | |
return render_template(CONF_TEMPLATE, vars) | |
def start_server(port, docroot, config_path): | |
content = make_config(port, docroot) | |
write_file(config_path, content) | |
os.execvp('lighttpd', ['lighttpd', '-f', config_path, '-D']) | |
def usage(): | |
prog = sys.argv[0] | |
print "Usage: %s port docroot" % (prog) | |
sys.exit(1) | |
def parse_port(s): | |
if re.match("\d+\Z", s): | |
p = int(s) | |
if p >= 1 and p <= 65535: | |
return p | |
return None | |
def valid_docroot(docroot): | |
return os.path.isdir(docroot) | |
def parse_args(args): | |
if len(args) < 2: usage() | |
port, docroot = args[0], args[1] | |
p = parse_port(port) | |
if p is None: | |
print "Not a valid port:", port | |
sys.exit(1) | |
if not valid_docroot(docroot): | |
print "docroot is not a directory:", docroot | |
sys.exit(1) | |
return p, docroot | |
def main(argv): | |
# usage: start-server port directory | |
port, docroot = parse_args(argv[1:]) | |
home = expanduser("~") | |
config_path = os.path.join(home, '.lighttpd-' + str(port)) | |
start_server(port, docroot, config_path) | |
if __name__ == '__main__': | |
main(sys.argv) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment