Page 1 of 1

launch python programm by pphp

Posted: Sat Dec 15, 2018 5:32 pm
by rapidograf
In the web page is button which run php script.
in php script wrote

$pr=exec("python /home/pi/pr.py");

whei i push putton , php stript launch pr.py

but if in pr.py
is strings about smbus , for example

importt smbus
bus=smbus.SMBus(1)

a=bus,read_pyte_data (0x68, 0x01)

this programm cant be lauch by php script , but good working by launch from console

when i detele all string about smbus and operation with registers , php script can lauch pr.py without any problem.

What is the trouble with launch python progremm by php scripts ?

and if you will advice directly work with python as cgi script , i cant it do , because i dont know as preset apache2 for it.

Re: launch python programm by pphp

Posted: Sat Dec 15, 2018 7:13 pm
by bzt
Hi,

First of all I suggest to redirect the output of your python script into a file so that you can see what's the error message.

Code: Select all

$pr=exec("python /home/pi/pr.py 2>&1 >/tmp/prpyoutput.txt");
Second, it's very likely you're having a file access issue. When you run your script from console, it's running as the logged in user. In contrast when you execute it from your php script, it's running as the webserver's user. I suppose your logged in user can read / write the smbus file (or some pseudo-device file) while the webserver's user can't. The solution is either add your webserver's user to the same groups, or change the file permissons on that file.

(Unfortunately I can't tell you what files are used by the python smbus library. Frankly I hate smbus, and I kill it everywhere I can. IMHO it's a seriously f*cked up, overcomplicated, unneccesary thing; only used by huge bloated session enviroments which I avoid anyway. Simple window manager environments (like blackbox, openbox, windowmaker, etc.) work happily without it.)

Cheers,
bzt

Re: launch python programm by pphp

Posted: Sat Dec 15, 2018 8:25 pm
by rapidograf
bzt wrote:
Sat Dec 15, 2018 7:13 pm
Hi,

First of all I suggest to redirect the output of your python script into a file so that you can see what's the error message.

Code: Select all

$pr=exec("python /home/pi/pr.py 2>&1 >/tmp/prpyoutput.txt");
Second, it's very likely you're having a file access issue. When you run your script from console, it's running as the logged in user. In contrast when you execute it from your php script, it's running as the webserver's user. I suppose your logged in user can read / write the smbus file (or some pseudo-device file) while the webserver's user can't. The solution is either add your webserver's user to the same groups, or change the file permissons on that file.

(Unfortunately I can't tell you what files are used by the python smbus library. Frankly I hate smbus, and I kill it everywhere I can. IMHO it's a seriously f*cked up, overcomplicated, unneccesary thing; only used by huge bloated session enviroments which I avoid anyway. Simple window manager environments (like blackbox, openbox, windowmaker, etc.) work happily without it.)

Cheers,
bzt
wow . thank yuy. i will teste your adbvice in catch errors.
Can you give some sample how working without smbus with i2c devices ?

file /tmp/prpyoutput.txt was created , size zero , no information , python script wit strings about smbus not working .

Re: launch python programm by pphp

Posted: Sun Dec 16, 2018 11:45 am
by bzt
rapidograf wrote:
Sat Dec 15, 2018 8:25 pm
Can you give some sample how working without smbus with i2c devices ?
Oh, you meant that smbus, not the session manager bus, my mistake, sorry. In that case, yes, you're using the correct py library. A few links:
https://elinux.org/Interfacing_with_I2C_Devices
https://www.instructables.com/id/Raspbe ... 2C-Python/
http://raspberry-projects.com/pi/progra ... nterface-2
and last, but not least
viewtopic.php?t=173781
file /tmp/prpyoutput.txt was created , size zero , no information , python script wit strings about smbus not working .
Those strings are what we need :-) Do they have some filenames in them? Chances are good that the file you lack permission for is /dev/i2c. To be sure, run this:

Code: Select all

strace python /home/pi/pr.py 2>&1 | grep open >/tmp/prpytrace.txt
This will list all linux system calls, and then grep will limit the output to opening files only. You'll see a lot of them (shared libraries and other technical files required by python for example), but somewhere around the end you'll see the one which causes the problem. When executed from php (the one which fails), it's very likely that the last line (or the one before) will be the one causing the trouble.

