好友
阅读权限20
听众
最后登录1970-1-1
|
知心
发表于 2022-9-8 11:28
本帖最后由 知心 于 2022-9-8 11:41 编辑
[Python] 纯文本查看 复制代码 import json
data = {
'columns': ['family', 'proto', 'port', 'mac', 'ip', 'conns', 'rx_bytes', 'rx_pkts', 'tx_bytes', 'tx_pkts', 'layer7'],
'data': [
[4, 'TCP', 443, '34:23:87:61:86:27', '192.168.5.134',
6779, 66049721, 68653, 7605216, 45729, 'HTTPS'],
[4, 'IP', 0, '34:23:87:61:86:27', '192.168.5.134',
884, 22577571, 16577, 1008030, 19575, None],
[4, 'TCP', 80, '34:23:87:61:86:27', '192.168.5.134',
3332, 4631492, 7600, 1118669, 6983, 'HTTP'],
[4, 'TCP', 443, '00:11:32:94:5b:9e', '192.168.5.222',
1055, 2188864, 3920, 326644, 4072, 'HTTPS'],
[4, 'IP', 0, '00:11:32:94:5b:9e', '192.168.5.222',
2551, 49020, 620, 300476, 2151, None],
[4, 'IP', 0, '64:09:80:4d:bb:b0', '192.168.43.201',
500, 11704, 154, 13452, 177, None],
[4, 'UDP', 443, '34:23:87:61:86:27',
'192.168.5.134', 4, 51, 1, 5112, 4, 'QUIC']
]
}
def first(datas):
'''
根据第4个不同的元素进行分组
'''
dist1 = {}
for data in datas:
if data[3] not in dist1:
dist1[data[3]] = [data]
else:
dist1[data[3]].append(data)
return dist1
def second(datas):
'''
统计updata(第7个元素),downdata(第9个元素),totaldata(updata+downdata),合并成最终格式
'''
end_data = {}
for idx,mac_add in enumerate(datas):
updata = 0
downdata = 0
for item in datas[mac_add]:
updata +=item[6]
downdata +=item[8]
end_data['user'+str(idx+1)]={
"mac_add":mac_add,
"updata":str(updata),
"downdata":str(downdata),
"totaldata":str(updata+downdata)
}
return json.dumps(end_data,indent=4)
if __name__ == "__main__":
first_data = first(data["data"])
second_data = second(first_data)
print(second_data)
结果
[JavaScript] 纯文本查看 复制代码 {
"user1": {
"mac_add": "34:23:87:61:86:27",
"updata": "93258835",
"downdata": "9737027",
"totaldata": "102995862"
},
"user2": {
"mac_add": "00:11:32:94:5b:9e",
"updata": "2237884",
"downdata": "627120",
"totaldata": "2865004"
},
"user3": {
"mac_add": "64:09:80:4d:bb:b0",
"updata": "11704",
"downdata": "13452",
"totaldata": "25156"
}
} |
免费评分
-
查看全部评分
|