douche5961
2015-04-17 05:02
浏览 147

为什么我不能使用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";
?>%  

图片转代码服务由CSDN问答提供 功能建议

我有一个从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 
  #按预期工作
   
 
 

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

供参考 ,PHP文件是:

 &lt;?
echo“YAY”; 
?&gt;%
   
 
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

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

    打赏 评论

相关推荐 更多相似问题