Friday, 27 September 2013

Catching Terminal Output in Python

Catching Terminal Output in Python

I want to do something similar to the second answer here (but not quite
similar): Simulate Ctrl-C keyboard interrupt in python while working in
Linux
It's much simpler and I think I'm just missing something. Say, from a
python script, I just want to call 'ping' and terminate it after the 10th
time. I'm trying to do it like from the link above:
p = subprocess.Popen(['ping', 'google.com'], stdout=subprocess.PIPE)
for line in p.stdout:
print line
if re.search('10', line):
break
os.kill(p.pid, signal.SIGINT)
But it doesn't work.
And I also want the regular output of 'ping' to be displayed. How do I do
this?

No comments:

Post a Comment