0

I have a simple subprocess setup to execute a linux command inside Python3.

As for a ping command, I can easily get real time output. However, for tshark command, I get the output only after the output 'accumulates' for about ~30 lines and then python prints the output.

Any ideas on how to get real time output for tshark?

EDIT: print(path, flush=True) does not fix the issue.

from subprocess import Popen, PIPE

def run(command):
    process = Popen(command, stdout=PIPE, stderr=PIPE, shell=True)
    while True:
        line = process.stdout.readline().rstrip()
        if not line:
            break
        yield line


if __name__ == "__main__":
#    for path in run("ping -c 5 google.com"): # ping commands works just fine
    for path in run("sudo tshark -i wlp4s0"):
        print(path)
hrk
  • 55
  • 1
  • 9
  • 1
    See: buffering. This is default behavior when stdout is *anything* other than a terminal. You get the same thing when you pipe to less, for example. – Charles Duffy Jul 25 '20 at 16:01
  • And why does it work for `ping` command? – hrk Jul 25 '20 at 16:18
  • 1
    Because ping explicitly flushes its writes, so stdout's default buffering settings don't matter to it. – Charles Duffy Jul 25 '20 at 16:27
  • 1
    Your problem is that *Wireshark* isn't flushing, not that *Python* isn't flushing (so of course adding a flush to your Python code won't make any difference). Read the linked duplicate's answers more carefully, specifically including the ones advising use of `stdbuf` to wrap your subprocess. – Charles Duffy Jul 25 '20 at 16:29
  • 1
    Thanks for your help. Indeed I have to tell wireshark to explicitly flush the output. The problem is fixed when you pass `-l` filter to tshark. For anybody interested, see -> https://osqa-ask.wireshark.org/questions/27357/how-to-pipe-tshark-output-in-realtime – hrk Jul 25 '20 at 16:34

0 Answers0