#!/usr/bin/env python#coding:utf-8def open_file (file_name): res={} with open (file_name) as f: for line in f: tmp= Line.split (' ') # print tmp ip,url=tmp[0],tmp[6]# print ip,url Save as a tuple in the list res[(Ip,url)]=res.get ((Ip,url), 0) +1 return sorted ( Res.items (), key=lambda x:x[1],reverse=true) #print open_file (' Log1.log ') def get_htmlstr ( ARR): #拼接字符串 tmp1= ' <tr><td>Num%s</td> <td>%s</td> <td>%s</td> <td>%s&lT;/td></tr> ' html_str= ' <table border= "1px" > ' +tmp1% (' rank ', ' IP ', ' URL ', ' COUNT ') for index,value in enumerate (Arr[:10]): html_str+= ' <tr><td>Num%s</td> <td>%s</td> <td>%s</td> <td>%s</td></tr> ' % (index,value[0][0],value[0][1], VALUE[1]) html_str+= ' </table> ' return html_strdef write_html (file_name): res=open_file (file_name) with Open (' res2.html ', ' W ') as f: f.write (GET_HTMLSTR (res)) write_html (' Log1.log ')
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/85/26/wKioL1ea_-fyxHYIAABLj-jxpwI996.png "title=" m0xuwx (ts]6afypd$pll9b0.png "alt=" Wkiol1ea_-fyxhyiaablj-jxpwi996.png "/>
This article from "Do not abandon!" Do not give up "blog, declined reprint!"
Python learns to sort by IP and URL in the log