When you've found the filename, do "ls -l filename" to see which group it belongs to, then add your webserver's user to that group. (If the file is under /dev, then you can't simply modify the file permissions, because /dev is generated, and your modifications will be lost. You'll have to write an udev rule to make that permanent, which is not easy for a beginner, therefore I suggest to add it's group instead)

Cheers,
bzt

Re: launch python programm by pphp

Posted: Sun Dec 16, 2018 4:54 pm
by rapidograf
bzt wrote:
Sun Dec 16, 2018 11:45 am
rapidograf wrote:
Sat Dec 15, 2018 8:25 pm
Can you give some sample how working without smbus with i2c devices ?
Oh, you meant that smbus, not the session manager bus, my mistake, sorry. In that case, yes, you're using the correct py library. A few links:
https://elinux.org/Interfacing_with_I2C_Devices
https://www.instructables.com/id/Raspbe ... 2C-Python/
http://raspberry-projects.com/pi/progra ... nterface-2
and last, but not least
viewtopic.php?t=173781
file /tmp/prpyoutput.txt was created , size zero , no information , python script wit strings about smbus not working .
Those strings are what we need :-) Do they have some filenames in them? Chances are good that the file you lack permission for is /dev/i2c. To be sure, run this:

Code: Select all

strace python /home/pi/pr.py 2>&1 | grep open >/tmp/prpytrace.txt
This will list all linux system calls, and then grep will limit the output to opening files only. You'll see a lot of them (shared libraries and other technical files required by python for example), but somewhere around the end you'll see the one which causes the problem. When executed from php (the one which fails), it's very likely that the last line (or the one before) will be the one causing the trouble.

When you've found the filename, do "ls -l filename" to see which group it belongs to, then add your webserver's user to that group. (If the file is under /dev, then you can't simply modify the file permissions, because /dev is generated, and your modifications will be lost. You'll have to write an udev rule to make that permanent, which is not easy for a beginner, therefore I suggest to add it's group instead)

Cheers,
bzt
yes ! you are genious ! :) i found it pleae look on it :arrow:

open ("/dev/i2c-1", O_RDWR | O_LARGEFILE) = -1 EACCES (permission denied)

what i can do with this for give acess to i2c device ? or necessary give acess to apache2 ?

Re: launch python programm by pphp

Posted: Sun Dec 16, 2018 9:20 pm
by fbe
The error output from

Code: Select all

$pr=exec("python /home/pi/pr.py");
should be in /var/log/apache2/error.log
If you want to redirect all output (stdout and stderr) into another file, use

Code: Select all

$pr=exec("python /home/pi/pr.py >>/tmp/prpyoutput.txt 2>&1");
The apache webserver is running as user www-data.
You could start your python programm as www-data in a terminal window first to see error messages:

Code: Select all

sudo -u www-data python /home/pi/pr.py
To give www-data the permission to access i2c use the command:

Code: Select all

adduser www-data i2c
You may need to add www-data to the group gpio too.

Re: launch python programm by pphp

Posted: Mon Dec 17, 2018 5:02 am
by rapidograf
fbe wrote:
Sun Dec 16, 2018 9:20 pm
The error output from

Code: Select all

$pr=exec("python /home/pi/pr.py");
should be in /var/log/apache2/error.log
If you want to redirect all output (stdout and stderr) into another file, use

Code: Select all

$pr=exec("python /home/pi/pr.py >>/tmp/prpyoutput.txt 2>&1");
The apache webserver is running as user www-data.
You could start your python programm as www-data in a terminal window first to see error messages:

Code: Select all

sudo -u www-data python /home/pi/pr.py
To give www-data the permission to access i2c use the command:

Code: Select all

adduser www-data i2c
You may need to add www-data to the group gpio too.
Great thank you :!:

Can you give sample how add GPIO to group www-data ?

Re: launch python programm by pphp

Posted: Mon Dec 17, 2018 3:14 pm
by mfa298
rapidograf wrote:
Sat Dec 15, 2018 5:32 pm
What is the trouble with launch python progremm by php scripts ?

and if you will advice directly work with python as cgi script , i cant it do , because i dont know as preset apache2 for it.
I realise it's not the route you're asking for but do you actually need apache if you went the pure python route (i.e. no php) ?

It's possible to have Python application provide a web interface directly with no other parts needed (i.e. no apache, php, nginx etc). There's a guide for getting started with Python and Flask. I think the other similar option people use is Django (which is possibly a bit more advanced than Flask).

For a larger Python web application I believe you can have it proxied via apache either using the apache proxy feature or using WSGI. This is potentially the route that large Python based sites use (Google informs me that the likes of Youtube, Reddit and even Google are based on Python code - I have no idea on their actual setup and whether they use Apache, Nginx or something else).

It's well worth exploring all the options available that would let you reach your goal. People having issues with a mix of Apache + PHP + Python seem to occur on a regular basis and they generally seem to be harder to diagnose due to the number of moving parts. I wouldn't be surprised if many of those are permission based issues but that still leaves a lot of hunting (it could be permissions on the filesystem, apache, php, python or on some setups even things like sudo, selinux or apparmour).

If you're looking at something that can scale up (the main reason why you'd want apache in front of the web application) then using techniques like php's exec function are a really bad idea (executing external programs like that can have a significant hit on performance)

