如何通过集成WireShark监控基于Python的包的网络流量?
How to monitor the network traffic of a Python-based package by incorporating WireShark?
我正在寻找方法来监控基于 Python 的软件包的网络流量。一种建议的方法是将 WireShark 合并到包中。我以前从来没有做过这样的事情,也无法在 Internet 上找到任何与此相关的教程。而且,无论我能收集到什么,似乎都建议使用 PyShark 作为包装器来执行任务。有人可以给我一些指导,例如代码片段或关于如何处理任务的指示吗?任何帮助将非常感激。
发现我可能可以使用 TShark
或 pyshark
来监控 Python-based 包的网络流量。有关详细信息,请参阅 https://www.wireshark.org/docs/man-pages/tshark.html 处的官方文档。可以按照以下来源了解我最初想做的事情:
2019 年 7 月 17 日更新:
所以我要注意的主要事情是注意pyshark中有两种方法可以用来捕获数据包,即FileCapture
或LiveCapture
。 FileCapture
主要用于从捕获文件中读取,所以它对我来说用处不大,因为我想捕获一些实时事件。另一方面,LiveCapture
用于从实时界面读取,因此我选择使用它来监控实时网络流量。所以我写了下面的代码片段来捕获在我的笔记本电脑上传输的一些 tcp 数据包:
@staticmethod
def get_packet_info(interface=None):
"""
Returns the size of the transmitted data using Wireshark.
Args:
interface: A string. Name of the interface to sniff on.
Returns: Size of the packet sent over WebSockets in a given event.
"""
if interface is None:
raise Exception("Please provide the interface used.")
else:
capture = pyshark.LiveCapture(interface=interface)
capture.sniff(timeout=60)
for packet in capture:
try:
packet_info = packet.pretty_print()
except:
raise Exception("Cannot determine packet info.")
return packet_info
sniff
的参数可以从 timeout
更改为类似 packet_count
的内容。此外,我可以向 LiveCapture 添加更多属性以实现更好的控制。
我正在寻找方法来监控基于 Python 的软件包的网络流量。一种建议的方法是将 WireShark 合并到包中。我以前从来没有做过这样的事情,也无法在 Internet 上找到任何与此相关的教程。而且,无论我能收集到什么,似乎都建议使用 PyShark 作为包装器来执行任务。有人可以给我一些指导,例如代码片段或关于如何处理任务的指示吗?任何帮助将非常感激。
发现我可能可以使用 TShark
或 pyshark
来监控 Python-based 包的网络流量。有关详细信息,请参阅 https://www.wireshark.org/docs/man-pages/tshark.html 处的官方文档。可以按照以下来源了解我最初想做的事情:
2019 年 7 月 17 日更新:
所以我要注意的主要事情是注意pyshark中有两种方法可以用来捕获数据包,即FileCapture
或LiveCapture
。 FileCapture
主要用于从捕获文件中读取,所以它对我来说用处不大,因为我想捕获一些实时事件。另一方面,LiveCapture
用于从实时界面读取,因此我选择使用它来监控实时网络流量。所以我写了下面的代码片段来捕获在我的笔记本电脑上传输的一些 tcp 数据包:
@staticmethod
def get_packet_info(interface=None):
"""
Returns the size of the transmitted data using Wireshark.
Args:
interface: A string. Name of the interface to sniff on.
Returns: Size of the packet sent over WebSockets in a given event.
"""
if interface is None:
raise Exception("Please provide the interface used.")
else:
capture = pyshark.LiveCapture(interface=interface)
capture.sniff(timeout=60)
for packet in capture:
try:
packet_info = packet.pretty_print()
except:
raise Exception("Cannot determine packet info.")
return packet_info
sniff
的参数可以从 timeout
更改为类似 packet_count
的内容。此外,我可以向 LiveCapture 添加更多属性以实现更好的控制。