python脚本实现统计日志文件中的ip访问次数代码分享
2014-08-06 00:00
1391 查看
适用的日志格式:
以上为nginx的日志,本程序只适合用于IP在开头并用空格分开的这种格式。
实例代码:
106.45.185.214 - - [06/Aug/2014:07:38:59 +0800] "GET / HTTP/1.0" 200 10 "-" "-" 171.104.119.22 - - [06/Aug/2014:08:55:01 +0800] "GET / HTTP/1.0" 200 10 "-" "-" 27.31.238.242 - - [06/Aug/2014:09:43:19 +0800] "GET / HTTP/1.0" 200 10 "-" "-" 218.65.202.131 - - [06/Aug/2014:10:33:59 +0800] "GET / HTTP/1.0" 200 10 "-" "-"
以上为nginx的日志,本程序只适合用于IP在开头并用空格分开的这种格式。
实例代码:
import sys class Log: def __init__(self, filename,dic,count): self.filename = filename self.dic=dic self.count=count def parse(self): i=1 f=file(self.filename) while True: line=f.readline() if len(line)==0: break ip=line.split(' ') if ip[0] in dic: self.dic[ip[0]]=self.dic[ip[0]]+1 else: self.dic[ip[0]]=i soredic=sorted(self.dic.items(), key=lambda d:d[1],reverse=True) counts=0; for item in soredic: if counts==int(self.count): break print("IP:%s Total Times: %s"%(item[0],item[1])) counts=counts+1 f.close() if __name__=="__main__": if len(sys.argv)<3: print('usage:log.py log.log toptimes\nexample log.py log.log 20\ncode by iswin') sys.exit() dic={} log=Log(sys.argv[1],dic,sys.argv[2]) log.parse()
相关文章推荐
- python脚本实现统计日志文件中的ip访问次数代码分享
- 使用python 分析统计nginx访问日志ip次数并且排序
- Linux通过日志文件统计IP访问次数排序
- python 脚本统计squid日志中的IP访问数和URL访问数量
- 本文以python实现了一个日志文件中ip提取与统计程序
- Python脚本实现代码行数统计代码分享
- Python脚本实现代码行数统计代码分享
- 海量日志数据提取出访问次数最多的那个IP python实现 总结
- 系统访问日志统计及邮件发送脚本--python实现
- python统计日志ip访问数脚本
- 海量日志数据提取出访问次数最多的那个IP python实现 总结
- .net页面访问次数统计实现原理与代码
- Python 统计日志中每个IP出现的次数
- 将C++代码全部写到头文件:)python脚本帮助自动生成相应的实现文件初始框架
- shell脚本中使用iconv实现批量文件转码的代码分享
- 如何统计日志里面访问次数最多的IP[转]
- 海量日志数据,提取出某日访问百度次数最多的那个IP的C++实现源代码
- Hadoop兮,杀鸡别用牛刀,python+shell实现一般日志文件的查询、统计
- python自动化工具日志查询分析脚本代码实现
- shell脚本实现拷贝大文件显示百分比的代码分享