code-8
code-8

Reputation: 58780

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.

enter image description here


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 ?

Upvotes: 0

Views: 252

Answers (1)

NibbleBits
NibbleBits

Reputation: 124

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.

Upvotes: 1

Related Questions