gpt4 book ai didi

php - 从 PHP Web 应用程序中的 Python 命令读取输出

转载 作者:太空宇宙 更新时间:2023-11-03 18:03:28 24 4
gpt4 key购买 nike

我正在尝试从 php web 应用程序读取 python 输出。

我在 php 代码中使用 $out = shell_exec("./mytest") 来启动应用程序,并 sys.exit("returnvalue")在 python 应用程序中返回值。

问题是 $out 不包含我的返回值。相反,如果我尝试使用 $out = shell_exec("ls")$out 变量将包含 ls 命令的输出。

如果我从终端运行 ./mytest ,它就会工作,并且我可以在终端上看到输出。

最佳答案

sys.exit("returnvalue")

使用带有sys.exit的字符串来指示错误值。因此,这将在 stderr 中显示 returnvalue,而不是 stdoutshell_exec() 默认情况下仅捕获 stdout

您可能想在 Python 代码中使用它:

print("returnvalue")
sys.exit(0)

或者,您也可以在 PHP 代码中使用它将 stderr 重定向到 stdout

$out = shell_exec("./mytest 2>&1");

(事实上,两者都做可能是最好的,因为如果发生意外情况,让 stderr 消失可能会非常困惑)。

关于php - 从 PHP Web 应用程序中的 Python 命令读取输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27314766/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com