sendp() 函数在打印时会有 “Send 1 packet…” 的字样,那如何去掉呢?
进入到sendp() 函数内,找到如下两个函数。
加入verbose = 0 即可关闭print打印
其中os.write(1, “xxx”) 是试图对标准输出文件描述符写入“xxx”,若此刻程序没有涉及到命令行,特别是一些界面开发打包后是不带命令行的,那么这个文件描述符是不存在的,尝试写入就会出错,导致程序在此处崩溃。
def sendp(x, # type: _PacketIterable
iface=None, # type: Optional[_GlobInterfaceType]
iface_hint=None, # type: Optional[str]
socket=None, # type: Optional[SuperSocket]
**kargs # type: Any
):
# type: (...) -> Optional[PacketList]
"""
Send packets at layer 2
:param x: the packets
:param inter: time (in s) between two packets (default 0)
:param loop: send packet indefinitely (default 0)
:param count: number of packets to send (default None=1)
:param verbose: verbose mode (default None=conf.verb)
:param realtime: check that a packet was sent before sending the next one
:param return_packets: return the sent packets
:param socket: the socket to use (default is conf.L3socket(kargs))
:param iface: the interface to send the packets on
:param monitor: (not on linux) send in monitor mode
:returns: None
"""
if iface is None and iface_hint is not None and socket is None:
iface = conf.route.route(iface_hint)[0]
return _send(
x,
lambda iface: iface.l2socket(),
iface=iface,
socket=socket,
verbose=0, #<----------- 补充此语句,即刻取消打印
**kargs
)
def __gen_send(s, # type: Sup
erSocket
x, # type: _PacketIterable
inter=0, # type: int
loop=0, # type: int
count=None, # type: Optional[int]
verbose=None, # type: Optional[int]
realtime=False, # type: bool
return_packets=False, # type: bool
*args, # type: Any
**kargs # type: Any
):
# type: (...) -> Optional[PacketList]
"""
An internal function used by send/sendp to actually send the packets,
implement the send logic...
It will take care of iterating through the different packets
"""
if isinstance(x, str):
x = conf.raw_layer(load=x)
if not isinstance(x, Gen):
x = SetGen(x)
if verbose is None:
verbose = conf.verb
n = 0
if count is not None:
loop = -count
elif not loop:
loop = -1
sent_packets = PacketList() if return_packets else None
p = None
try:
while loop:
dt0 = None
for p in x:
if realtime:
ct = time.time()
if dt0:
st = dt0 + float(p.time) - ct
if st > 0:
time.sleep(st)
else:
dt0 = ct - float(p.time)
s.send(p)
if sent_packets is not None:
sent_packets.append(p)
n += 1
if verbose:
os.write(1, b".") # <--------此语句,涉及到命令行的打印,若是你的程序不涉及命令框,程序将在这里挂掉
time.sleep(inter)
if loop < 0:
loop += 1
except KeyboardInterrupt:
pass
finally:
try:
cast(Packet, x).sent_time = cast(Packet, p).sent_time
except AttributeError:
pass
if verbose:
print("\nSent %i packets." % n) # <--------- 此语句就是我们再命令行中看到的打印了
return sent_packets