-
Notifications
You must be signed in to change notification settings - Fork 5
/
app.py
49 lines (45 loc) · 1.75 KB
/
app.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
#!/usr/bin/env python
# coding: utf-8
import tornado.ioloop
import tornado.options
from tornado.web import Application
from settings import *
def make_app(routers, **kwargs):
class ExtApplication(Application):
def __init__(self, routers, **kwargs):
self.redisConn = None
postRouters = []
for urlSpec in routers:
if urlSpec.name in enableList:
postRouters.append(urlSpec)
super(ExtApplication, self).__init__(postRouters, **kwargs)
def getRedisConn(self):
try:
import tornadoredis
if self.redisConn is None:
redisConn = tornadoredis.Client(host=redis["host"], port=redis["port"], selected_db=redis["db"])
redisConn.connect()
self.redisConn = redisConn
return self.redisConn
except:
print("can not connect to redis.")
self.redisConn = None
def getSMTPConn(self):
try:
import smtplib
smtp = smtplib.SMTP()
smtp.connect(mail["smtpServer"], mail["smtpPort"])
# smtp.starttls()
smtp.login(mail["username"], mail["password"])
return smtp
except Exception:
if smtp:
smtp.close()
raise
return ExtApplication(routers, **kwargs)
if __name__ == "__main__":
tornado.options.define(name='port', default=port, type=int, help='given a http listen port')
tornado.options.parse_command_line()
app = make_app(routers, **appSettings)
app.listen(tornado.options.options.port, address=address)
tornado.ioloop.IOLoop.current().start()