栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

[Python] Pyshark统计pcap的packet协议字段并柱状图可视化

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

[Python] Pyshark统计pcap的packet协议字段并柱状图可视化

import pyshark
import collections
import matplotlib.pyplot as plt
import numpy as np

cap = pyshark.FileCapture('pcap/netatmoweatherPOWER_3.pcap', only_summaries=True)
protocolList = []
for packet in cap:
    line = str(packet)
    formattedLine = line.split(" ")
    protocolList.append(formattedLine[4])
counter = collections.Counter(protocolList)

plt.style.use('ggplot')
y_pos = np.arange(len(list(counter.keys())))
plt.bar(y_pos, list(counter.values()), align='center', alpha=0.5, color=['b', 'g', 'r', 'c', 'm'])
plt.xticks(y_pos, list(counter.keys()))
plt.ylabel("Frequency")
plt.xlabel("Protocol Name")
plt.savefig("ProtocolGraph.png")

报错解决

报的这个错 

Task was destroyed but it is pending!
task: >
C:UsersRyananaconda3libasynciobase_events.py:641: RuntimeWarning: coroutine 'Capture.close_async' was never awaited

更改后的代码如下

import asyncio
import pyshark
import collections
import matplotlib.pyplot as plt
import numpy as np

loop = asyncio.ProactorEventLoop()
asyncio.set_event_loop(loop)
cap = pyshark.FileCapture('pcap/netatmoweatherPOWER_3.pcap', only_summaries=True, eventloop=loop)
protocolList = []
for packet in cap:
    line = str(packet)
    formattedLine = line.split(" ")
    protocolList.append(formattedLine[4])
counter = collections.Counter(protocolList)

plt.style.use('ggplot')
y_pos = np.arange(len(list(counter.keys())))
plt.bar(y_pos, list(counter.values()), align='center', alpha=0.5, color=['b', 'g', 'r', 'c', 'm'])
plt.xticks(y_pos, list(counter.keys()))
plt.ylabel("Frequency")
plt.xlabel("Protocol Name")
plt.savefig("ProtocolGraph.png")
参考

PyShark - Create Protocol Graph - YouTube

Exception: got Future attached to a different loop · Issue #467 · KimiNewt/pyshark (github.com)

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/960525.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号