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)


![[Python] Pyshark统计pcap的packet协议字段并柱状图可视化 [Python] Pyshark统计pcap的packet协议字段并柱状图可视化](http://www.mshxw.com/aiimages/31/960525.png)
