Skip to content

Instantly share code, notes, and snippets.

@john100ster
Last active May 27, 2021 00:12
Show Gist options
  • Save john100ster/da19450bcd39d37a42cd512cb8e8375c to your computer and use it in GitHub Desktop.
Save john100ster/da19450bcd39d37a42cd512cb8e8375c to your computer and use it in GitHub Desktop.
[解析文件中的 ip 地址] #python
# -*- coding:utf-8 -*-
import re
import 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'./session-17-43')
R = count.items()
for i in R:
if i[1] > 0: # 提取出现次数大于0的IP
print(i)
num += 1
print('符合要求数量:%s耗时(%s)' % (num, time.clock()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment