-
Notifications
You must be signed in to change notification settings - Fork 15
/
server.py
68 lines (63 loc) · 2.13 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
import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web
import os
import sqlite3
import multiprocessing
import dnslog
import string
import random
from tornado.options import define, options
define("port", default=8000, help="run on the given port", type=int)
class BaseHandler(tornado.web.RequestHandler):
def initialize(self):
self.set_header('Server', 'Nginx')
self.conn = self.application.conn
class IndexHandler(BaseHandler):
def get(self):
cursor = self.conn.cursor()
sql = "select * from `log`"
cursor.execute(sql)
result = cursor.fetchall()
self.render("index.html",result=result)
def post(self):
get = self.get_argument('get',None)
delete = self.get_argument('delete',None)
if get == 'true':
self.write(''.join(random.sample(string.lowercase+string.digits,5)))
else:
self.write('0')
if delete == 'true':
cursor = self.conn.cursor()
sql = "delete from `log`"
cursor.execute(sql)
sql = "delete from sqlite_sequence where name='log'"
cursor.execute(sql)
cursor.execute("VACUUM")
self.conn.commit()
self.write('1')
else:
self.write('0')
class Application(tornado.web.Application):
def __init__(self):
handlers = [
(r'/', IndexHandler),
]
self.conn = sqlite3.connect('./dnslog.db')
settings = {
'template_path': os.path.join(os.path.dirname(__file__), "templates"),
'static_path': os.path.join(os.path.dirname(__file__), "static"),
'debug': True,
'cookie_secret': 'ea78c248-c5d0-4dd7-9780-cdb2f40ec21e',
}
tornado.web.Application.__init__(self,handlers,**settings)
if __name__ == "__main__":
p = multiprocessing.Process(target=dnslog.main)
p.daemon = True
p.start()
tornado.options.parse_command_line()
app = Application()
server = tornado.httpserver.HTTPServer(app)
server.listen(options.port)
tornado.ioloop.IOLoop.instance().start()