其中system()在php中打印python的输出


where system() prints output of python in php?

在控制台运行一个python脚本时,python会打印一些行例如

hello,
World.
...
...

对于使用php在网页上显示,我尝试过。。

exec("sudo -u username python temp.py  >> logdata.log")

并打印logdata.log。但在这方面,我在网页上得到了混乱的输出。我发现这是刷新数据的问题。

我在谷歌搜索时读到了system()命令,它执行外部命令并打印输出。同时,它还会在发生时刷新输出。所以我试过了。。

system("sudo -u username Python temp.py")

我想知道它在哪里打印输出。

为什么不使用popen。

os.popen('sudo -u username Python temp.py').read()

或更好的子流程:-

import subprocess
proc = subprocess.Popen('sudo -u username Python temp.py', stdout=subprocess.PIPE, shell=True)
(out, err) = proc.communicate()
print "program output:", out