我有两个问题,我试图解决,添加一列数据,如果从另一列的元素在字典
我想通过元素为每个IP地址在字典
frequency4
元素检查在它被存储后,如果该IP地址在文本文件中的数据行中的column[4]
中,它将继续在数据文件中添加该确切ip的字节量。如果
column[8]
bytes
下包含一个“M”的含义亿美元,这将使得M转换成“* 1000000”等于3300(请参阅下面的文本文件中的数据),请记住,这是文本文件的样本,该文本文件包含数千行数据。
我要找的输出是:
Total bytes for ip 172.217.9.133 is 33000000
Total bytes for ip 205.251.24.253 is 9516
Total bytes for ip 52.197.234.56 is 14546
CODE
from collections import OrderedDict
from collections import Counter
frequency4 = Counter({})
ttlbytes = 0
with open('/Users/rm/Desktop/nettestWsum.txt', 'r') as infile:
next(infile)
for line in infile:
if "Summary:" in line:
break
try:
srcip = line.split()[4].rsplit(':', 1)[0]
frequency4[srcip] = frequency4.get(srcip,0) + 1
f4 = OrderedDict(frequency4.most_common())
for srcip in f4:
ttlbytes += int(line.split()[8])
except(ValueError):
pass
print("\nTotal bytes for ip",srcip, "is:", ttlbytes)
for srcip, count in f4.items():
print("\nIP address from destination:", srcip, "was found:", count, "times.")
DATA文件
Date first seen Duration Proto Src IP Addr:Port Dst IP Addr:Port Packets Bytes Flows
2017-04-11 07:23:17.880 929.748 UDP 172.217.9.133:443 -> 205.166.231.250:41138 3019 3.3 M 1
2017-04-11 07:38:40.994 6.676 TCP 205.251.24.253:443 -> 205.166.231.250:24723 16 4758 1
2017-04-11 07:38:40.994 6.676 TCP 205.251.24.253:443 -> 205.166.231.250:24723 16 4758 1
2017-04-11 07:38:41.258 6.508 TCP 52.197.234.56:443 -> 205.166.231.250:13712 14 7273 1
2017-04-11 07:38:41.258 6.508 TCP 52.197.234.56:443 -> 205.166.231.250:13712 14 7273 1
Summary: total flows: 22709, total bytes: 300760728, total packets: 477467, avg bps: 1336661, avg pps: 265, avg bpp: 629
Time window: 2017-04-11 07:13:47 - 2017-04-11 07:43:47
Total flows processed: 22709, Blocks skipped: 0, Bytes read: 1544328
Sys: 0.372s flows/second: 61045.7 Wall: 0.374s flows/second: 60574.9
这肯定工作,但它确实需要齐头并进,从词典frequency4因为我的项目,我还指望该事件的一部分IP地址的数据显示,并将其从最常见它们排序不太频繁,所以我也想提供不仅如此,而且它提供的总字节数。 – k5man001