simple web server
This commit is contained in:
48
webserver.py
Executable file
48
webserver.py
Executable file
@@ -0,0 +1,48 @@
|
|||||||
|
#!/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)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Reference in New Issue
Block a user