1

Hello I'm really new to the Python programming language and i have encountered a problem writing one script. I want to save the output from stdout that i obtain when i run a tcpdump command in a variable in a Python script, but i want the tpcdump command to run continuously because i want to gather the length from all packets transferred that get filtered by tcpdump(with the filter i wrote). I tried :

    fin, fout = os.popen4(comand)
    result = fout.read()
    return result

But it just hangs.

1 Answer 1

3

I'm guessing that it hangs because os.popen4 doesn't return until the child process exits. You should be using subprocess.Popen instead.

import subprocess
import shlex  #just so you don't need break "comand" into a list yourself ;)

p=subprocess.Popen(shlex.split(comand),stdout=subprocess.PIPE)
first_line_of_output=p.stdout.readline()
second_line_of_output=p.stdout.readline()
...
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. That worked. At first i tried from the command line and it didn't seem to work but when i wrote a script it did. Also thanks for the promptness :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.