Jun-26-2018, 01:17 PM
Cannot be done, you say ? This was done on Ubuntu
Output:In [23]: ping_proc = subprocess.Popen(['ping', 'google.com'],
...: stdout=subprocess.PIPE, stdin=subprocess.PIPE,
...: stderr=subprocess.STDOUT, universal_newlines=True)
In [24]: for line_no, line in enumerate(ping_proc.stdout):
...: print(line,)
...: if line_no == 10:
...: print('Killing process')
...: ping_proc.kill()
...: break
...:
64 bytes from lhr25s10-in-f14.1e100.net (216.58.198.174): icmp_seq=3 ttl=50 time=66.0 ms
64 bytes from lhr25s10-in-f14.1e100.net (216.58.198.174): icmp_seq=4 ttl=50 time=66.1 ms
64 bytes from lhr25s10-in-f14.1e100.net (216.58.198.174): icmp_seq=5 ttl=50 time=66.3 ms
64 bytes from lhr25s10-in-f14.1e100.net (216.58.198.174): icmp_seq=6 ttl=50 time=66.0 ms
64 bytes from lhr25s10-in-f14.1e100.net (216.58.198.174): icmp_seq=7 ttl=50 time=66.2 ms
64 bytes from lhr25s10-in-f14.1e100.net (216.58.198.174): icmp_seq=8 ttl=50 time=66.1 ms
64 bytes from lhr25s10-in-f14.1e100.net (216.58.198.174): icmp_seq=9 ttl=50 time=66.1 ms
64 bytes from lhr25s10-in-f14.1e100.net (216.58.198.174): icmp_seq=10 ttl=50 time=66.2 ms
Killing process
I redirect stderr
to stdout
- some processes use stderr
instead of stdout
, so suppressing the former is a bad idea
Test everything in a Python shell (iPython, Azure Notebook, etc.)
- Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
- Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
- You posted a claim that something you did not test works? Be prepared to eat your hat.