活着的死人 发表于 2015-1-14 20:21:47

给大家带来用python剖析apache等web日记

如果您觉得本篇CentOSLinux教程讲得好,请记得点击右边漂浮的分享程序,把好文章分享给你的好朋友们!1剖析日记的python框架awk.py

viewplaincopyprint?



[*]#
[*]#Customawk.pymodule
[*]#
[*]
[*]
[*]classcontroller:
[*]
[*]def__init__(self,f):
[*]self.m_file=f
[*]self.m_handlers=[]
[*]
[*]
[*]defsubscribe(self,o):
[*]self.m_handlers.append(o)
[*]
[*]defrun(self):
[*]
[*]foroinself.m_handlers:
[*]o.begin()
[*]
[*]s=self.m_file.readline()
[*]
[*]whiles!="":
[*]
[*]foroinself.m_handlers:
[*]o.process_line(s)
[*]
[*]s=self.m_file.readline()
[*]
[*]
[*]foroinself.m_handlers:
[*]o.end()
[*]
[*]
[*]defprint_results(self):
[*]
[*]print
[*]print"Results:"
[*]print
[*]
[*]foroinself.m_handlers:
[*]print"------------------------------------------------------"
[*]printo.description()
[*]print"------------------------------------------------------"
[*]printo.result()


统计日记的点击量count_line.py

viewplaincopyprint?



[*]#Standardsysmodule
[*]importsys
[*]
[*]#Customawk.pymodule
[*]importawk
[*]
[*]classcount_lines:
[*]
[*]defbegin(self):
[*]self.m_count=0
[*]
[*]defprocess_line(self,s):
[*]self.m_count+=1
[*]
[*]defend(self):
[*]pass
[*]
[*]defdescription(self):
[*]return"#oflinesinthefile"
[*]
[*]defresult(self):
[*]returnself.m_count
[*]
[*]
[*]#
[*]#Step1:CreatetheAwkcontroller
[*]#
[*]ac=awk.controller(sys.stdin)
[*]
[*]#
[*]#Step2:Subscribethehandler
[*]#
[*]ac.subscribe(count_lines())
[*]
[*]#
[*]#Step3:Run
[*]#
[*]ac.run()
[*]
[*]#
[*]#Step4:Printtheresults
[*]#
[*]ac.print_results()


利用***是shell中实行
#catapachelog.log|pythoncount_lines.py

统计扫瞄次数凌驾n次的会见者visitors.py
HowmanypeoplehavereturnedtothesitemorethanNtimes?

<divclass="dp-highlighterbg_html"style="font-family:Consolas,CourierNew,Courier,mono,serif;width:700.90625px;overflow:auto;padding-top:1px;color:rgb(51,51,51);line-height:26px;margin:18px0px!important;background-color:rgb(231,229,220);">viewplaincopyprint?


