forked from JTSinghal/GeoTrash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
executable file
·69 lines (61 loc) · 1.92 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env python3
import tornado.ioloop
import tornado.web as web
import mysql.connector
import os
public_root = os.path.join(os.path.dirname(__file__), 'public')
images_root = os.path.join(os.path.dirname(__file__), 'Images')
class MainHandler(web.RequestHandler):
def get(self):
self.render("geotrash.html", title="GeoTrash")
class Retrieve(web.RequestHandler):
def get(self):
_, c = get_cursor()
c.execute("select lat, lon, code, floor from bins")
r = c.fetchall()
d = {}
index = 0
for i in r:
d[index] = {"lat":i[0], "lng": i[1], "code": i[2], "floor":i[3]}
index += 1
self.write(d)
c.close()
class PostHandler(web.RequestHandler):
def post(self):
lat = self.get_argument('lat')
lon = self.get_argument('lon')
code = self.get_argument('code')
floor = self.get_argument('floor')
sql = "INSERT INTO bins (lat, lon, code, floor) values (%s, %s, %s, %s)"
val = (lat, lon, code, floor)
db, c = get_cursor()
c.execute(sql, val)
db.commit()
c.close()
def get_cursor():
db = mysql.connector.connect(
host="localhost",
user="geotrash",
passwd="ifeelfree",
database="geotrash"
)
c = db.cursor()
return db, c
def make_app():
return web.Application([
(r"/add", PostHandler),
(r"/", MainHandler),
(r'/public/(.*)', web.StaticFileHandler, {'path': public_root}),
(r'/Images/(.*)', web.StaticFileHandler, {'path': images_root}),
(r"/retrieve", Retrieve)
])
def main():
app = make_app()
http_server = tornado.httpserver.HTTPServer(app, ssl_options={
"certfile": "/usr/lib/ssl/certs/cert.pem",
"keyfile": "/usr/lib/ssl/certs/key.pem",
})
http_server.listen(8443)
tornado.ioloop.IOLoop.current().start()
if __name__ == "__main__":
main()