我正在尝试从php webapp读取python输出。
我在php代码中使用$out = shell_exec("./mytest")
启动应用程序,并在python应用程序中使用sys.exit("returnvalue")
返回值。
问题是$out
不包含我的返回值。
相反,如果我尝试使用$out = shell_exec("ls")
,则$out
变量将包含ls
命令的输出。
如果我在终端上运行./mytest
,它将正常工作,并且可以在终端上看到输出。
最佳答案
sys.exit("returnvalue")
在
sys.exit
中使用字符串表示错误值。因此,这将在returnvalue
中显示stderr
,而不是stdout
。 shell_exec()
默认情况下仅捕获stdout
。您可能想在Python代码中使用它:
print("returnvalue")
sys.exit(0)
另外,您也可以在PHP代码中使用它来将
stderr
重定向到stdout
。$out = shell_exec("./mytest 2>&1");
(实际上,最好同时执行这两种操作,因为如果发生意外情况,使
stderr
消失可能会造成混乱。)关于php - 从PHP Webapp中的Python命令读取输出,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27314766/