-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmapdata.py
84 lines (68 loc) · 2.3 KB
/
mapdata.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
#coding=utf-8
"""
$ pip install tornado
$ python livemap.py --port=8888
Open http://localhost:8888 in one window
Written with tornado==2.3
"""
import os
import json
import logging
import tornado.ioloop
import tornado.web
import sqlite3
from tornado.options import define, options
log = logging.getLogger(__name__)
dbPath = 'data.db'
def _query_db(query, args=(), one=False):
connection = sqlite3.connect(dbPath)
cur = connection.cursor()
cur.execute(query, args)
r = [dict((cur.description[i][0], value) \
for i, value in enumerate(row)) for row in cur.fetchall()]
cur.connection.close()
return (r[0] if r else None) if one else r
def _execute_db(sql,args=(),one=False):
connection = sqlite3.connect(dbPath)
cur = connection.cursor()
cur.execute(sql, args)
cur.connection.commit()
cur.connection.close()
class MainHandler(tornado.web.RequestHandler):
def get(self):
log.debug("open root")
self.render("index.html")
class DataHandler(tornado.web.RequestHandler):
"""docstring for DataHandler"""
def get(self):
"""get method"""
# ajax no cache
self.set_header('Cache-Control', 'no-cache, must-revalidate')
self.set_header('Expires', '0')
dic= _query_db("select * from gis")
self.write(json.dumps(dic,ensure_ascii=False))
def post(self):
""" post for gis env"""
dep = self.get_argument("dep")
id = self.get_argument("id")
name= self.get_argument("name")
lon = self.get_argument("lon")
lat = self.get_argument("lat")
# query for only one
_execute_db("insert or replace into gis (id,dep,name,lon,lat) values(?,?,?,?,?)",(id,dep,name,lon,lat))
self.write("OK")
settings = {
'template_path':os.path.join(os.path.dirname(os.path.abspath(__file__)), 'templates'),
'static_path': os.path.join(os.path.dirname(os.path.abspath(__file__)), "static"),
}
print settings
application = tornado.web.Application([
(r"/", MainHandler),
(r"/mapdata", DataHandler),
],
**settings)
if __name__ == "__main__":
define("port", default=8888, help="Run server on a specific port", type=int)
tornado.options.parse_command_line()
application.listen(options.port)
tornado.ioloop.IOLoop.instance().start()