-
Notifications
You must be signed in to change notification settings - Fork 0
/
webapp.py
58 lines (49 loc) · 1.52 KB
/
webapp.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
# -*- coding: utf-8 -*-
#!/usr/bin/python
import web, os, sys
from models import *
import json
root = os.path.dirname(__file__)
constr = 'sqlite:///'+root+'/info.sqlite3'
#这里配置的url映射是大小写敏感的 ;所以最好 类名,路径全是小写
urls = {}
urls['/favicon.ico'] ='fav'#图标
class fav:
def GET(self):
path = os.path.join(root,'static/favicon.ico')
return open(path,'rb').read()
#静态文件 图片,css,js,html
#不必处理,webpy 默认映射 /static/ 路径
#但是在windows下要注意 启动程序时的路径,webpy并没有使用__file__当前路径
#以上是静态文件的处理,以下开始有逻辑页面
urls['/'] = 'index'#首页
class index:
def GET(self):
raise web.seeother('/static/v/index.html')
urls['/select/(.+)'] = 'select'#对象输出
class select:
def GET(self,modelname):
ss = Context(constr).session
exec('l = ss.query('+modelname+')')
a = dict(
error=modelname,
count=l.count(),
entities = [a.tdict() for a in l],
)
ss.close()
return json.dumps(a)
urls['/test'] = 'test'
class test:
def POST(self):
return 'test'+json.dumps(web.data())
def GET(self):
return 'test'+json.dumps(web.input())
dic = urls
urls = []
for k,v in dic.items():
urls.extend([k,v])
app = web.application(urls, globals())
if __name__ == "__main__":
#web.config.debug = False
#print app.request('/m').data
app.run()