douche5961 2015-04-17 05:02
浏览 154
已采纳

为什么我不能使用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 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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作