背景:
开发使用的是php语言,而自己使用的是python语言,有时候需要借助开发以实现的功能,可以使用python执行php脚本,其中使用到subprocess模块
subprocess模块参考:https://blog.csdn.net/chengxuyuanyonghu/article/details/79317772
commands.getstatusoutput(command) 返回一个元组(命令执行状态码, 命令执行结果)
实现:
import getstatusoutput
commond="php /home/q/php/bx2a_core/cli.php test/send_gift 2000003334 2000003333 1000 "
cmdresult=subprocess.getstatusoutput(commond)[0] #得到脚本执行结果
cmdstring=subprocess.getstatusoutput(commond)[1] #获取脚本执行返回值
print ("cmdstring{0}".format(cmdstring))
if cmdresult==0: #脚本执行成功
pattern = r'order_no:(.*?)\n'
print ("pattern{0}".format(pattern))
res = re.match(pattern, cmdstring) #正则匹配到订单号
if res:
orderno=res[1].strip(' ')
print("匹配到orderno:",orderno)
print("执行脚本结果{0}{1}".format(cmdresult,cmdstring))
执行脚本返回的打印值如下
#脚本执行返回值
order_no:20200802130241T0083G17311
Core2a\Lib\Res Object
(
[errno:protected] => 0
[errcode:protected] => SUCC
[errmsg:protected] => 操作成功
[data:protected] => 11757
[logErrMsg:protected] =>
[isWrtLog:protected] => 1
)
Array
(
[order_no] => 20200802130241T0083G17311
[uid] => 2000003334
[pay_amount] => 1000
[pay_time] => 2020-08-02 13:02:41
[type] => 1
[act_type] => 1
[status] => 1
)
Core2a\Lib\Res Object
(
[errno:protected] => 0
[errcode:protected] => SUCC
[errmsg:protected] => 操作成功
[data:protected] => Array
(
[self_amount] => 140
[team_amount] => Array
(
[2000003332] => Array
(
[level] => 7
[amount] => 80
)
)
[team_supply_amount] => Array
(
)
)
[logErrMsg:protected] =>
[isWrtLog:protected] => 1
)