-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.py
111 lines (97 loc) · 3.74 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
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/env python
#encoding=utf-8
import time, json
from twisted.web.server import Site, NOT_DONE_YET
from twisted.web.resource import Resource
from twisted.web.static import File
from twisted.internet import reactor
from mako.template import Template
from mako.lookup import TemplateLookup
# 导入本应用的配置参数
import cfg
# 创建并加载模板
lookup = TemplateLookup(directories=[cfg.template_dir], input_encoding='utf-8', output_encoding='utf-8')
tpl_login = lookup.get_template('login.html')
tpl_runstat = lookup.get_template('runstat.html')
# 导入model包定义的业务函数
from models.auth import Auth
from models.runstat import getRunstat
# 创建导航栏信息
navi = [(u'runstat', u'运行状态'), (u'parameter', u'参数配置'), (u'loginfo', u'日志信息'),]
# 创建页面公共信息
page_share = {'app_title':cfg.app_title, 'navi':navi}
class Runstat(Resource):
''' 运行状态 '''
isLeaf = True
page = {'page_title' : u'运行状态'}; page.update(page_share)
def render_POST(self, request):
request.setHeader("content-type", "text/plain")
return 'POST data: ' + request.content.getvalue()
def render_GET(self, request):
request.setHeader("content-type", "text/html")
self.page['stat'] = getRunstat()
return tpl_runstat.render(page=self.page)
class Parameter(Resource):
''' 参数配置 '''
isLeaf = True
def render_POST(self, request):
request.setHeader("content-type", "text/plain")
return 'POST data: ' + request.content.getvalue()
def render_GET(self, request):
request.setHeader("content-type", "text/plain")
return 'GET Request'
class Loginfo(Resource):
''' 日志信息 '''
isLeaf = True
def render_POST(self, request):
request.setHeader("content-type", "text/plain")
return 'POST data: ' + request.content.getvalue()
def render_GET(self, request):
request.setHeader("content-type", "text/plain")
return 'GET Request'
class LoginPage(Resource):
isLeaf = True
page = {'page_title' : u'用户登录'}; page.update(page_share)
def render_POST(self, request):
session = request.getSession()
# print request.args
if Auth(request.args['inputPassword'][0]):
session.isLogin = True
request.redirect("runstat")
else:
session.isLogin = False
request.redirect("loginpage?failed=1")
return ''
def render_GET(self, request):
request.getSession().expire()
request.setHeader("content-type", "text/html")
self.page['isFailed'] = request.args.get('failed')
return tpl_login.render(page=self.page)
class GetTime(Resource):
isLeaf = True
def render_GET(self, request):
return time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
class GetRunstat(Resource):
isLeaf = True
def render_GET(self, request):
request.setHeader("content-type", "application/json")
return json.dumps(getRunstat())
class Root(Resource):
''' 首页,同时第一级URL路由 '''
url = {'static':File(cfg.static_dir), 'loginpage':LoginPage(), 'runstat':Runstat(), 'parameter':Parameter(), 'loginfo':Loginfo()}
url.update({'gettime':GetTime(), 'getrunstat':GetRunstat()})
def getChild(self, name, request):
if name in ('static', 'gettime'):
return self.url.get(name)
session = request.getSession()
if not getattr(session, 'isLogin', False):
return self.url.get('loginpage')
else:
return self.url.get(name, self)
def render_GET(self, request):
request.redirect("runstat")
return ''
root = Root()
factory = Site(root)
reactor.listenTCP(8880, factory)
reactor.run()