首页 > 编程语言 > 详细

python 写的日志分析

时间:2015-09-12 01:04:48      阅读:288      评论:0      收藏:0      [点我收藏+]

#coding=utf-8

#!/usr/bin/python

import string

import re

import sys

file =  sys.argv[1]

f = open(file, ‘r‘)

ip_url = []

ip = []

url_url = []

while True:

    line = f.readline()

    if not line:break

    line1 = line.split()

    clientIP = line1[0]

    status = line1[8]

    url = line1[6]

    ip_url.append(status)

    ip.append(clientIP)

    url_url.append(url)

ip_list = list(set(ip))

url_list = list(set(url_url))

status_list = list(set(ip_url))

print "#######统计各个访问状态数#############"


dict = {}

for i in status_list:

    dict[ip_url.count(i)] = i

cc = sorted(dict.iteritems(), reverse = True )

for key in cc:

    print ‘\033[1;31;40m‘

    print ‘状态是‘,

    print key[1],

    print ‘的有‘,

    print key[0],

    print ‘个‘

    print ‘\033[0m‘

print "#######统计各个IP状态数#############"

print ‘\033[1;31;40m‘

print ‘UV是‘,

print len(ip_list),

print ‘个‘

print ‘\033[0m‘

print "#####################################"

dict1 = {}

for i in ip_list:

    dict1[ip.count(i)] = i

dd = sorted(dict1.iteritems(), reverse = True )

for key1 in dd:

    print ‘\033[1;38;40m‘

    print ‘访问IP为‘,

    print key1[1],

    print ‘的有‘,

    print key1[0],

    print ‘个‘

    print ‘\033[0m‘


print "#######统计各个URL状态数#############"

print ‘\033[1;31;40m‘

print ‘PV是‘,

print len(url_url),

print ‘个‘

print ‘\033[0m‘

print "#####################################"


dict2 = {}

for i in url_list:

    dict2[url_url.count(i)] = i

dd = sorted(dict2.iteritems(), reverse = True )

for key1 in dd:

    print ‘\033[1;33;40m‘

    print ‘访问此URL:‘,

    print key1[1],

    print ‘的有‘,

    print key1[0],

    print ‘个‘

    print ‘\033[0m‘



执行结果

技术分享

本文出自 “信不信由你” 博客,请务必保留此出处http://312461613.blog.51cto.com/965442/1693896

python 写的日志分析

原文:http://312461613.blog.51cto.com/965442/1693896

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!