-
Notifications
You must be signed in to change notification settings - Fork 32
/
app_webpy.py
118 lines (98 loc) · 3.49 KB
/
app_webpy.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
112
113
114
115
116
117
118
#!/usr/bin/python
# -*- coding: utf-8 -*-
'''
@date: 2013-12-30
@author: shell.xu
@license: BSD-3-clause
'''
from __future__ import absolute_import, division,\
print_function, unicode_literals
import os
import copy
import stat
import httputil
import urllib
import logging
import unittest
from os import path
import web
from template import Template
class Main(object):
def GET(self, name):
logging.info('main url count: {}', session.count)
logging.info('main url match: {}', name)
body = 'main page, count: {}, match: {}'.format(
session.count, name)
session.count += 1
return body
class Post(object):
def POST(self, name):
l = str(len(web.data()))
logging.info('test post: {}', l)
return str(l)
class Path(object):
tplstr = '''{%import os%}{%from os import path%}<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
</head><body><table><thead><tr><td>file name</td><td>file mode</td>
<td>file size</td></tr></thead><tbody>{%for name in namelist:%}
{%stat_info = os.lstat(path.join(real_path, name))%}\
<tr><td><a href="{%=path.join(url_path, name).replace(os.sep, "/")%}">\
{%=name%}</a></td><td>{%=get_stat_str(stat_info.st_mode)%}</td><td>\
{%=stat_info.st_size%}</td></tr>{%end%}
</tbody></table></body>'''
tpl = Template(template=tplstr)
index_set = ['index.html', ]
def file_app(self, filepath):
with open(filepath, 'rb') as fi:
for b in httputil.file_source(fi):
yield b
def get_stat_str(self, mode):
stat_map = [
(stat.S_ISDIR, 'd'),
(stat.S_ISREG, 'f'),
(stat.S_ISLNK, 'l'),
(stat.S_ISSOCK, 's')]
return ''.join([s for f, s in stat_map if f(mode)])
def GET(self, filepath):
url_path = urllib.unquote(filepath)
real_path = path.join(self.basedir, url_path.lstrip('/'))
real_path = path.abspath(path.realpath(real_path))
if not real_path.startswith(self.basedir):
raise web.forbidden()
if not path.isdir(real_path):
return self.file_app(real_path)
for i in self.index_set:
test_path = path.join(real_path, i)
if os.access(test_path, os.R_OK):
return self.file_app(real_path)
namelist = os.listdir(real_path)
namelist.sort()
return self.tpl.render({
'namelist': namelist, 'get_stat_str': self.get_stat_str,
'real_path': real_path, 'url_path': url_path})
def StaticPath(basedir):
p = copy.copy(Path)
p.basedir = path.abspath(path.realpath(path.expanduser(basedir)))
return p
app = web.application((
'/post/(.*)', Post,
'/self/(.*)', StaticPath('.'),
'/(.*)', Main))
session = web.session.Session(
app, web.session.DiskStore('sessions'), initializer={'count': 0})
class TestAppWebpy(unittest.TestCase):
def test_main(self):
resp = app.request('/urlmatch')
self.assertEqual(resp.status, '200 OK')
self.assertEqual(
resp.data,
b'main page, count: 0, match: urlmatch')
self.assertIn('Set-Cookie', resp.headers)
def test_post(self):
resp = app.request('/post/postmatch', method='POST', data='postinfo')
self.assertEqual(resp.status, '200 OK')
self.assertEqual(resp.data, '8')
def test_path(self):
resp = app.request('/self/')
self.assertEqual(resp.status, '200 OK')
self.assertIn('httputil.py', resp.data)