douche5961
2015-04-17 05:02 阅读 123

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

I have a program that calls PHP from python, with popen, and I don't get any stdout or stderr from my calls.

The problematic lines are:

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

When I replace the PHP path with ls, it works:

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

And when I run the PHP command from the shell, it does produce output.

For reference, the PHP file is:

<?
echo "YAY";
?>%  
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    dongliuzhuan1219 dongliuzhuan1219 2015-04-17 05:11

    I fixed my problem. Earlier I had written the file contents, and forgotten to flush() it, so the file was empty when the exec call happened.

    点赞 评论 复制链接分享

相关推荐