This repository was archived by the owner on Jun 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfeaturefilter_http_server.py
More file actions
79 lines (65 loc) · 2.44 KB
/
featurefilter_http_server.py
File metadata and controls
79 lines (65 loc) · 2.44 KB
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
#!/usr/bin/python
__author__ = "Michel Ott"
__version__ = "0.1"
__license__ = "???"
__copyright__ = "???"
import mimetypes, os
from optparse import OptionParser
from FeatureFilter.Server import wsgi_app
local_path_location = None
def local_app(environ, start_response):
global local_path_location
path = environ['PATH_INFO'][1:]
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:
return wsgi_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(environ, start_response)
def run(port=8080, 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)
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)