forked from iocast/featureserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
workspace_http_server.py
executable file
·70 lines (57 loc) · 2.31 KB
/
workspace_http_server.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
#!/usr/bin/python
"""A simple, standalone web server that serves FeatureServer requests."""
__author__ = "MetaCarta"
__version__ = "FeatureServer $Id: featureserver_http_server.py 564 2008-05-24 14:32:18Z crschmidt $"
__license__ = "Clear BSD"
__copyright__ = "2006-2008 MetaCarta"
import mimetypes, os
from optparse import OptionParser
from FeatureServer.Server import wsgi_app_workspace
local_path_location = None
def local_app(environ, start_response):
if environ['PATH_INFO'].startswith("/static/"):
global local_path_location
path = environ['PATH_INFO'].replace("/static/","")
path.lstrip("/")
mime = mimetypes.guess_type(path)
try:
f = open(os.path.join(local_path_location, path))
start_response("200 OK", [("Content-Type",mime[0])])
return [f.read()]
except Exception, E:
start_response("404 Not Found", [("Content-Type","text/plain")])
return ["Not found: %s" % E]
return wsgi_app_workspace(environ, start_response)
def run(port=8081, thread=False, local_path=""):
from wsgiref import simple_server
if thread:
from SocketServer import ThreadingMixIn
class myServer(ThreadingMixIn, simple_server.WSGIServer):
pass
else:
class myServer(simple_server.WSGIServer):
pass
httpd = myServer(('',port), simple_server.WSGIRequestHandler,)
if local_path:
global local_path_location
local_path_location = local_path
httpd.set_app(local_app)
else:
httpd.set_app(wsgi_app_workspace)
try:
print "Listening on port %s" % port
httpd.serve_forever()
except KeyboardInterrupt:
print "Shutting down."
if __name__ == '__main__':
parser = OptionParser(version=__version__, description=__doc__)
parser.add_option("-p", "--port",
help="port to run webserver on. Default is 8080",
dest="port",
action='store',
type="int",
default=8080)
parser.add_option("-t", help="enable threading in HTTP Server.", dest="thread", action="store_true", default=False)
parser.add_option("-l", help="serve files from local disk", dest="local_path")
(options, args) = parser.parse_args()
run(options.port, options.thread, options.local_path)