首页 > 代码库 > Tornado 代码说明

Tornado 代码说明

 1 class FaultyInfoHandler(tornado.web.RequestHandler): 2     def get(self): 3         import xmlrpc.client 4         s = xmlrpc.client.ServerProxy(LocalRpcSvcAddr) 5         info = s.faultyinfo() 6         self.render("faultyinfo.htm", lists=info) 7  8     def post(self): 9         import xmlrpc.client10         s = xmlrpc.client.ServerProxy(LocalRpcSvcAddr)11         event_id = self.get_argument(event_id, ‘‘) 12         if event_id :13             evid = event_id.split()14             num_evid = len(evid)15             for i in range(num_evid):16                 t = s.faultyrepair(evid[i])17                 if t[0] == 0:18                     flag = True19                 else:20                     flag = False21                     message = 操作失败 + : + t[1]22                     break23             if flag:    24                 message = 操作成功!25             self.write("<script language=javascript> alert(‘%s‘);</script>" % message)26         info = s.faultyinfo()27         info_num = len(info)28         if info_num > 0:29             self.write(<META HTTP-EQUIV=REFRESH CONTENT=0;30                     URL=/View/faultyinfo>)31         else:32             self.write("<script language=javascript> top.location=‘/‘;"33             "</script>")

(r"/View/faultyinfo", FaultyInfoHandler) #打开/View/faultyinfo页面,执行FaultyInfoHandler

分析:

1.class FaultyInfoHandler(tornado.web.RequestHandler):

--FaultyInfoHandler继承tornado.web.RequestHandler

2.def get(self):

--self 指的是FaultyInfoHandler(PS:我是这么认为的,有待确认)

 def get(self) 指重新定义get方法,像get、post这类方法原本在tornado.web.RequestHandler是存在的,这里是重定义

   self.render("faultyinfo.htm", lists=info) 中render方法在FaultyInfoHandler没有重定义,所以用的是继承于tornado.web.RequestHandler定义的render方法

3.def post(self):

--重新定义post方法,加载页面时,默认调用的是FaultyInfoHandler中的get方法,在htm中采用form表单指定post方法时才会调用post方法,htm中使用如下面代码所示:

  1 <html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office">  2 <head><title></title>  3 <link href=http://www.mamicode.com/"/css/base.css" type="text/css" rel="stylesheet">  4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">  5 <META HTTP-EQUIV="Pragma" CONTENT="no-cache">  6 <META HTTP-EQUIV="Cache-Control" content="no-cache">  7 </head>  8   9 <script language=javascript> 10 function IdentifyRepair(event_id) 11 { 12     var form_info = document.getElementById("form_info"); 13     form_info.method = "post"; 14     form_info.action = "/View/faultyinfo?event_id=" + event_id; 15     form_info.submit(); 16 } 17  18 function GetEventId() 19 { 20     var form_info = document.getElementById("form_info"); 21     var evid_list = document.getElementsByName("evid_list"); 22     var evid_info = ""; 23     for(var j=0; j<evid_list.length; j++) 24     { 25         evid_info += evid_list[j].value + " "; 26     } 27  28     form_info.method = "post"; 29     form_info.action = "/View/faultyinfo?event_id=" + evid_info; 30     form_info.submit(); 31 } 32 </script> 33  34 <body> 35  36 <table class=title width=700> 37     <tr> 38         <td><b>故障信息</b></td> 39         <td style="font-size:12px;color:#185A73" align=right width=*>{% module Uptime() %}</td> 40     </tr> 41 </table> 42  43 <br> 44 {% if lists != [] %} 45     <input type="button" onclick="GetEventId()" value=http://www.mamicode.com/"修复全部"> 46 {% end %} 47 <br> 48 {% for d in lists %} 49 <form id="form_info" name="form_info"> 50 <table width=900 border=0 cellspacing=1 cellpadding=1> 51     <tr id=tblhdr height=22> 52         <td width=200 align=center>TIME</td> 53         <td width=300 align=center>EVENT_ID</td> 54         <td width=150 align=center>MSG_ID</td> 55         <td width=150 align=center>SEVERITY</td> 56         <td align=center> 57             {% if d[event_id] %} 58                 <a style="color:#0000ff"  59                 href=http://www.mamicode.com/"javascript:IdentifyRepair(‘{{d[‘event_id‘]}}‘)">修复</a> 60             {% end %} 61         </td> 62     </tr> 63  64     <tr id=row0> 65         <td align=center>{{ d[time] }}</td> 66         <td align=center>{{ d[event_id] }}</td> 67         <td align=center>{{ d[msg_id] }}</td> 68         <td align=center>{{ d[severity] }}</td> 69         <input name="evid_list" type="hidden" value=http://www.mamicode.com/{{ d[event_id] }} /> 70      71     </tr> 72     <tr>  73     <table width=900 > 74         <tr id=row1> 75             <td width=20 ></td> 76             <td width=100 align=left>Product_sn</td> 77             <td width=780 align=left>{{ d[product_sn] }}</td> 78         </tr> 79         <tr id=row1> 80             <td width=20 ></td> 81             <td width=100 align=left >Fault class</td> 82             <td width=780 align=left>{{ d[faulty_class] }}</td> 83         </tr> 84         <tr id=row1> 85             <td width=20 ></td> 86             <td width=100 align=left>Affects</td> 87             <td width=780 align=left>{{ d[affects] }}</td> 88         </tr> 89         <tr id=row1> 90             <td width=20 ></td> 91             <td width=100 align=left>Problem in</td> 92             <td width=780 align=left>{{ d[problem_in] }}</td> 93         </tr> 94         <tr id=row1> 95             <td width=20 ></td> 96             <td width=100 align=left>FRU</td> 97             <td width=780 align=left>{{ d[fru] }}</td> 98         </tr> 99         <tr id=row1>100             <td width=20 ></td>101             <td width=100 align=left>Serial ID</td>102             <td width=780 align=left>{{ d[serial_id] }}</td>103         </tr>104         <tr id=row1>105             <td width=20 ></td>106             <td width=100 align=left>Description</td>107             <td width=780 align=left>{{ d[description] }}</td>108         </tr>109         <tr id=row1>110             <td width=20 ></td>111             <td width=100 align=left>Response</td>112             <td width=780 align=left>{{ d[response] }}</td>113         </tr>114         <tr id=row1>115             <td width=20 ></td>116             <td width=100 align=left>Impact</td>117             <td width=780 align=left>{{ d[impact] }}</td>118         </tr>119         <tr id=row1>120             <td width=20 ></td>121             <td width=100 align=left>Action</td>122             <td width=780 align=left>{{ d[action] }}</td>123         </tr>124     </table>125     </tr>126 </table>127 </form>128 <br>129 {% end %}130 </body>131 </html>

通过表单的方法:form_info.method  form_info.action  form_info.submit

4.self.write(‘<META HTTP-EQUIV=REFRESH CONTENT=0;URL=/View/faultyinfo>‘)

--重新加载/View/faultyinfo页面

5.self.write("<script language=javascript> top.location=‘/‘;</script>")

--重新加载整个页面(缺少说明!!!)