gintonik
Posts: 52
Joined: Fri Apr 22, 2016 4:09 pm
Location: Germany - Rhineland Palatinate

start python over php

Sun Jul 19, 2020 4:08 pm

Hi,

i´m very frustated because i have an issue and no idea why it dosn´t works!

Explain:

I have an RPI with Apache Webserver (incl. PHP). There is a php-Site who starting an bash-script inside the /var/www/html/scripts-Folder.
The script .sh starts a python-Script located /home/pi/Desktop/myscript.py. Everything works absolute great and doing that what it have to do very good.

But if i try to expand more function by addition more scripts it dosn´t works!

This is the Example of my Python-Script who is already working from PHP:

start.py

Code: Select all

#!/usr/local/bin/python
from time import sleep
from datetime import datetime
from sh import gphoto2 as gp
import signal, os, subprocess

..........
This is an Example of my Bash, wo starts the Python-Script by calling from a PHP-Website:

start.sh

Code: Select all

#!/bin/bash
#DISPLAY=:0 chromium-browser --kiosk http://127.0.0.1/Animation_Startseite.html
sudo python3 /home/pi/Desktop/start.py
And this is the PHP-Website who open the sh-script:

start.php

Code: Select all

<!DOCTYPE html>
<html>
    <head>
        <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <meta http-equiv="refresh" content="1; url=working2.php">
        <script type='text/javascript' src='Animation_verarbeitung_resources/jquery-3.3.1.min.js'></script>       
    </head>
    <body style="background-color:black;">>
    <body>

        
        <div id="Animation_verarbeitungStage" class="wascript">
            <script type="text/javascript" charset="utf-8" src="Animation_verarbeitung_resources/WA_SCRIPT.js"></script>
        </div>
  <body>
        <center><img src="http://192.168.178.5:8000/stream.mjpg" width="800" height="600"></center>
              
  </body>
<?php
$script = "sudo /var/www/html/scripts/start.sh";
$run=`$script`;
?>

    </body>
</html>

with sudo visudo i written that:

Code: Select all

www-data ALL= NOPASSWD:/var/www/html/scripts/start.sh
Thats it: If you start the Website start.php, who are located at the apache-webserver inside the RPI, everything works fine.

BUT NOW:

I try to make a copy of them what i already posted above. Maybe everything named with a "2" at the end start2.py, start2.php for Example.
I did everything exactly like before but it doesnt works.

OK, the first sequence i programmed in January and today i like to expand some functions. I can´t say that i didnt do a little step more then today, but i dont know where is the problem!

And before some of you starts screaming "IT SECURITY" and "THATS A NOGO": The System ist Embedded and not reachable from Outside.

I feel very happy if someone can help me. I hope i can convey my problem, because my english is not the best.
No one said it´s easy...

User avatar
neilgl
Posts: 2183
Joined: Sun Jan 26, 2014 8:36 pm
Location: Near Aston Martin factory

Re: start python over php

Sun Jul 19, 2020 4:42 pm

Check the files by entering ls -lh to see if you have made them executable (chmod +x) and who owns them (root or pi)

gintonik
Posts: 52
Joined: Fri Apr 22, 2016 4:09 pm
Location: Germany - Rhineland Palatinate

Re: start python over php

Sun Jul 19, 2020 6:03 pm

Thank you for your comment, but that not fixing the problem. Every file is owned by "pi" and be executable by sudo chmod +x start.sh/start.py.


These Files are hosted in: /var/www/html/scripts

Code: Select all

-rwxrwxrwx 1 pi pi 133 Jan 25 18:44 test1.sh
-rwxrwxrwx 1 pi pi 133 Jan 25 18:44 test2.sh
-rwxrwxrwx 1 pi pi 159 Mar 11 16:11 test3.sh
-rwxrwxrwx 1 pi pi 142 Mar 11 11:47 test4.sh
-rwxrwxrwx 1 pi pi 132 Jan 14 17:04 test.sh
These Files are hosted in: /home/pi/Desktop

Code: Select all

drwxrwxrwx 2 pi pi 4.0K Mar 11 17:29 3inarow
-rwxrwxrwx 1 pi pi 3.6K Mar 11 11:32 cappic1.py
-rwxrwxrwx 1 pi pi 5.0K Mar 11 11:32 cappic2.py
-rwxrwxrwx 1 pi pi 3.3K Mar 11 17:34 cappic_3inarow1.py
-rwxrwxrwx 1 pi pi 6.8K Mar 11 15:57 cappic_3inarow2.py
drwxrwxrwx 2 pi pi  36K Mar 11 17:31 fotos
drwxrwxrwx 2 pi pi 4.0K Mar 10 23:25 issue
-rwxrwxrwx 1 pi pi 3.6K Mar 11 11:32 picam_stream.py

These Files are hosted in: /var/www/html/

Code: Select all

-rw-r--r-- 1 pi pi 2.5K Mar 11 11:47 1_start.html
-rw-r--r-- 1 pi pi  535 Mar 10 03:34 Animation_so_gehts.html
-rw-r--r-- 1 pi pi  658 Mar 10 03:39 anleitung.html
-rw-r--r-- 1 pi pi  649 Mar 10 06:27 ausgabe1.html
-rw-r--r-- 1 pi pi  645 Mar 10 06:27 ausgabe2.html
-rw-r--r-- 1 pi pi  645 Mar 10 06:56 ausgabe3.html
-rw-r--r-- 1 pi pi  645 Mar 10 20:29 ausgabe4.html
-rw-r--r-- 1 pi pi  645 Mar 11 09:13 ausgabe5.html
-rw-r--r-- 1 pi pi  645 Mar 11 17:31 ausgabe.html
-rw-r--r-- 1 pi pi  530 Mar 11 11:48 countdown_3inarow.html
-rw-r--r-- 1 pi pi  527 Mar 11 15:12 countdown_fast.html
-rw-r--r-- 1 pi pi  522 Mar 11 11:06 countdown.html
-rw-r--r-- 1 pi pi  639 Jan 26 16:47 gallerie.html
-rw-r--r-- 1 pi pi  635 Jan 25 16:17 index.html
-rw-r--r-- 1 pi pi  436 Jan 19 08:13 preview.html
-rw-r--r-- 1 pi pi  953 Mar 11 11:49 selection.html
-rw-r--r-- 1 pi pi  860 Mar 11 11:50 start.html
-rw-r--r-- 1 pi pi  645 Jan 19 08:13 working.html
pi@pi3:/var/www/html $ ls *.php -lh
-rw-r--r-- 1 pi pi 523 Jan 22 19:07 cappic.php
-rw-r--r-- 1 pi pi 626 Jan 22 19:12 countdown.php
-rwxrwxrwx 1 pi pi 721 Mar 11 15:43 working2_3inarow.php
-rw-r--r-- 1 pi pi 719 Mar 11 09:20 working2_fast.php
-rw-r--r-- 1 pi pi 721 Jan 25 18:57 working2.php
-rwxrwxrwx 1 pi pi 857 Mar 11 17:17 working_3inarow.php
-rw-r--r-- 1 pi pi 854 Mar 11 09:20 working_fast.php
-rw-r--r-- 1 pi pi 849 Mar 11 17:31 working.php

I don´t see a different between the files who working and the files doesnt. I think it´s just a little thing, nothing special. On every step when this project was growing i notice every step for recovering it some day, if i forget some thing. But this was a very little thing so my notebook doesnt have many informations about that, because it was an easy job at januar, whent the first scripts are written.
No one said it´s easy...

gintonik
Posts: 52
Joined: Fri Apr 22, 2016 4:09 pm
Location: Germany - Rhineland Palatinate

Re: start python over php

Sun Jul 19, 2020 6:08 pm

I´m sure that the Problem is starting the python. I modified my script.py for shutdown immediately. If it works, RPI shut down. So the Issue will just have one reason; why the php doesnt start the bash script?
No one said it´s easy...

pidd
Posts: 572
Joined: Fri May 29, 2020 8:29 pm
Location: Birkenhead, Wirral, UK
Contact: Website

Re: start python over php

Mon Jul 20, 2020 1:13 am

So where is the php trying to run the script?

I expect an exec(), shell_exec(), or system() etc

I've not come across $run other than as a variable?

I can't see start.php in your var/www/html directory.

EDIT: apologies for my ignorance on this one, see next post!
Last edited by pidd on Mon Jul 20, 2020 11:56 am, edited 1 time in total.

User avatar
neilgl
Posts: 2183
Joined: Sun Jan 26, 2014 8:36 pm
Location: Near Aston Martin factory

Re: start python over php

Mon Jul 20, 2020 9:28 am