<olclass="dp-xml"start="1"style="padding-right:0px;padding-left:0px;border:none;list-style-position:initial;list-style-image:initial;color:rgb(92,92,92);margin:0px0px1px45px!important;background-color:rgb(255,255,255);"><liclass="alt"style="border-style:nonenonenonesolid;border-left-width:3px;border-left-color:rgb(108,226,108);list-style:decimal-leading-zerooutside;color:inherit;line-height:24px;margin:0px!important;padding:0px3px0px10px!important;">importre;<listyle="border-style:nonenonenonesolid;border-left-width:3px;border-left-color:rgb(108,226,108);list-style:decimal-leading-zerooutside;line-height:24px;margin:0px!important;padding:0px3px0px10px!important;background-color:rgb(248,248,248);">importsys<liclass="alt"style="border-style:nonenonenonesolid;border-left-width:3px;border-left-color:rgb(108,226,108);list-style:decimal-leading-zerooutside;color:inherit;line-height:24px;margin:0px!important;padding:0px3px0px10px!important;">imortawk<listyle="border-style:nonenonenonesolid;border-left-width:3px;border-left-color:rgb(108,226,108);list-style:decimal-leading-zerooutside;line-height:24px;margin:0px!important;padding:0px3px0px10px!important;background-color:rgb(248,248,248);"><liclass="alt"style="border-style:nonenonenonesolid;border-left-width:3px;border-left-color:rgb(108,226,108);list-style:decimal-leading-zerooutside;color:inherit;line-height:24px;margin:0px!important;padding:0px3px0px10px!important;">classreturn_visitors:<listyle="border-style:nonenonenonesolid;border-left-width:3px;border-left-color:rgb(108,226,108);list-style:decimal-leading-zerooutside;line-height:24px;margin:0px!important;padding:0px3px0px10px!important;background-color:rgb(248,248,248);"><liclass="alt"style="border-style:nonenonenonesolid;border-left-width:3px;border-left-color:rgb(108,226,108);list-style:decimal-leading-zerooutside;color:inherit;line-height:24px;margin:0px!important;padding:0px3px0px10px!important;">def__init__(self,n):<listyle="border-style:nonenonenonesolid;border-left-width:3px;border-left-color:rgb(108,226,108);list-style:decimal-leading-zerooutside;line-height:24px;margin:0px!important;padding:0px3px0px10px!important;background-color:rgb(248,248,248);">self.m_n=n;<liclass="alt"style="border-style:nonenonenonesolid;border-left-width:3px;border-left-color:rgb(108,226,108);list-style:decimal-leading-zerooutside;color:inherit;line-height:24px;margin:0px!important;padding:0px3px0px10px!important;">self.m_ip_days={};<listyle="border-style:nonenonenonesolid;border-left-width:3px;border-left-color:rgb(108,226,108);list-style:decimal-leading-zerooutside;line-height:24px;margin:0px!important;padding:0px3px0px10px!important;background-color:rgb(248,248,248);"><liclass="alt"style="border-style:nonenonenonesolid;border-left-width:3px;border-left-color:rgb(108,226,108);list-style:decimal-leading-zerooutside;color:inherit;line-height:24px;margin:0px!important;padding:0px3px0px10px!important;">defbegin(self):<listyle="border-style:nonenonenonesolid;border-left-width:3px;border-left-color:rgb(108,226,108);list-style:decimal-leading-zerooutside;line-height:24px;margin:0px!important;padding:0px3px0px10px!important;background-color:rgb(248,248,248);">pass;<liclass="alt"style="border-style:nonenonenonesolid;border-left-width:3px;border-left-color:rgb(108,226,108);list-style:decimal-leading-zerooutside;color:inherit;line-height:24px;margin:0px!important;padding:0px3px0px10px!important;"><listyle="border-style:nonenonenonesolid;border-left-width:3px;border-left-color:rgb(108,226,108);list-style:decimal-leading-zerooutside;line-height:24px;margin:0px!important;padding:0px3px0px10px!important;background-color:rgb(248,248,248);">defprocess_line(self,s):<liclass="alt"style="border-style:nonenonenonesolid;border-left-width:3px;border-left-color:rgb(108,226,108);list-style:decimal-leading-zerooutside;color:inherit;line-height:24px;margin:0px!important;padding:0px3px0px10px!important;"><listyle="border-style:nonenonenonesolid;border-left-width:3px;border-left-color:rgb(108,226,108);list-style:decimal-leading-zerooutside;line-height:24px;margin:0px!important;padding:0px3px0px10px!important;background-color:rgb(248,248,248);">try:<liclass="alt"style="border-style:nonenonenonesolid;border-left-width:3px;border-left-color:rgb(108,226,108);list-style:decimal-leading-zerooutside;color:inherit;line-height:24px;margin:0px!important;padding:0px3px0px10px!important;">array=s.split();<listyle="border-style:nonenonenonesolid;border-left-width:3px;border-left-color:rgb(108,226,108);list-style:decimal-leading-zerooutside;line-height:24px;margin:0px!important;padding:0px3px0px10px!important;background-color:rgb(248,248,248);">ip=array;<liclass="alt"style="border-style:nonenonenonesolid;border-left-width:3px;border-left-color:rgb(108,226,108);list-style:decimal-leading-zerooutside;color:inherit;line-height:24px;margin:0px!important;padding:0px3px0px10px!important;">day=array;<listyle="border-style:nonenonenonesolid;border-left-width:3px;border-left-color:rgb(108,226,108);list-style:decimal-leading-zerooutside;line-height:24px;margin:0px!important;padding:0px3px0px10px!important;background-color:rgb(248,248,248);"><liclass="alt"style="border-style:nonenonenonesolid;border-left-width:3px;border-left-color:rgb(108,226,108);list-style:decimal-leading-zerooutside;color:inherit;line-height:24px;margin:0px!important;padding:0px3px0px10px!important;">ifself.m_ip_days.has_key(ip):<listyle="border-style:nonenonenonesolid;border-left-width:3px;border-left-color:rgb(108,226,108);list-style:decimal-leading-zerooutside;line-height:24px;margin:0px!important;padding:0px3px0px10px!important;background-color:rgb(248,248,248);"><liclass="alt"style="border-style:nonenonenonesolid;border-left-width:3px;border-left-color:rgb(108,226,108);list-style:decimal-leading-zerooutside;color:inherit;line-height:24px;margin:0px!important;padding:0px3px0px10px!important;">ifdaynotinself.m_ip_days:<listyle="border-style:nonenonenonesolid;border-left-width:3px;border-left-color:rgb(108,226,108);list-style:decimal-leading-zerooutside;line-height:24px;margin:0px!important;padding:0px3px0px10px!important;background-color:rgb(248,248,248);">self.m_ip_days.append(day);<liclass="alt"style="border-style:nonenonenonesolid;border-left-width:3px;border-left-color:rgb(108,226,108);list-style:decimal-leading-zerooutside;color:inherit;line-height:24px;margin:0px!important;padding:0px3px0px10px!important;"><listyle="border-style:nonenonenonesolid;border-left-width:3px;border-left-color:rgb(108,226,108);list-style:decimal-leading-zerooutside;line-height:24px;margin:0px!important;padding:0px3px0px10px!important;background-color:rgb(248,248,248);">else:<liclass="alt"style="border-style:nonenonenonesolid;border-left-width:3px;border-left-color:rgb(108,226,108);list-style:decimal-leading-zerooutside;color:inherit;line-height:24px;margin:0px!important;padding:0px3px0px10px!important;">self.m_ip_days=[];<listyle="border-style:nonenonenonesolid;border-left-width:3px;border-left-color:rgb(108,226,108);list-style:decimal-leading-zerooutside;line-height:24px;margin:0px!important;padding:0px3px0px10px!important;background-color:rgb(248,248,248);">self.m_ip_days.append(day);<liclass="alt"style="border-style:nonenonenonesolid;border-left-width:3px;border-left-color:rgb(108,226,108);list-style:decimal-leading-zerooutside;color:inherit;line-height:24px;margin:0px!important;padding:0px3px0px10px!important;"><listyle="border-style:nonenonenonesolid;border-left-width:3px;border-left-color:rgb(108,226,108);list-style:decimal-leading-zerooutside;line-height:24px;margin:0px!important;padding:0px3px0px10px!important;background-color:rgb(248,248,248);">exceptIndexError:<liclass="alt"style="border-style:nonenonenonesolid;border-left-width:3px;border-left-color:rgb(108,226,108);list-style:decimal-leading-zerooutside;color:inherit;line-height:24px;margin:0px!important;padding:0px3px0px10px!important;">pass;<listyle="border-style:nonenonenonesolid;border-left-width:3px;border-left-color:rgb(108,226,108);list-style:decimal-leading-zerooutside;line-height:24px;margin:0px!important;padding:0px3px0px10px!important;background-color:rgb(248,248,248);"><liclass="alt"style="border-style:nonenonenonesolid;border-left-width:3px;border-left-color:rgb(108,226,108);list-style:decimal-leading-zerooutside;color:inherit;line-height:24px;margin:0px!important;padding:0px3px0px10px!important;"><listyle="border-style:nonenonenonesolid;border-left-width:3px;border-left-color:rgb(108,226,108);list-style:decimal-leading-zerooutside;line-height:24px;margin:0px!important;padding:0px3px0px10px!important;background-color:rgb(248,248,248);"><liclass="alt"style="border-style:nonenonenonesolid;border-left-width:3px;border-left-color:rgb(108,226,108);list-style:decimal-leading-zerooutside;color:inherit;line-height:24px;margin:0px!important;padding:0px3px0px10px!important;">defend(self):<listyle="border-style:nonenonenonesolid;border-left-width:3px;border-left-color:rgb(108,226,108);list-style:decimal-leading-zerooutside;line-height:24px;margin:0px!important;padding:0px3px0px10px!important;background-color:rgb(248,248,248);"><liclass="alt"style="border-style:nonenonenonesolid;border-left-width:3px;border-left-color:rgb(108,226,108);list-style:decimal-leading-zerooutside;color:inherit;line-height:24px;margin:0px!important;padding:0px3px0px10px!important;">ips=self.m_ip_days.keys();<listyle="border-style:nonenonenonesolid;border-left-width:3px;border-left-color:rgb(108,226,108);list-style:decimal-leading-zerooutside;line-height:24px;margin:0px!important;padding:0px3px0px10px!important;background-color:rgb(248,248,248);">count=0;<liclass="alt"style="border-style:nonenonenonesolid;border-left-width:3px;border-left-color:rgb(108,226,108);list-style:decimal-leading-zerooutside;color:inherit;line-height:24px;margin:0px!important;padding:0px3px0px10px!important;"><listyle="border-style:nonenonenonesolid;border-left-width:3px;border-left-color:rgb(108,226,108);list-style:decimal-leading-zerooutside;line-height:24px;margin:0px!important;padding:0px3px0px10px!important;background-color:rgb(248,248,248);">foripinips:<liclass="alt"style="border-style:nonenonenonesolid;border-left-width:3px;border-left-color:rgb(108,226,108);list-style:decimal-leading-zerooutside;color:inherit;line-height:24px;margin:0px!important;padding:0px3px0px10px!important;">

金色的骷髅 发表于 2015-1-16 13:57:07

给大家带来用python剖析apache等web日记

熟读写基础知识,学得会不如学得牢。

admin 发表于 2015-1-25 21:45:45

尽量不要提问纯属是扯蛋.学习Linux特别是自己一个人初学入手的时候没人教很困难.当然如果可以的话平时多去买些Linux书...对学习Linux很有帮助.

仓酷云 发表于 2015-2-4 13:51:05

掌握硬件配置,如显卡,声卡,网卡等,硬件只要不是太老或太新一般都能被支持,作为一名Linux系统管理员建议多阅读有关硬件配置文章,对各种不支持或支持不太好的硬件有深刻的了解。

若相依 发表于 2015-2-10 01:48:22

熟读写基础知识,学得会不如学得牢。

小妖女 发表于 2015-2-28 17:59:01

学习Linux应具备的。[书籍+网络资源]

乐观 发表于 2015-3-10 08:43:07

清楚了解网络的基础知识,特别是在Linux下应用知识,如接入internet等等。

爱飞 发表于 2015-3-24 09:47:32

这也正是有别的OS得以存在的原因,每个系统都有其自身的优点。?
页: [1]
查看完整版本: 给大家带来用python剖析apache等web日记