I'm trying to get familiar with subprocess.Popen mechanism.
In the bellow example, I try to run netstat then run grep on the output.
netstat = subprocess.Popen("netstat -nptl".split(), stdout = subprocess.PIPE)
grep = subprocess.Popen("grep 192.168.46.134".split(), stdin = subprocess.PIPE)
However this does not result in a desired output.
答案 0 :(得分:1)
You need to reference the first process' stdout
as the stdin
for the second process:
import subprocess
netstat = subprocess.Popen("netstat -nptl".split(), stdout=subprocess.PIPE)
grep = subprocess.Popen("grep 192.168.46.134".split(), stdin=netstat.stdout, stdout=subprocess.PIPE)
stdout, stderr = grep.communicate()
print stdout # this is a string containing the output