简体   繁体   English

在PHP中运行Python脚本

[英]Run Python script in PHP

My goal 我的目标

is to run my python script using PHP. 是使用PHP运行我的python脚本。 I have a start button on the front-end when I click on that button. 单击该按钮时,前端有一个开始按钮。 It start a service. 它启动服务。


Try #1 尝试#1

$command = escapeshellcmd("python ".public_path().'/python/start_clientsim.py 2>&1');
$result = exec($command);

Still not working 还是行不通


Try #2 尝试#2

$command = escapeshellcmd("python ".public_path().'/python/start_clientsim.py 2>&1');
$result = shell_exec($command);

Still not working 还是行不通


Try #3 尝试#3

chmod a+x start_clientsim.py

-rwxr-xr-x  1 bheng  staff  1154 May 17 19:42 start_clientsim.py

Re-tried still same result 重试还是一样的结果


Try #4 尝试#4

If I execute the command directly 如果我直接执行命令

python /Applications/MAMP/htdocs/code/site/portal/public/python‌/start_clientsim.py 2>&1

I got it to run perfectly fine. 我知道它运行得perfectly

在此处输入图片说明


Try #5 尝试#5

I tried with different python simple script 我尝试了不同的python简单脚本

python /Applications/MAMP/htdocs/code/site/portal/public/python/print.py

print.py print.py

# This program prints Hello, world!

print('Hello, world!')

I got 我有

result: "Hello, world!" 结果:“你好,世界!”


Question

How would one go about and debug this further ? 人们将如何进行进一步调试?

One way of doing it is to write a PHP module in C that basically acts as a gateway to the python interpreter and then just feed everything through that. 一种实现方法是用C编写一个PHP模块,该模块基本上充当python解释器的网关,然后通过它提供所有内容。 This would be much more efficient and is what I would do. 这将更加有效,这就是我要做的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM