37,741
社区成员
发帖
与我相关
我的任务
分享
import re
from datetime import datetime
# un-comment this line
# s = open(r'/work/data/base_data.txt').read()
# comment these lines
s = '''2012-04-18 12:33:33 192.168.13.106 218.16.121.240 80
2012-04-18 12:33:43 192.168.13.106 110.75.187.22 80
2012-04-18 12:34:13 192.168.65.27 192.168.0.188 443
2012-04-18 12:34:27 192.168.40.117 192.168.0.174 80
2012-04-18 12:35:39 192.168.20.109 119.147.113.98 80
2012-04-18 12:35:59 192.168.20.109 119.147.113.98 80
2012-04-18 12:36:13 192.168.65.27 192.168.0.189 443
2012-04-18 12:36:20 192.168.13.106 113.11.195.106 80
2012-04-18 12:36:26 192.168.50.112 192.168.0.174 80
2012-04-18 12:36:33 192.168.50.146 118.186.66.51 80
2012-04-18 12:36:43 192.168.30.105 192.168.0.174 80
2012-04-18 12:36:53 192.168.50.145 119.147.194.250 80
2012-04-18 12:37:01 192.168.40.105 192.168.0.174 80
2012-04-18 12:37:12 192.168.13.106 182.50.0.106 80
2012-04-18 12:37:33 192.168.13.106 182.50.0.106 80
2012-04-19 12:34:13 192.168.65.27 192.168.0.188 443'''
# srcIP->destIP:port = date time
dateDict = {}
# srcIP->destIP:port = number of slots
slotDict = {}
# total number of slots
totalNum = 0
# loop
for line in s.split('\n'):
items = line.split(' ')
if len(items)==5:
# total time slot
totalNum += 1
# new key
newkey = items[-3]+'->'+items[-2]+':'+items[-1]
# dateDict
if dateDict.has_key(newkey):
dateDict[newkey].append(items[0]+' '+items[1])
else:
dateDict[newkey] = [items[0]+' '+items[1]]
# slotDist
if slotDict.has_key(newkey):
slotDict[newkey] += 1
else:
slotDict[newkey] = 0
# write files
for k in slotDict.keys():
# ratio
ratio = slotDict[k]*1.0/totalNum
# line string
newline = k+', '+str(int(ratio*100))+'%\n'
# open file
if ratio>0.9:
fid = open('high.txt','a+')
elif 0.6<ratio<=0.9:
fid = open('middle.txt','a+')
elif ratio<=0.6:
fid = open('low.txt','a+')
# write, close
fid.write(newline)
fid.close()
print 'DONE!'
192.168.65.27->192.168.0.189:443, 0%
192.168.50.112->192.168.0.174:80, 0%
192.168.50.146->118.186.66.51:80, 0%
192.168.13.106->218.16.121.240:80, 0%
192.168.30.105->192.168.0.174:80, 0%
192.168.50.145->119.147.194.250:80, 0%
192.168.40.105->192.168.0.174:80, 0%
192.168.20.109->119.147.113.98:80, 6%
192.168.65.27->192.168.0.188:443, 6%
192.168.13.106->113.11.195.106:80, 0%
192.168.13.106->182.50.0.106:80, 6%
192.168.13.106->110.75.187.22:80, 0%
192.168.40.117->192.168.0.174:80, 0%
192.168.65.27->192.168.0.189:443, 0%
192.168.50.112->192.168.0.174:80, 0%
192.168.50.146->118.186.66.51:80, 0%
192.168.13.106->218.16.121.240:80, 0%
192.168.30.105->192.168.0.174:80, 0%
192.168.50.145->119.147.194.250:80, 0%
192.168.40.105->192.168.0.174:80, 0%
192.168.20.109->119.147.113.98:80, 6%
192.168.65.27->192.168.0.188:443, 6%
192.168.13.106->113.11.195.106:80, 0%
192.168.13.106->182.50.0.106:80, 6%
192.168.13.106->110.75.187.22:80, 0%
192.168.40.117->192.168.0.174:80, 0%