小编典典

从php执行python

python

假设您有这样的课程:

class MyClass:
    def __init__(self, var1):
        self.var = var1
    ....

在python中,此类仅在分配值时才起作用:

x = MyClass("Hi")

所以基本上,我的问题是我是否可以从php发送变量以执行python类,并返回其输出(它是字符串)并继续执行我的php代码?

有什么建议?

在php中:

$var = "something";
$result = exec("python fileName.py .$var")

在python中:

import sys

sys.argv[0] # this is the file name
sys.argv[1] # this is the variable passed from php

阅读 362

收藏
2021-01-20

共1个答案

小编典典

首先,创建一个包含要执行的python脚本的文件,包括(或加载)类和 x = MyClass("Hi")

现在,使用以下行获取结果:

$result = exec('python yourscript.py');
2021-01-20