Re: launch python programm by pphp

Posted: Tue Dec 18, 2018 2:15 am
by bzt
I'm glad you have found it! :-)
rapidograf wrote:
Mon Dec 17, 2018 5:02 am
Can you give sample how add GPIO to group www-data ?
You have two options. Suggested one is using the usermod command (as root) in a terminal:

Code: Select all

usermod -a -G i2c www-data
If you're not root, prefix the command with "sudo".

The other option is to edit /etc/group file (also as root). This is a simple text file, just look for the line which starts with "i2c:" and add "www-data" at the end. To get the manual of this file, type "man group", but it's very-very simple: every line describes one group. Coloumns are separated by colon (':'), in order: name of the group, the character 'x' (meaning no group password), numerical value of the group, comma (',') separated usernames that belong to this group. That's all.

It worth mentioning that privileges are CACHED, so one must logout/login again to refresh the session. In case of apache that means to restart the webserver service. With SysV I always had problem restarting apache, so I suggest to use two commands to be sure (as root):

Code: Select all

/etc/init.d/apache2 stop
/etc/init.d/apache2 start
Or you could just simply reboot your Pi.

Cheers,
bzt

Re: launch python programm by pphp

Posted: Tue Dec 18, 2018 2:52 am
by bzt
fbe wrote:
Sun Dec 16, 2018 9:20 pm
You could start your python programm as www-data in a terminal window first to see error messages:

Code: Select all

sudo -u www-data python /home/pi/pr.py
This is a good advice! And sudo doesn't care if www-data otherwise not allowed to log in!
To give www-data the permission to access i2c use the command:

Code: Select all

adduser www-data i2c
You're right, maybe a wee simpler than "usermod -a -G".

Cheers,
bzt

Re: launch python programm by pphp

Posted: Tue Dec 18, 2018 5:07 am
by rapidograf
mfa298 wrote:
Mon Dec 17, 2018 3:14 pm
rapidograf wrote:
Sat Dec 15, 2018 5:32 pm
What is the trouble with launch python progremm by php scripts ?

and if you will advice directly work with python as cgi script , i cant it do , because i dont know as preset apache2 for it.
I realise it's not the route you're asking for but do you actually need apache if you went the pure python route (i.e. no php) ?

It's possible to have Python application provide a web interface directly with no other parts needed (i.e. no apache, php, nginx etc). There's a guide for getting started with Python and Flask. I think the other similar option people use is Django (which is possibly a bit more advanced than Flask).

For a larger Python web application I believe you can have it proxied via apache either using the apache proxy feature or using WSGI. This is potentially the route that large Python based sites use (Google informs me that the likes of Youtube, Reddit and even Google are based on Python code - I have no idea on their actual setup and whether they use Apache, Nginx or something else).

It's well worth exploring all the options available that would let you reach your goal. People having issues with a mix of Apache + PHP + Python seem to occur on a regular basis and they generally seem to be harder to diagnose due to the number of moving parts. I wouldn't be surprised if many of those are permission based issues but that still leaves a lot of hunting (it could be permissions on the filesystem, apache, php, python or on some setups even things like sudo, selinux or apparmour).

If you're looking at something that can scale up (the main reason why you'd want apache in front of the web application) then using techniques like php's exec function are a really bad idea (executing external programs like that can have a significant hit on performance)
good advice. but i/m not progremmer . to me very important that @all working from box@ without any setups, but i seen your link all look likes enought easy.

Re: launch python programm by pphp

Posted: Tue Dec 18, 2018 5:54 pm
by mfa298
rapidograf wrote:
Tue Dec 18, 2018 5:07 am
good advice. but i/m not progremmer . to me very important that @all working from box@ without any setups, but i seen your link all look likes enought easy.
I'd definitely suggest trying to do everything in a single language then. Trying to learn several languages (PHP, Python as well as HTML/CSS) is likely to lead to more confusion. If you can get everything in a single language it's usually easier to debug and easier to learn. It's also likely to be easier to get help with as everything should be more contained.

Re: launch python programm by pphp

Posted: Wed Dec 19, 2018 5:07 am
by rapidograf
mfa298 wrote:
Tue Dec 18, 2018 5:54 pm
rapidograf wrote:
Tue Dec 18, 2018 5:07 am
good advice. but i/m not progremmer . to me very important that @all working from box@ without any setups, but i seen your link all look likes enought easy.
I'd definitely suggest trying to do everything in a single language then. Trying to learn several languages (PHP, Python as well as HTML/CSS) is likely to lead to more confusion. If you can get everything in a single language it's usually easier to debug and easier to learn. It's also likely to be easier to get help with as everything should be more contained.
yes youare right ! impossible always use stone tools when around steel and plastic )))))))))