Search code examples
phppythonshellpopen

Why can't I get output from PHP, using python Popen


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";
?>%  

Solution

  • 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.