r45i r45i - 4 months ago 34
Python Question

Subprocess doesn't show data from tcpdump in realtime. It shows with a pause about 10-20 seconds

So i wanna get all data from

tcpdump
and add some logic in the future.
I haven't such problem yet with subprocess' pipes.
I wrote code and ran
tcpdump
and
run.py
in parallel.

run.py:

from subprocess import Popen, PIPE

# process = Popen(['/usr/bin/sudo', '/usr/sbin/tcpdump', '-i', 'wlan0'], bufsize=1, stdout=PIPE, stderr=PIPE)
process = Popen('sudo tcpdump -i wlan0', bufsize=1, universal_newlines=True, shell=True, stdout=PIPE, stderr=PIPE)
while True:
print(process.stdout.readline())


Output looks like this:
enter image description here
I tried different values for
bufsize
and other but behavior hasn't changed.
How can i get output as fast as tcpdump gets with
Subprocess.Popen
?

Answer

It's stdio buffering in tcpdump process.
By default stdio sets the buffering mode to _IOFBF(full) on redirected streams.
Luckily tcpdump has -l option which switches the mode to line-buffered:

process = Popen('sudo tcpdump -l -i wlan0', bufsize=1, universal_newlines=True, 
                shell=True, stdout=PIPE, stderr=PIPE)

Andrea's solution also works but mine would work on windows too.

Comments