-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
51 lines (40 loc) · 1.51 KB
/
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
from http import HTTPStatus
import sys
import socketserver
import http.server
from model import get_price
import json
from urllib.parse import urlparse, parse_qs
def query_param_to_int(query_component, param_name):
return int(query_component[param_name][0])
class Handler(http.server.SimpleHTTPRequestHandler):
def do_GET(self):
self.send_response(HTTPStatus.OK)
self.send_header('Content-type', 'application/json')
self.send_header('Access-Control-Allow-Origin', '*')
self.end_headers()
query_components = parse_qs(urlparse(self.path).query)
# ask model for price
price = get_price([
query_param_to_int(query_components, 'LotArea'),
query_param_to_int(query_components, 'YearBuilt'),
query_param_to_int(query_components, '1stFlrSF'),
query_param_to_int(query_components, '2ndFlrSF'),
query_param_to_int(query_components, 'FullBath'),
query_param_to_int(query_components, 'BedroomAbvGr'),
query_param_to_int(query_components, 'TotRmsAbvGrd')
])
# create JSON response with price
response = {
"price": price,
}
# Encode the JSON response into a string
output = json.dumps(response).encode("utf-8")
self.wfile.write(output)
port = 8000
if len(sys.argv) > 1:
port = int(sys.argv[1])
# Start the server
httpd = socketserver.TCPServer(('', port), Handler)
print('* Server started on port', port)
httpd.serve_forever()