-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathweb.py
40 lines (31 loc) · 987 Bytes
/
web.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
import tornado.web
import tornado.ioloop
import json
from ipcountry import IPCountry
ipcountry = IPCountry('ipcountry.db')
class WebHandler(tornado.web.RequestHandler):
def get(self):
ip = self.get_argument('ip', self.request.remote_ip).strip()
cc = ipcountry.lookup(ip)
self.render('web.html', ip=ip, cc=cc)
class APIHandler(tornado.web.RequestHandler):
def get(self):
ip = self.get_argument('ip', self.request.remote_ip).strip()
cc = ipcountry.lookup(ip)
self.set_header('Content-Type', 'application/javascript')
data = {'cc': cc, 'ip': ip}
callback = self.get_argument('callback', None)
if callback:
self.finish('%s(%s);' % (callback, json.dumps(data)))
else:
self.finish(data)
def make_app():
return tornado.web.Application([
(r"/", WebHandler),
(r"/api", APIHandler),
], debug=True)
if __name__ == "__main__":
tornado.log.enable_pretty_logging()
app = make_app()
app.listen(9000, xheaders=True)
tornado.ioloop.IOLoop.current().start()