Python统计日志中每个IP出现次数
介绍了Python统计日志中每个IP出现次数的方法,实例分析了Python基于正则表达式解析日志文件的相关技巧,需要的朋友可以参考下
本脚本可用于多种日志类型
#-*- coding:utf-8 -*-
import re,time
def mail_log(file_path):
global count
log=open(file_path,'r')
C=r'\.'.join([r'\d{1,3}']*4)
find=re.compile(C)
count={}
for i in log:
for ip in find.findall(i):
count[ip]=count.get(ip,1)+1
if __name__ == '__main__':
print time.clock()
num=0
mail_log(r'e:\MDaemon-20110329-all.log')
R=count.items()
for i in R:
if i[1]>0: #提取出现次数大于0的IP
print i
num+=1
print '符合要求数量:%s耗时(%s)'%(num,time.clock())
输出结果如下:
('206.220.200.250', 8)
('66.40.52.37', 10)
('66.40.52.36', 5)
('207.115.11.41', 4)
('96.47.193.25', 9)
('96.47.193.24', 5)
('96.47.193.23', 17)
('72.32.181.92', 5)
('67.76.103.168', 10)
('64.34.161.218', 5)
('209.151.96.3', 7)
('61.135.168.0', 15)
('199.81.128.37', 2)
('199.81.128.36', 2)
('199.81.128.38', 2)
('198.45.19.170', 16)
('12.236.15.9', 4)
('66.96.142.52', 51)
('66.96.142.51', 55)
('66.96.142.50', 62)
('64.18.5.13', 1553)
('69.39.47.14', 9)
('64.18.5.11', 1557)
('64.18.5.10', 2752)
('210.72.13.102', 4)
('64.118.108.196', 4)
('66.60.192.44', 26)
('112.90.194.8', 4)
('198.49.244.245', 5)
('216.183.174.227', 5)
('195.245.230.131', 5)
('211.115.13.27', 5)
('222.247.123.217', 3)
('218.213.85.210', 2)
('201.236.205.96', 3)
('209.85.161.136', 2)
('173.165.120.188', 5)
('50.22.89.39', 7)
('219.129.20.168', 3)
('24.106.197.167', 5)
('207.190.225.69', 4)
('156.3.32.236', 5)
('209.92.157.161', 5)
('216.153.192.200', 5)
('76.77.158.130', 3)
('12.166.4.221', 5)
('66.46.182.96', 4)
('80.252.97.102', 4)
('66.46.182.94', 5)
('66.46.182.95', 4)
('124.14.5.3', 3)
('202.85.139.0', 5)
('207.173.160.17', 15)
('143.101.0.21', 5)
('65.75.75.59', 9)
('77.88.21.89', 53)
('216.128.11.30', 44)
原文转载:http://www.jb51.net/article/69011.htm
Python统计日志中每个IP出现次数的更多相关文章
- python 统计字符串中指定字符出现次数的方法
python 统计字符串中指定字符出现次数的方法: strs = "They look good and stick good!" count_set = ['look','goo ...
- 使用python脚本实现统计日志文件中的ip访问次数
使用python脚本实现统计日志文件中的ip访问次数,注意此脚本只适用ip在每行开头的日志文件,需要的朋友可以参考下 适用的日志格式: 106.45.185.214 - - [06/Aug/2014: ...
- 统计日志中ip出现的次数
grep -r 'GET /weixin/weixin_izp/index.html' ./chunyun.access.log > ~/access.log cat access.log |a ...
- 用shell统计访问日志里每个ip访问次数【转】
今天,要统计一个系统的每个ip访问次数,于是我找到该系统访问日志,并写脚本实现. 访问日志情况: [root@qular ~]# cd /usr/local/nginx/logs/ [root@q ...
- Python统计列表中的重复项出现的次数的方法
本文实例展示了Python统计列表中的重复项出现的次数的方法,是一个很实用的功能,适合Python初学者学习借鉴.具体方法如下:对一个列表,比如[1,2,2,2,2,3,3,3,4,4,4,4],现在 ...
- python统计文本中每个单词出现的次数
.python统计文本中每个单词出现的次数: #coding=utf-8 __author__ = 'zcg' import collections import os with open('abc. ...
- 配置日志中显示IP
package com.demo.conf; import ch.qos.logback.classic.pattern.ClassicConverter; import ch.qos.logback ...
- Python习题-统计日志中访问次数超过限制的IP
#1.1分钟之内ip访问次数超过200次的,就给他的ip加入黑名单#需求分析: #1.读日志,1分钟读一次 #2.获取这1分钟之内所有访问的ip #3.判断ip出现的次数,如果出现200次,那么就加入 ...
- Python监控日志中经常访问的ip
一.需求:每分钟检查一次日志文件,如果这一分钟内同一个ip请求次数超过200次,加入黑名单 1.日志文件中,每一行的格式为:XXX.XXX.XXX.XXX - - [04/Jun/2017:05:25 ...
随机推荐
- js中的构造函数
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 腾讯云极速配置NodeJS+LNMP运行环境
版权声明:本文由吴逸翔原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/848754001487150669 来源:腾云阁 h ...
- 整型数字转utf8
static std::string codePointToUTF8(unsigned int cp){ std::string result; // based on description fro ...
- D - Lake Counting
Due to recent rains, water has pooled in various places in Farmer John's field, which is represented ...
- Project Move from Qt 4 to Qt 5 项目工程的迁移
将Qt4的项目迁到Qt5中并不需要新建一个Qt5的工程,可以直接在原工程文件上修改,这里我们使用的是VS2010和Qt5.4的环境,我们需要做以下修改: 1. 在工程里找到这个文件:工程名.vcxpr ...
- ie9 form submit 请求参数问题替代办法
//隐藏表单 <input id="hdPeriod" name="period" type="hidden" value=" ...
- PAT甲级1013-1014-1015
题目:1013 Battle Over Cities 思路:城市数也就1000, 对于每次询问暴力bfs一下看一下有多少连通块就行了.答案就是联通块数减一. #include<stdio.h&g ...
- 如何通过automator创建自动化备份任务?
Windows用户对于Task Scheduler应该不陌生,但是到了macOS因该用什么呢?那就是automator,今天因为有一个个人使用需求:备份一个移动硬盘中的文件,但是又不想完全手动.怎么办 ...
- python中的过滤fliter
movie_people = ['sb_alex', 'sb_wupeiqi', 'hello'] def filter_test(array): ret = [] for p in array: i ...
- hbase与hive集成:hive读取hbase中数据
1.创建hbase jar包到hive lib目录软连接 hive需要jar包: hive-hbase-handler-0.13.1-cdh5.3.6.jar zookeeper-3.4.5-cdh5 ...