import shlex
import subprocess
if __name__ == '__main__':
shell_cmd = 'ping baidu.com'
cmd = shlex.split(shell_cmd)
p = subprocess.Popen(cmd, shell=False, stdout=subprocess.PIPE, encoding='utf8')
while p.poll() is None:
line = p.stdout.readline()
line = line.strip()
if line:
print('Subprogram output: {}'.format(line))
if p.returncode == 0:
print('Subprogram success')
else:
print('Subprogram failed')