为什么我不能使用python Popen从PHP获得输出

时间:2015-04-17 05:02:15

标签: php python shell popen

我有一个程序从python调用PHP,使用popen,我不会从我的调用中获取任何stdout或stderr。

有问题的行是:

task = subprocess.Popen(
        ['/usr/bin/php', exec_path],
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
        env=env
    )
stdout_str, stderr_str = task.communicate()
task_result = task.returncode

当我用ls替换PHP路径时,它可以工作:

task = subprocess.Popen(
    ['/bin/ls', exec_path],
    stdout=subprocess.PIPE,
    stderr=subprocess.PIPE,
    env=env
)

stdout_str, stderr_str = task.communicate()
task_result = task.returncode
# Works as expected

当我从shell运行PHP命令时,它确实产生了输出。

作为参考,PHP文件是:

<?
echo "YAY";
?>%  

1 个答案:

答案 0 :(得分:0)

我解决了我的问题。之前我已经编写了文件内容,并且忘记了flush(),所以当执行exec调用时文件是空的。