i am trying to execute a python script in my XAMPP server but i get a blank page each time i call the script. When i try to execute it in the adress localhost/cgi-bin/script.py it works fine but when i try to execute it in htdocs it comes in blank. In my htdocs directory i have 2 files:
html file:
<!DOCTYPE html>
<html>
<body>
<form name="input" action="http://localhost/insert.PHP" method="post">
User: <input type="text" name="user" value="Obama"><br>
Password: <input type="password" name="pass">
<input type="submit" value="Submit">
</form>
</body>
</html>
insert.php file which brings .py file to execute:
<?php
$Nome = $_POST['user'];
$Password = $_POST['pass'];
echo ini_get("disable_functions");
$python = exec('C:\xampp\cgi-bin\script.py');
echo $python;
?>
finally .py file (script.py) in cgi-bin directory:
#!C:\Python27\python.exe
import cgitb
cgitb.enable()
print "Content-Type: text/html"
print
print """
<TITLE>CGI script</TITLE>
<H1> just fine !</H1>
"""
print " <p>I am a script in python! </p>"
So i type User and password, it redirects to insert.php and i get a blank page. Thanks!
-
Read the documentation of PHP exec. php.net/manual/en/function.exec.php It does not return the full output of the command executed. And why do you run the python script through PHP in the first place? Why not execute it directly?deets– deets2014年10月18日 13:44:55 +00:00Commented Oct 18, 2014 at 13:44
-
thanks, the problem was really calling from php.João Pedro– João Pedro2014年10月18日 15:08:24 +00:00Commented Oct 18, 2014 at 15:08
1 Answer 1
I think the problem is that you have safe mode enabled which prevents execution outside of safe mode directory. See http://php.net/manual/en/function.exec.php description.
You can also try using system() function instead.
I also think that it is not a great idea to call python script from php. :)
EDIT: If you are on development environment, I would suggest you try to following:
1) copy script.py to the directory where your .php file is.
2) change .php exec line to system('python script.py');
3) if that works, try to call .py in cgi-bin directory. if cgi-bin is one level up from your htdocs directory, you can try system('python ..\cgi-bin\script.py');
I can't try it on my machine, since I run Mac.
4) I looked at safe mode description, it seems it was removed in PHP 5.4.0. So if your version of PHP is higher than that, it should not apply to you.
5) I would suggest also trying forward slashes in path: system('python ../cgi-bin/script.py');
Good luck. Let me know if it helps.