mktap web --path ~/twisted/www --port 8080利用下面的命令启动该应用程序:
twistd -f web.tap就是这样的。碰巧在 ~/twisted/www 基本目录(或子目录)中的任何 HTML 或 .rpy 文件将为端口 8080 上的客户端服务。实际上,您可以提供任何您喜欢的文件类型,只是.rpy 文件将被看作是特殊的动态脚本。
1 2 3 4 5 6 7 | from twisted.web import resource, server from persist import Records from webloglib import log_fields, COLOR from urllib import unquote_plus as uqp fieldnames = """ip timestamp request status bytes referrer agent""".split() field_dict = dict(zip(fieldnames, range(len(fieldnames)))) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | TOP = '''<html><head><title>Weblog Refresher</title> <META HTTP-EQUIV="Refresh" CONTENT="30"/></head> <body> <table border="1" width="100%%"> <tr bgcolor="yellow"> <form action="http://gnosis.cx:8080/config_refresher.rpy" method="GET"> <td> IP <input type="checkbox" name="ip" %s/> </td> <td> Timestamp <input type="checkbox" name="timestamp" %s/></td> <td> Request <input type="checkbox" name="request" %s/></td> <td> Status <input type="checkbox" name="status" %s/></td> <td> Bytes <input type="checkbox" name="bytes" %s/></td> <td> Referrer <input type="checkbox" name="referrer" %s/></td> <td> Agent <input type="checkbox" name="agent" %s/></td> <td> <input type="submit" value="Change Fields"></td> </form> </td></tr> <table border="0" cellspacing="0" width="100%%">''' ROW = '<tr bgcolor=" %s">%s</tr>\n' END = '</table></body></html>' COLOR = ['white','lightgray'] END = '''</table></body></html>''' |
1 2 3 4 | records = registry.getComponent(Records) if not records: records = Records() registry.setComponent(Records, records) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | class Resource(resource.Resource): def render(self, request): showlist = [] for field in request.args.keys(): showlist.append(field_dict[field]) showlist.sort() checked = [""] * len(fieldnames) for n in showlist: checked[n] = 'checked' request.write(TOP % tuple(checked)) odd = 0 for rec in records.getNew(): hit = [field.strip('"') for field in log_fields(rec)] flds='\n'.join(['<td>%s</td>'%hit[n] for n in showlist]) request.write(ROW % (COLOR[odd], uqp(flds).replace('&&',' &'))) odd = not odd request.write(END) request.finish() return server.NOT_DONE_YET resource = Resource() |
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) | Powered by Discuz! 7.0.0 |