我尝试了许多描述的方法,但它不适用于我。谁能解释我如何使用子进程在单个 python 脚本中使用它?
iperf -c 10.0.0.1 -i 1 -t 100 | grep -Po '[0-9.]*(?= Mbits/sec)'
我尝试了许多描述的方法,但它不适用于我。谁能解释我如何使用子进程在单个 python 脚本中使用它?
iperf -c 10.0.0.1 -i 1 -t 100 | grep -Po '[0-9.]*(?= Mbits/sec)'
所以我已经解决了这个问题。这个想法是将这两个命令与不同的子进程一起使用。首先创建进程iperf
并将该进程的输出输入到命令stdin
的第二个进程中,grep
如下所示:
process1 = subprocess.Popen(["iperf","-c", 10.10.0.1], stdout=subprocess.PIPE)
prrocess2 = subprocess.Popen(["grep", "-Po",[0-9.]*(?= Mbits/sec)], stdin=process1.stdout, stdout=subprocess.PIPE).communicate()[0]