[英]Call Python From PHP And Get Return Code
我正在从 PHP 调用 python 脚本。
python 程序必须根据传递给它的参数返回一些值。
这是一个示例 python 程序,它会让你对我目前正在做的事情有一个基本的了解:
#!/usr/bin/python
import sys
#get the arguments passed
argList = sys.argv
#Not enough arguments. Exit with a value of 1.
if len(argList) < 3:
#Return with a value of 1.
sys.exit(1)
arg1 = argList[1]
arg2 = argList[2]
#Check arguments. Exit with the appropriate value.
if len(arg1) > 255:
#Exit with a value of 4.
sys.exit(4)
if len(arg2) < 2:
#Exit with a value of 8.
sys.exit(8)
#Do further coding using the arguments------
#If program works successfully, exit with a value of 0
从上面的代码可以看出,我的基本目标是
目前,我为此使用了“sys.exit(n)”。
我使用 sys.exit 是否正确,还是需要使用其他东西?
还有什么方法存在于 PHP 中,以便我可以从 python 访问返回代码?
抱歉问了这么长的问题,但希望它能帮助你理解我的困境
万分感谢
在 PHP 中,您可以执行命令并使用exec
获取返回码。
exec
的手册说第三个参数是一个变量,其中将存储返回代码,例如
exec('python blibble.py', $output, $ret_code);
$ret_code
将是 shell 返回码, $output
是打印到 std 的文本行数组。 输出。
这似乎是您描述的返回码的适当用法,即 0 表示成功,>0 是各种类型错误的代码。
这是 exit() 的正确用法。 另见: http : //docs.python.org/library/sys.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.