Jack Gibson
Jack Gibson

Reputation: 1

Issues executing python script from HTML button using PHP

I cannot get a button on my webpage to execute a python3 script that is also inside the same folder in the server as the rest of the html and php files.

exec();
system();
escapeshellcmd();
shell_exec()

all of these commands are not working for me right now. I have chmod +x my .py file and included #!/usr/bin/env python3 at the very beginning of my python file.

<?php
if( isset($_POST['runScript'])){

$command = escapeshellcmd('/nfs/nfs7/home/team51/cgi- 
pub/dataProcess.py');
$output = shell_exec($command);
echo $output;

}
?>

<form method="post">
<input type="submit" name="runScript" value="runScript">
</form>

In the end the python script should place a csv file in the same folder as the rest of the files. But I am getting nothing.

Some functions just print back "Array()"

Upvotes: 0

Views: 81

Answers (2)

SachinPatil4991
SachinPatil4991

Reputation: 774

You are running pythin script from apache user. Python path may not be accessible by apache user

Use full python installation path to execute script.

exec("/usr/bin/python /nfs/nfs7/home/team51/cgi- 
pub/dataProcess.py");

To find out exact error check apache error log. If error logs give permission denied , it means apache is not having permission to execute script. You can get access from sudoers file.

Upvotes: 1

Youssef mahmoed
Youssef mahmoed

Reputation: 373

Try Make a sh file to execute the python script and use PHP to execute this sh file

Upvotes: 0

Related Questions