[英]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.