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 | <html> <head> <title>Weblog Viewer</title> <meta HTTP-EQUIV="Refresh" CONTENT="30" /> <style type="text/css"><!-- div.info { background-color: lightblue; padding: 2px dotted; } table th, table td { text-align: left; cellspacing: 0px; cellpadding: 0px; } table.log { border: 0px; width: 100%; } table.log tr.even { background-color: white; } table.log tr.odd { background-color: lightgray; } --></style> </head> <body> <div class="info"> You are displaying the contents of <code model="filename" view="Text">filename</code>. </div> <table border="0" cellspacing="0" width="100%" class="log" model="entries" view="List"> <tr bgcolor="yellow" pattern="listHeader"> <th>Referrer</th><th/> <th>Resource</th> </tr> <tr pattern="listItem" view="alternateColor"> <td model="referrer" view="Text"> Referrer</td> <td>-></td> <td model="request_resource" view="Text"> Resource</td> </tr> <tr pattern="emptyList"> <td colspan="2">There is nothing to display.</td> </tr> </table> </body> </html> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | import webloglib as wll import os, sys from urllib import unquote_plus as uqp from twisted.internet import reactor from twisted.web import microdom from twisted.web.woven import page, widgets logfile = '../access-log' LOG = open(logfile) RECS = [] NUM_ROWS = 25 def update(): global RECS RECS.extend(LOG.readlines()) RECS = RECS[-NUM_ROWS*3:] reactor.callLater(5, update) update() |
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 | 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() |
% mktap web --resource-script=WeblogViewer.py --port 8080在最后一部分中这篇介绍只涉及了 Woven 的一些皮毛。该软件包中还有许多复杂的功能,我希望自己给出的例子能对模板化系统起到抛砖引玉的作用。
% twistd -f web.tap
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) | Powered by Discuz! 7.0.0 |