os.popen(command):这种调用方式是通过管道的方式来实现,函数返回一个file对象,里面的内容是脚本输出的内容(可简单理解为echo输出的内容),使用os.popen调用的情况:
>>> import os
>>> cmd=os.popen("""netstat -nlpt|grep -v 'rpc'|awk -F "[ :]+" '{if($4 ~ /0.0.0.0/ || $4 ~ /127.0.0.1/) print $5}'""")
>>> cmd
<open file ‘netstat -nlpt|grep -v ‘rpc’|awk -F “[ :]+” ‘{if($4 ~ /0.0.0.0/ || $4 ~ /127.0.0.1/) print $5}’’, mode ‘r’ at 0x7f3113bc65d0>
>>> cmd.readlines()
[‘36220\n’, ‘2049\n’, ‘10050\n’, ‘111\n’, ‘80\n’, ‘22\n’, ‘8088\n’, ‘25\n’, ‘8089\n’]