Mercurial > hg > GlobalNeighbors
annotate globalneighbors/web.py @ 15:21095c9006e5
city page is now functional + linky
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Sun, 25 Jun 2017 14:55:53 -0700 |
parents | 27925261c137 |
children | 2fef925fbf37 |
rev | line source |
---|---|
0
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
1 #!/usr/bin/env python |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
2 |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
3 """ |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
4 web handler for GlobalNeighbors |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
5 """ |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
6 |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
7 # imports |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
8 import argparse |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
9 import json |
6 | 10 import os |
0
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
11 import sys |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
12 import time |
11 | 13 from paste import httpserver |
14 from paste.urlparser import StaticURLParser | |
0
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
15 from webob import Request, Response, exc |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
16 from wsgiref import simple_server |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
17 from .locations import locations |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
18 from .locations import city_dict |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
19 from .read import read_cities |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
20 from .read import read_city_list |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
21 from .schema import fields |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
22 from .schema import name |
6 | 23 from .template import template_dir |
24 from .template import TemplateLoader | |
0
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
25 |
10
21ed15391e8a
add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents:
7
diff
changeset
|
26 here = os.path.dirname(os.path.abspath(__file__)) |
21ed15391e8a
add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents:
7
diff
changeset
|
27 static_dir = os.path.join(here, 'static') |
21ed15391e8a
add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents:
7
diff
changeset
|
28 |
21ed15391e8a
add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents:
7
diff
changeset
|
29 |
21ed15391e8a
add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents:
7
diff
changeset
|
30 class PassthroughFileserver(object): |
21ed15391e8a
add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents:
7
diff
changeset
|
31 |
21ed15391e8a
add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents:
7
diff
changeset
|
32 """serve files if they exist""" |
21ed15391e8a
add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents:
7
diff
changeset
|
33 |
21ed15391e8a
add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents:
7
diff
changeset
|
34 |
21ed15391e8a
add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents:
7
diff
changeset
|
35 def __init__(self, app, directory=static_dir): |
21ed15391e8a
add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents:
7
diff
changeset
|
36 self.app = app |
21ed15391e8a
add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents:
7
diff
changeset
|
37 self.directory = directory |
21ed15391e8a
add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents:
7
diff
changeset
|
38 self.fileserver = StaticURLParser(self.directory) |
21ed15391e8a
add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents:
7
diff
changeset
|
39 |
21ed15391e8a
add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents:
7
diff
changeset
|
40 def __call__(self, environ, start_response): |
21ed15391e8a
add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents:
7
diff
changeset
|
41 |
21ed15391e8a
add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents:
7
diff
changeset
|
42 path = environ['PATH_INFO'].strip('/') |
21ed15391e8a
add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents:
7
diff
changeset
|
43 |
21ed15391e8a
add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents:
7
diff
changeset
|
44 if path and os.path.exists(os.path.join(self.directory, path)) and '..' not in path: |
21ed15391e8a
add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents:
7
diff
changeset
|
45 |
21ed15391e8a
add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents:
7
diff
changeset
|
46 return self.fileserver(environ, start_response) |
21ed15391e8a
add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents:
7
diff
changeset
|
47 return self.app(environ, start_response) |
21ed15391e8a
add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents:
7
diff
changeset
|
48 |
0
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
49 |
7
254195d0bac2
partial implementation of autocomplete using jqueryui; easyautocomplete.com may be more what we want
Jeff Hammel <k0scist@gmail.com>
parents:
6
diff
changeset
|
50 def autocomplete(cities, startswith=None, limit=None): |
1 | 51 """autocomplete function for city names""" |
7
254195d0bac2
partial implementation of autocomplete using jqueryui; easyautocomplete.com may be more what we want
Jeff Hammel <k0scist@gmail.com>
parents:
6
diff
changeset
|
52 ### TODO: |
254195d0bac2
partial implementation of autocomplete using jqueryui; easyautocomplete.com may be more what we want
Jeff Hammel <k0scist@gmail.com>
parents:
6
diff
changeset
|
53 # - sort once, ahead of time |
254195d0bac2
partial implementation of autocomplete using jqueryui; easyautocomplete.com may be more what we want
Jeff Hammel <k0scist@gmail.com>
parents:
6
diff
changeset
|
54 # - return most populous cities |
1 | 55 |
56 if startswith: | |
57 retval = [] | |
58 for i in cities: | |
7
254195d0bac2
partial implementation of autocomplete using jqueryui; easyautocomplete.com may be more what we want
Jeff Hammel <k0scist@gmail.com>
parents:
6
diff
changeset
|
59 if i[name].startswith(startswith): |
13 | 60 retval.append({"name": i[name], |
15
21095c9006e5
city page is now functional + linky
Jeff Hammel <k0scist@gmail.com>
parents:
14
diff
changeset
|
61 "country code": i["country code"], |
21095c9006e5
city page is now functional + linky
Jeff Hammel <k0scist@gmail.com>
parents:
14
diff
changeset
|
62 "population": i['population'], |
13 | 63 "geonameid": i['geonameid']}) |
1 | 64 else: |
13 | 65 retval = [{"name": i[name], |
15
21095c9006e5
city page is now functional + linky
Jeff Hammel <k0scist@gmail.com>
parents:
14
diff
changeset
|
66 "country code": i["country code"], |
21095c9006e5
city page is now functional + linky
Jeff Hammel <k0scist@gmail.com>
parents:
14
diff
changeset
|
67 "population": i['population'], |
21095c9006e5
city page is now functional + linky
Jeff Hammel <k0scist@gmail.com>
parents:
14
diff
changeset
|
68 "geonameid": i['geonameid']} |
13 | 69 for i in cities] |
15
21095c9006e5
city page is now functional + linky
Jeff Hammel <k0scist@gmail.com>
parents:
14
diff
changeset
|
70 return sorted(retval, |
21095c9006e5
city page is now functional + linky
Jeff Hammel <k0scist@gmail.com>
parents:
14
diff
changeset
|
71 key=lambda x: (x['name'], |
21095c9006e5
city page is now functional + linky
Jeff Hammel <k0scist@gmail.com>
parents:
14
diff
changeset
|
72 -x['population']) |
21095c9006e5
city page is now functional + linky
Jeff Hammel <k0scist@gmail.com>
parents:
14
diff
changeset
|
73 )[:limit] |
1 | 74 |
75 | |
0
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
76 class Handler(object): |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
77 """base class for HTTP handler""" |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
78 |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
79 def __call__(self, environ, start_response): |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
80 request = Request(environ) |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
81 method = getattr(self, request.method, None) |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
82 response = None |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
83 if method is not None: |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
84 response = method(request) |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
85 if response is None: |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
86 response = exc.HTTPNotFound() |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
87 return response(environ, start_response) |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
88 |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
89 |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
90 class CitiesHandler(Handler): |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
91 """cities ReST API""" |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
92 |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
93 content_type = 'application/json' |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
94 |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
95 def __init__(self, locations, cities): |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
96 self.locations = locations |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
97 self._cities = cities |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
98 |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
99 |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
100 def cities(self, startswith=None): |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
101 """return list of cities""" |
10
21ed15391e8a
add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents:
7
diff
changeset
|
102 return autocomplete(self._cities.values(), |
1 | 103 startswith=startswith) |
0
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
104 |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
105 def GET(self, request): |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
106 return Response(content_type=self.content_type, |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
107 body=json.dumps(self.cities( |
7
254195d0bac2
partial implementation of autocomplete using jqueryui; easyautocomplete.com may be more what we want
Jeff Hammel <k0scist@gmail.com>
parents:
6
diff
changeset
|
108 startswith=request.GET.get('term')))) |
0
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
109 |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
110 |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
111 class GlobalHandler(Handler): |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
112 """WSGI HTTP Handler""" |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
113 |
6 | 114 content_type = 'text/html' |
0
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
115 |
6 | 116 def __init__(self, datafile, template_dir=template_dir): |
117 | |
0
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
118 |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
119 # parse data |
6 | 120 self.datafile = datafile |
0
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
121 self.cities = read_city_list(self.datafile, |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
122 fields=fields) |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
123 self.locations = locations(self.cities) |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
124 |
13 | 125 # get country codes |
126 self.country_codes = sorted(set([city['country code'] | |
127 for city in self.cities | |
128 if city['country code']])) | |
129 | |
10
21ed15391e8a
add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents:
7
diff
changeset
|
130 # convert cities to a dict for lookup |
21ed15391e8a
add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents:
7
diff
changeset
|
131 self.cities = {city['geonameid'] : city |
21ed15391e8a
add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents:
7
diff
changeset
|
132 for city in self.cities} |
21ed15391e8a
add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents:
7
diff
changeset
|
133 |
13 | 134 |
6 | 135 # declare handlers |
0
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
136 self.handlers = {'/cities': |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
137 CitiesHandler(self.locations, |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
138 self.cities)} |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
139 |
6 | 140 # template loader |
141 self.loader = TemplateLoader(template_dir) | |
142 self.index = self.loader.load('index.html') | |
10
21ed15391e8a
add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents:
7
diff
changeset
|
143 self.citypage = self.loader.load('city.html') |
6 | 144 |
0
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
145 |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
146 def GET(self, request): |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
147 if request.path_info in ('', '/'): |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
148 # Landing page |
13 | 149 body = self.index.render(n_cities=len(self.cities), |
150 country_codes=self.country_codes) | |
0
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
151 return Response(content_type=self.content_type, |
13 | 152 body=body) |
153 | |
10
21ed15391e8a
add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents:
7
diff
changeset
|
154 elif request.path_info in self.handlers: |
21ed15391e8a
add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents:
7
diff
changeset
|
155 return request.get_response(self.handlers[request.path_info]) |
0
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
156 else: |
10
21ed15391e8a
add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents:
7
diff
changeset
|
157 try: |
21ed15391e8a
add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents:
7
diff
changeset
|
158 geoid = int(request.path.strip('/')) |
21ed15391e8a
add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents:
7
diff
changeset
|
159 city = self.cities.get(geoid) |
21ed15391e8a
add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents:
7
diff
changeset
|
160 if not city: |
21ed15391e8a
add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents:
7
diff
changeset
|
161 return |
15
21095c9006e5
city page is now functional + linky
Jeff Hammel <k0scist@gmail.com>
parents:
14
diff
changeset
|
162 variables = dict(city=city) |
10
21ed15391e8a
add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents:
7
diff
changeset
|
163 return Response(content_type=self.content_type, |
15
21095c9006e5
city page is now functional + linky
Jeff Hammel <k0scist@gmail.com>
parents:
14
diff
changeset
|
164 body=self.citypage.render(variables)) |
10
21ed15391e8a
add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents:
7
diff
changeset
|
165 except ValueError: |
21ed15391e8a
add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents:
7
diff
changeset
|
166 pass |
0
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
167 |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
168 |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
169 |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
170 def main(args=sys.argv[1:]): |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
171 """CLI""" |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
172 |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
173 # parse command line |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
174 parser = argparse.ArgumentParser(description=__doc__) |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
175 parser.add_argument('cities', |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
176 help="cities1000 data file") |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
177 parser.add_argument('-p', '--port', dest='port', |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
178 type=int, default=8080, |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
179 help="port to serve on") |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
180 parser.add_argument('--hostname', dest='hostname', |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
181 default='localhost', |
6 | 182 help="host name [DEFAULT: %(default)s]") |
0
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
183 options = parser.parse_args(args) |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
184 |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
185 # instantiate WSGI handler |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
186 app = GlobalHandler(datafile=options.cities) |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
187 |
11 | 188 # wrap it in a static file server |
189 app = PassthroughFileserver(app) | |
190 | |
191 print ("Serving on http://{hostname}:{port}/".format(**options.__dict__)) | |
0
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
192 try: |
11 | 193 httpserver.serve(app, host='0.0.0.0', port=options.port) |
0
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
194 except KeyboardInterrupt: |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
195 pass |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
196 |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
197 |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
198 if __name__ == '__main__': |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
199 main() |