0

I want to call the python file from php using exec() but the output always is blank, and i do not know what is the problem, can anybody help me?

This is php code

<?php 
exec("D:\python27\python.exe C:\xampp\htdocs\hi.py");
?>

This is python code

print "Content-Type: text/HTML"
print

print"""
<html>
<body>
<h2>Hello World!</h2>
</body>
</html>
"""

I can run the python file along and the browser will display hello world!, however when i using exec() in php, it would not work.

钟子康
  • 11
  • 3

1 Answers1

0

as you said you are running it like apache->php->shell_exec(SUDO..)

So the apache user has to be in sudoers file, better you don't give sudo to apache instead give apache (www-data) user right to run your python program

put first line in your python script: #!/usr/bin/env python so the script knows which program to open it with..

then

change group:

chgrp www-data /path/to/python-script.py

make it executabel

chmod +x /path/to/python-script.py

try it

shell_exec("/path/to/python-script.py");

I hope it works ;)

TIPP: Apache and PHP are for delivering Documents and Strings, if you want some kind of control and an API start with nodejs and https://www.npmjs.com/package/rpi-gpio package. This way you will have one place for your solid automation environment

Muhammad Usman
  • 1,090
  • 11
  • 22
  • #!/usr/bin/env python this is the path to the python file? if my python file is in #!D:\python27\ then i put #!D:\python27 python in first line? – 钟子康 Apr 17 '18 at 04:28