我有兩個問題,我試圖解決,添加一列數據,如果從另一列的元素在字典
我想通過元素爲每個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