Files
q-tools/webserver.py
ville rantanen 0d4ea78b05 simple web server
2013-11-07 15:41:48 +02:00

49 lines
1.3 KiB
Python
Executable File

#!/usr/bin/python
import sys,os
import BaseHTTPServer
from SimpleHTTPServer import SimpleHTTPRequestHandler
def setup_options():
''' Create command line options '''
from argparse import ArgumentParser
parser=ArgumentParser()
parser.add_argument("-p",type=int,dest="port",default=8000,
help="Server port (Default: %(default)s)")
parser.add_argument("-a",type=str,dest="address",default="",
help="Server bind address. Use localhost for privacy. Defaults to all interfaces.")
parser.add_argument("rootpath",type=str,action="store",default=os.path.abspath('.'),nargs='?',
help="Root path of the server")
options=parser.parse_args()
return options
def serve(options):
""" Run the web server """
HandlerClass = SimpleHTTPRequestHandler
ServerClass = BaseHTTPServer.HTTPServer
Protocol = "HTTP/1.0"
server_address = (options.address, options.port)
os.chdir(options.rootpath)
HandlerClass.protocol_version = Protocol
httpd = ServerClass(server_address, HandlerClass)
sa = httpd.socket.getsockname()
print "Serving http://"+ sa[0]+ ":"+ str(sa[1])+ "/"
try:
httpd.serve_forever()
except KeyboardInterrupt:
pass
httpd.server_close()
options=setup_options()
serve(options)