【问题标题】:Can you execute a Python script from a function in a codeigniter controller您可以从 codeigniter 控制器中的函数执行 Python 脚本吗
【发布时间】:2021-04-10 12:16:20
【问题描述】:

我有一个要从我的 codeigniter 控制器执行的 python 脚本。 我发现了一个类似的问题,但不太能看到输出 (Executing a Python file from Codeigniter)

有没有一种方法可以让我从我的 CI 应用程序运行 python 脚本并将输出放入变量或类似的东西中?

【问题讨论】:

    标签: python php codeigniter scripting


    【解决方案1】:

    您可以从 php 执行 python 脚本,例如使用 passthru 函数。

    <?php
    ob_start();
    passthru('/full/path/to/python /full/path/to/test.py arg1 arg2');
    $output = ob_get_clean(); 
    
    echo $output;
    ?>
    

    或者配合“系统”功能。

    <?php
    system('full/path/to/python /full/path/to/test.py', $return_value);
    echo $return_value;
    ?>
    

    但是如果你想“看到”某些东西,你的 python 脚本应该返回一些东西。

    import sys
     #calculate stuff
    sys.stdout.write('Bugs: 5 |Other: 10\n')
    sys.stdout.flush()
    sys.exit(0)
    

    【讨论】:

    • 感谢@Jakob 的回复。 “return something”是否足够好?
    • 如果你在 php 中使用“system”,在 python 中使用“print”,“system”函数会在你的 php 脚本中回显/打印它。
    猜你喜欢
    • 1970-01-01
    • 2011-04-09
    • 2014-06-21
    • 2015-06-29
    • 1970-01-01
    • 1970-01-01
    • 2012-01-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多