【问题标题】:Why can't I get output from PHP, using python Popen为什么我不能使用 python Popen 从 PHP 获取输出
【发布时间】:2015-04-17 05:02:14
【问题描述】:

我有一个从 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";
?>%  

【问题讨论】:

  • exec_path 在这两种情况下都是一样的,对吧?我想知道这是否可能是权限问题。
  • 但是我仍然可以从命令行执行文件,如 php /tmp/regraphd_testing_dir_A5wdrS/queries/tmp0AKqI6.php,但是是的,路径是一样的
  • PHP文件不应该以&lt;?php开头吗?
  • 另外,最后的% 是干什么用的?
  • 哦,那只是表示那里没有换行符的shell

标签: php python shell popen


【解决方案1】:

我解决了我的问题。之前我写了文件内容,忘记了flush(),所以exec调用发生时文件是空的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-04-07
    • 1970-01-01
    • 1970-01-01
    • 2019-06-04
    • 1970-01-01
    • 2015-09-21
    • 1970-01-01
    相关资源
    最近更新 更多