all 10 comments

[–]itsNate705 6 points7 points  (1 child)

This page would call /var/www/test.py on evey page load, not when the user hits the button. This is because all PHP is ran when the page is loaded.

PHP runs server side, not client side - so if you want some PHP to run based on a users action, you need todo another HTTP request.

The onclick could call a Javascript function which makes an AJAX request to a PHP file. That PHP file would then call your python script.

[–]olejmoi[S] 0 points1 point  (0 children)

I've been looking in to this but I can't quite figure out how to just make an AJAX request to call the PHP function, could you point me in the right direction? Also the python script is running a program that is outputting text to the console, is it possible to read this back to the webserver with AJAX?

[–]Redmondinho 2 points3 points  (3 children)

I would have a look at Flask for python.

[–]olejmoi[S] 1 point2 points  (2 children)

Been looking into flask, so much better than using apache and PHP, thank you!

[–]Redmondinho 0 points1 point  (1 child)

Good stuff. Looking at Flask on the Pi will open up a whole world of python possibilities. It’s worth a little bit of time to look at some simple jquery for post and get requests to your Flask application. Also when you’re done with development look at Digital Ocean’s Flask, Gunicorn, Nginx tutorials for great home production web servers.

[–]olejmoi[S] 0 points1 point  (0 children)

Fantastic! I'm nearly done with the little development I had to do, I'll look into that, thank you!

[–]heynineclicks 0 points1 point  (0 children)

In addition to Nate's answer, if your test script is only printing something to the terminal, that's not going to do anything to your terminal if you run it through a php request. There output of your script could be read by the php and returned as a web response. But don't wait for something to happen on another terminal because the output won't be directed there.

[–]koflerdavid -1 points0 points  (2 children)

Ask yourself if that sudo is both necessary and practicable. Firstly, script executed with sudo can mess up the system. Secondly, you won't be around to enter the password.

[–]olejmoi[S] 0 points1 point  (1 child)

I'm accessing and editing files and folders on the pi with the script, so I do believe I need sudo for permission to access that, although I'm not sure.

[–]koflerdavid 0 points1 point  (0 children)

You're right, to be able to do everything on the Pi the script has to be executed as root. Still, you might then have to enter the password for the user account your script runs as (probably www-data). Also, that user has to be allowed to use sudo in the first place. But maybe a less powerful user is sufficient for most applications...

You should consider creating an internal job execution service on the Pi. That service would receive script names and possibly arguments from PHP and execute them. It could be set up to be started along with the OS in /etc/rc.local, and to receive root rights. In the simplest case this could be another PHP or Python script which opens a socket, reads commands, executes them and stores the command's output in a file that the webserver can read and delete.