#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‘
执行结果