This repository has been archived by the owner on Sep 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 82
/
local-serve.py
executable file
·127 lines (86 loc) · 3.03 KB
/
local-serve.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/python
import BaseHTTPServer
import ConfigParser
import optparse
import os
import SimpleHTTPServer
import sys
# ############################################################
# Exceptions
# #########################################################{{{
class IncompatibleEnvironment(Exception):
"""Exception for when something has determined a problem with the
deployment environment."""
def __init__(self, msg):
self.msg = msg
def __str__(self):
return self.msg
# }}} END: Exceptions
# ############################################################
# Run
# #########################################################{{{
def run(port, address):
print "Base directory: %s" % os.getcwd()
handler_class = SimpleHTTPServer.SimpleHTTPRequestHandler
server_class = BaseHTTPServer.HTTPServer
server_address = (address, port)
handler_class.protocol_version = "HTTP/1.0"
httpd = server_class(server_address, handler_class)
sa = httpd.socket.getsockname()
#print "Serving HTTP on", sa[0], "port", sa[1], "..."
print "\nURL: http://%s:%d" % (address, sa[1])
httpd.serve_forever()
# }}} END: Run
# ############################################################
# Commandline arguments
# #########################################################{{{
class ARGS:
"""Class for command-line argument constants"""
PORT_LONG="--port"
PORT="-p"
PORT_HELP="Override port number (default is 8888)"
ADDR_LONG="--address"
ADDR="-a"
ADDR_HELP="Address to bind to (default is 'localhost')"
def parsersetup():
"""Return configured command-line parser."""
ver="Local build and serve docs for: http://workspace.globus.org"
usage="see help (-h)."
parser = optparse.OptionParser(version=ver, usage=usage)
# ----
group = optparse.OptionGroup(parser, "Options", "-------------")
group.add_option(ARGS.PORT, ARGS.PORT_LONG,
dest="port", metavar="PORT",
help=ARGS.PORT_HELP)
group.add_option(ARGS.ADDR, ARGS.ADDR_LONG,
dest="address", metavar="HOST/IP",
help=ARGS.ADDR_HELP)
parser.add_option_group(group)
# ----
return parser
# }}} END: Commandline arguments
# ############################################################
# Standalone entry and exit
# #########################################################{{{
def main(argv=None):
if os.name != 'posix':
print >>sys.stderr, "Only runs on POSIX systems."
return 3
parser = parsersetup()
if argv:
(opts, args) = parser.parse_args(argv[1:])
else:
(opts, args) = parser.parse_args()
port = 8888
if opts.port:
port = int(opts.port)
address = "localhost"
if opts.address:
address = opts.address
run(port, address)
if __name__ == "__main__":
try:
sys.exit(main())
except KeyboardInterrupt:
print "\n\nExiting..."
# }}} END: Standalone entry and exit