简体   繁体   中英

Run Python script in PHP

My goal

is to run my python script using PHP. I have a start button on the front-end when I click on that button. It start a service.


Try #1

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

Still not working


Try #2

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

Still not working


Try #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

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.

在此处输入图片说明


Try #5

I tried with different python simple script

python /Applications/MAMP/htdocs/code/site/portal/public/python/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. This would be much more efficient and is what I would do.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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