0

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 ?

13
  • What's the value of public_path() ? You can try to provide the full path of the python binary, to find it, use, which python. What's the output of echo "python ".public_path()."/python/start_clientsim.py 2>&1" ? does this output runs without errors if pasted on the shell? Commented May 17, 2017 at 23:20
  • 2
    did you try to start the same script from the user running your webserver? It could be www-data, httpd, apache etc Commented May 17, 2017 at 23:28
  • @PedroLobito : python /Applications/MAMP/htdocs/code/benu/ssc-portal/public/python/start_clientsim.py 2>&1 Commented May 17, 2017 at 23:34
  • 1
    That means exec is available . It's propably about permissons, like @Marat suggested. Use chmod 0755 Commented May 18, 2017 at 0:22
  • 1
    @ihue to check if this is the case you need to find out the username. Then change to the desired user terminal (sudo su <username>) and run the desired command. If it works, then you can either chmod 755, or chown/chgrp service script to the <username>, or add username to the script group - there are many ways to give exec permissions Commented May 18, 2017 at 0:46

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Thank-you answering, but I am not a C expert, but I'll look into it though if I got nothing in the next couple days. :)
I mean what you basically do is make a PHP module in C that talks with the python DLL file or whatever much like the .exe file of the python interpreter would. Might need to take a look at the python source code to see how the .exe talks with the DLL though :). Or you could just invoke the execute commands in the PHP module but I dont recommend that the one I mentioned first would be much better
Obviously if we are talking linux then .SO file.
English please. jk. Your suggestion is very technical for me, and I completely respect that. But I just don't know where to start. I'll start with thank-you !
I mean ive only ever written a hello world PHP module. But these things aren't designed for people not to use them. They are designed well and I am sure you could pull something off this way. Might be overkill for what your doing and no problem :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.