linux tcpdump命令行执行后回显是一行一行输出的,但是如果用python的subprocess执行tcpdump命令就会成为一段一段的回显,实时性不高。这个怎么解决?有人说是tcpdump本身的原因,和linux缓存有关系,但是python有没有办法实现和命令行一行一行输出的效果。
代码如下
#coding=utf-8
import subprocess
p = subprocess.Popen("tcpdump udp -i eth0",shell=True,stdout=subprocess.PIPE)
while p.stdout.readline()!=' ':
line = p.stdout.readline()
line = line.strip()
print line
tcpdump
-l
optionsThe code you gave has a bug in this line, while p.stdout.readline()!=' '
Correction as follows!
I also made this, used to passively start a certain port service!