Looks like the shell script is run using backticks rather than shell exec etc. In $run=`$script`;
Also is WA_SCRIPT.js Web Animator?

pidd
Posts: 572
Joined: Fri May 29, 2020 8:29 pm
Location: Birkenhead, Wirral, UK
Contact: Website

Re: start python over php

Mon Jul 20, 2020 11:55 am

neilgl wrote:
Mon Jul 20, 2020 9:28 am
Looks like the shell script is run using backticks rather than shell exec etc. In $run=`$script`;
Also is WA_SCRIPT.js Web Animator?
I didn't realise that worked in php, thanks for the enlightenment, I probably skipped over that chapter because backticks are often problematic on my keyboards, I remember some battle with backticks in the past and on the current keyboard I have to press it twice???

I'm a great fan of php, one of the beauties of php is the choice of ways to do things where different people find different ways are more intuitive, but as in this case it catches me out quite regularly when I see other's code.

I keep on meaning to do some speed tests between python and php.

gintonik
Posts: 52
Joined: Fri Apr 22, 2016 4:09 pm
Location: Germany - Rhineland Palatinate

Re: start python over php

Mon Jul 20, 2020 3:32 pm

neilgl wrote:
Mon Jul 20, 2020 9:28 am
Looks like the shell script is run using backticks rather than shell exec etc. In $run=`$script`;
Also is WA_SCRIPT.js Web Animator?

Yes, it´s a part from webanimator
No one said it´s easy...

gintonik
Posts: 52
Joined: Fri Apr 22, 2016 4:09 pm
Location: Germany - Rhineland Palatinate

Re: start python over php

Mon Jul 20, 2020 3:49 pm

OK, i tried to post you just the relevant parts from my project, i don´t like boring you but now i will try to descripe you the function:

It´s a Photobooth with an GUI (html5 Website displayed on a 10" Tablet):

On the startscreen you push "take a photo). Then coundown.html starts (showing you a liveview by a raspicam). After 10seconds next page "working.php" ist loading. There is the first call for execute a shell-script (for making pictures). After Script finished "working2.php" will be loaded and uses the next shell-sript (download the picture from camera, renamed it, create QR-code etc...). After all a review is called and the last picture shown on the screen, before it goes back to the start screen.

Now i´d like to expand the function with a "3 in a row" Mode. And this needs an other python script, because there are 3 Pictures taken and need to processed. And at this point a have the issue: The html is complete and the python scripts are still working, if it starts manual from terminal.

I dont understand why the way for the single mode is still running and the same way taken for the new function doesnt work.

I will try the shell_exec() in php and will report after getting results.
No one said it´s easy...

User avatar
neilgl
Posts: 2183
Joined: Sun Jan 26, 2014 8:36 pm
Location: Near Aston Martin factory

Re: start python over php

Mon Jul 20, 2020 4:38 pm

Sounds like a nice project.

gintonik
Posts: 52
Joined: Fri Apr 22, 2016 4:09 pm
Location: Germany - Rhineland Palatinate

Re: start python over php

Sat Jul 25, 2020 9:54 pm

So, after a few days and a lot of work i done it!

I did an real stupid thing, i wrote data-WWW instead of data-www at the sudoers! This simple thing is the different between "working" and "do nothing".

So i will repeat all thing you have to do, if you like to start a python from webbrowser:

Code: Select all

sudo apt-get update
sudo apt-get install apache2
sudo apt-get install php
create a bash-script inside "/var/www/html/scripts/" (this Example test.sh):

Code: Select all

#!/bin/bash
sudo python3 /home/pi/Desktop/yourpython.py
place your python to "/home/pi/Desktop/".


then put this code to your website.php for action:

Code: Select all

$script = "sudo /var/www/html/scripts/test.sh";
$run=`$script`;
then write

Code: Select all

sudo visudo

#includedir /etc/sudoers.d
www-data ALL= NOPASSWD:/usr/bin/python
www-data ALL= NOPASSWD:/var/www/html/scripts/test.sh
and last but not least you have to change the bash-script, that it´s executable:

Code: Select all

sudo chmod +x test.sh
sudo chmod 777 test.sh
If you do this you will be ready for executing an python-script by open a php on a webbrowser hosted on the RPI.



ATTENTION !!!!!!

This solution is workable but not the best solution by the case of it-security!

My project is running offline and nobody can get a gateway for the network. Take care about the things you like to do with it.
No one said it´s easy...

Return to “General programming discussion”