简体   繁体   中英

PHP unable to call python script

I am currently working on a web page where I want to run a python script on Button click event. My python script is working properly in terminal but I am not able to call the same script from the PHP page.

Below is the code for my PHP page.

<html>
<head>
<meta charset="UTF-8" />
</head>


<? php
print_r($_POST);

if (isset($_POST['ON']))
{
exec("sudo python /home/pi/Desktop/IOT/script5.py");
}
if (isset($_POST['OFF']))
{
exec("sudo python /home/pi/Desktop/IOT/script5a.py");
}
if (isset($_POST['1']))
{
exec("sudo python /home/pi/Desktop/IOT/script1.py");
}
?>
<form method="POST">
<button name="ON" value="Submit">turn On</button>&nbsp;
<button name="OFF">turn Off</button><br>&nbsp;
<button name="1">First</button><br>

</form>
</html>

you need to pass your password to run any command with sudo, you can use the argument:

-S          The -S (stdin) option causes sudo to read the password from
            the standard input instead of the terminal device.

You can use it like (replace with your actually password):

exec("echo <password> | sudo -S python /home/pi/Desktop/IOT/script1.py");

Find more here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM