-
Notifications
You must be signed in to change notification settings - Fork 0
/
testServer.py
147 lines (118 loc) · 4.73 KB
/
testServer.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
__author__ = 'xhaos'
#!/usr/bin/python
import SocketServer as socket
import json
import numpy as np
import scipy.cluster.hierarchy as hac
from scipy import cluster
#from matplotlib import pyplot
import random
import threading
import SimpleHTTPServer
import SocketServer
import logging
import cgi
from BaseHTTPServer import BaseHTTPRequestHandler
import sys
PORT = 8081
elements = []
APIkey = 'AIzaSyARZhrqUd5ZbaPNcSXRIaaZaOZlZ7Br-Y0'
travelMode = 'walking'
def tic():
import time
global startTime_for_tictoc
startTime_for_tictoc = time.time()
def toc():
import time
if 'startTime_for_tictoc' in globals():
print "Elapsed time is " + str(time.time() - startTime_for_tictoc) + " seconds."
else:
print "Toc: start time not set"
def claster(destinations, num_clusters):
z = hac.linkage(destinations, 'ward')
claster_assignment = hac.fcluster(z, num_clusters, 'maxclust')
return claster_assignment
class ServerHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
ID = ""
numberOfDays = ""
homeLocation = ""
destinations = []
def do_GET(self):
logging.warning("======= GET STARTED =======")
logging.warning(self.headers)
SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)
def options(self):
self.response.headers['Access-Control-Allow-Origin'] = '*'
self.response.headers['Access-Control-Allow-Headers'] = 'Origin, X-Requested-With, Content-Type, Accept'
self.response.headers['Access-Control-Allow-Methods'] = 'POST, GET, PUT, DELETE'
def do_POST(self):
#logging.warning("======= POST STARTED =======")
#logging.warning(self.headers)
form = cgi.FieldStorage(
fp=self.rfile,
headers=self.headers,
environ={'REQUEST_METHOD':'POST',
'CONTENT_TYPE':self.headers['Content-Type'],
})
#logging.warning("======= POST VALUES =======")
for item in form.list:
logging.warning(item)
logging.warning("\n")
print form
ID = ""
numberOfDays = ""
homeLocation = ""
destinations = []
for field in form.keys():
if "_id" in form[field].name:
ID = form[field].value
elif "days" in form[field].name:
numberOfDays = form[field].value
elif "home" in form[field].name:
temp = form[field].value
homeLocation = temp.split(",")
elif "destination" in form[field].name:
temp = form[field].value
tempDestination = temp.split(",")
destinations.append(tempDestination)
print ID
print numberOfDays
print homeLocation
print destinations
tempClaster = claster(destinations, numberOfDays)
print tempClaster
# Calculate response
googleString = 'https://maps.googleapis.com/maps/api/directions/json?origin='
googleString += str(homeLocation[0])+','+str(homeLocation[1])+'&destination='+str(homeLocation[0])+','+str(homeLocation[1])+'&waypoints=optimize:true|'
# Begin the response
self.send_response(200)
self.send_header("Content-type", "application/json")
self.send_header("Access-Control-Allow-Origin", "*");
self.send_header("Access-Control-Expose-Headers", "Access-Control-Allow-Origin");
self.send_header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
self.end_headers()
for i in range(1, int(numberOfDays)+1):
print "day:"+ str(i)
for j in range(0, len(tempClaster)):
if int(tempClaster[j]) == i:
#internarary.append(destinations[j])
googleString += str(destinations[j][0])+','+str(destinations[j][1])+'|'
#print internarary
googleString = googleString[:-1]
googleString += '&key='+APIkey+'&mode='+travelMode
self.request.sendall('Days internarary:')
self.request.sendall('\n')
print(googleString)
self.request.sendall(googleString)
self.request.sendall('\n')
googleString = 'https://maps.googleapis.com/maps/api/directions/json?origin='
googleString += str(homeLocation[0])+','+str(homeLocation[1])+'&destination='+str(homeLocation[0])+','+str(homeLocation[1])+'&waypoints=optimize:true|'
SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)
def start_server():
"""Start the server."""
Handler = ServerHandler
httpd = SocketServer.TCPServer(("", PORT), Handler)
print("Server started and listening")
httpd.serve_forever()
if __name__ == "__main__":
start_server()