php脚本:

// 调用Debug.py文件中的test1方法
$cmd = "python  D:\python\Debug.py test1 p1 p2 2>&1";
$data = exec($cmd, $array, $return);
var_dump($data, $return);

Debug.py文件:

#!/usr/bin/env python
# _*_coding:utf-8 _*_
#@Time:2019/7/29
#@Autho:Luohc

import sys

class Debug(object):
    
    def __inti__(self):
        pass
    
    def test1(self, param1, param2): 
        return 'Action is test1, Params is ' + param1 + ',' + param2
    
    def test2(self, param1, param2):
        return 'Action is test2, Params is ' + param1 + ',' + param2

if __name__ == '__main__':
    # 获取php传的参数
    option = sys.argv[1]
    param1 = sys.argv[2]
    param2 = sys.argv[3]
    # 调用Debug类的option方法
    debug = Debug()
    res = getattr(debug, option, 'None')(param1, param2)
    # 返回执行结果给php
    print(res)

执行结果:

Action is test1, Params is p1,p2
0

以上为php脚本调用python类方法的代码示例。

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