简体   繁体   English

Python 使用 Scapy 创建空 pcap 文件时抑制警告

[英]Python supress warnings while creating a empty pcap file with Scapy

I want to create an empty pcap file.我想创建一个空的pcap文件。 I'm using wrpcap module of 'Scapy'.我正在使用“Scapy”的wrpcap模块。 But wrpcap takes exactly 2 arguments : one is the file name and other is the packet list like:但是wrpcap恰好需要2 arguments :一个是file name ,另一个是packet list ,如:

wrpcap("my.pcap",my_pkt_list)

Since I want to make it, empty and I don't have a packet list, I'm writing an empty string to the pcap file.因为我想把它设为空,而且我没有数据包列表,所以我正在向pcap文件写入一个空字符串。 Which is creating the file but also giving a warning as well as errors since a string doesn't match to a packet type.这是在创建文件,但也会给出警告和错误,因为字符串与数据包类型不匹配。

WARNING: PcapWriter: unknown LL type for str. Using type 1 (Ethernet)
Traceback (most recent call last):
  File "test.py", line 35, in <module>
    wrpcap("pcap/FU.pcap","")
  File "/usr/lib/python2.7/site-packages/scapy/utils.py", line 466, in wrpcap
    PcapWriter(filename, *args, **kargs).write(pkt)
  File "/usr/lib/python2.7/site-packages/scapy/utils.py", line 646, in write
    self._write_packet(pkt)
  File "/usr/lib/python2.7/site-packages/scapy/utils.py", line 688, in _write_packet
    sec = int(packet.time)
AttributeError: 'str' object has no attribute 'time'

For now, I'm able to suppress the errors with try and except but unable to suppress the warning.现在,我可以使用try and except来抑制错误,但无法抑制警告。

Code代码

from scapy.all import *
try:
    wrpcap("my.pcap","")
except:
    pass

and the warning is still there:并且警告仍然存在:

WARNING: PcapWriter: unknown LL type for str. Using type 1 (Ethernet)

How to suppress it from inside the python code?如何从 python 代码中抑制它?

Python has a built in method in the standard library for supressing warnings: Python在标准库中有一个内置方法来禁止警告:

import warnings
warnings.simplefilter("ignore", Warning)

You can read more about the warnings library in the docs . 您可以在docs中阅读有关警告库的更多信息。

EDIT 编辑

It doesn't look like scapy uses the warnings library to generate this. 看起来scapy并没有使用warnings库来生成warnings Instead it logs them to a logger called scapy.runtime with a level of warning . 相反,它将它们记录到名为scapy.runtime 的记录器中, scapy.runtime带有warning级别。 There's obviously some default logging config to echo that to stdout . 显然,有一些默认的日志记录配置可以将其回显到stdout You could add your own logging handler for scapy.runtime to supress them. 您可以为scapy.runtime添加自己的日志记录处理程序以scapy.runtime它们。

You can avoid that warning by using PcapWriter method of scapy. 您可以通过使用PcapWriter方法避免该警告。

from scapy.all import *
try:
    writer=PcapWriter("my.pcap")
except:
    pass

This creates your empty pcap file. 这将创建您的空pcap文件。 When you want to write some packets to it, just use the following code: 要向其中写入一些数据包时,只需使用以下代码:

writer.write(<Your_packets>)
writer.flush()

You can suppress this warning by disabling warning logging before the call wrpcap :您可以通过在调用wrpcap之前禁用警告日志记录来抑制此警告:

import logging
logging.getLogger("scapy.runtime").setLevel(logging.ERROR)

The text "WARNING: ..." is not part of the exception and will thus not be surpressed by your try clause. 文本“ WARNING:...”不是例外,因此您的try子句不会对其造成干扰。

An idea I got was to redirect stdout during the call 我的想法是在通话期间重定向标准输出

from scapy.all import *
try:
    old_stdout = sys.stdout
    sys.stdout = None
    wrpcap("my.pcap","")
    sys.stdout = old_stdout
except:
    pass

that should surpress any and all output during the call 在通话期间应抑制所有输出

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM