仓酷云

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 505|回复: 7
打印 上一主题 下一主题

[CentOS(社区)] 给大家带来用python剖析apache等web日记

[复制链接]
活着的死人 该用户已被删除
跳转到指定楼层
楼主
发表于 2015-1-14 20:21:47 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

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

[html]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

[html]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);">[html]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[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;">day=array[3][1:7];<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[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);">self.m_ip_days[ip].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[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);">self.m_ip_days[ip].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系统管理员建议多阅读有关硬件配置文章,对各种不支持或支持不太好的硬件有深刻的了解。
若相依 该用户已被删除
5#
发表于 2015-2-10 01:48:22 | 只看该作者
熟读写基础知识,学得会不如学得牢。
小妖女 该用户已被删除
6#
发表于 2015-2-28 17:59:01 | 只看该作者
学习Linux应具备的。[书籍+网络资源]
乐观 该用户已被删除
7#
发表于 2015-3-10 08:43:07 | 只看该作者
清楚了解网络的基础知识,特别是在Linux下应用知识,如接入internet等等。
爱飞 该用户已被删除
8#
发表于 2015-3-24 09:47:32 | 只看该作者
这也正是有别的OS得以存在的原因,每个系统都有其自身的优点。?
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|仓酷云 鄂ICP备14007578号-2

GMT+8, 2024-12-23 20:22

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表