使用 Twisted 框架进行网络编程,第 3 部分
2008-11-13 13:13:08 来源:WEB开发网有趣的东西在于我们对类 twisted.web.woven.page.Page 的自定义。我们所做的大部分事情都是不可思议的,因为您需要定义特别指定的属性和方法。
清单 7. WeblogViewer.py Twisted 服务器 (续)
class WeblogViewer(page.Page):
"""A Page used for viewing Apache access logs."""
templateDirectory = '~/twisted/www'
templateFile = "WeblogViewer.xhtml"
# View factories and updates
def wvupdate_alternateColor(self, request, node, data):
"""Makes our table rows alternate CSS classes"""
# microdom.lmx is very handy; another example is located here:
# http://twistedmatrix.com/documents/howto/picturepile#auto0
tr = microdom.lmx(node)
tr['class'] = ('odd','even')[data['_number']%2]
# Model factories
def wmfactory_filename(self, request):
"""Returns the filename of the log being examined."""
return os.path.split(logfile)[1]
def wmfactory_entries(self, request):
"""Return list of dict objects representing log entries"""
entries = []
for rec in RECS:
hit = [field.strip('"') for field in wll.log_fields(rec)]
if hit[wll.status] == '200' and hit[wll.referrer] != '-':
# We add _number so our alternateColor view will work.
d = {'_number': len(entries),
'ip': hit[wll.ip],
'timestamp': hit[wll.timestamp],
'request': hit[wll.request],
'request_resource': hit[wll.request].split()[1],
'status': hit[wll.status],
'bytes': hit[wll.bytes],
'referrer': uqp(hit[wll.referrer]).
replace('&&',' &'),
'agent': hit[wll.agent],
}
entries.append(d)
return entries[-NUM_ROWS:]
resource = WeblogViewer()
更多精彩
赞助商链接