I execute tcpdump in a subprocess as follows:
pcap_process = subprocess.Popen(['tcpdump', '-s 0', '-w -', 'tcp'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
The -w - argument -w - important: it tells tcpdump print the resulting .pcap file to stdout .
Then I will go to the website using urllib.open() . After that, I would like to kill tcpdump and put everything that it printed into a line. I tried the following:
pcap_process.terminate() result = pcap_process.stdout.read() # or readline(), etc.
But (if I am not doing something wrong), this does not work; I killed the process, now there is nothing left to read. If I use read() or communicate() before exiting, my script will just sit and read and continue, waiting for tcpdump finish (which will not happen).
Is there a way to do this (preferably without loops)?
sebastian_k
source share