1

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!

2
  • 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? Commented Oct 18, 2014 at 13:44
  • thanks, the problem was really calling from php. Commented Oct 18, 2014 at 15:08

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your feedback Yerke. How do you suggest i call my script ? I am new at this stuff :s
I solved when i changed <form name="input" action="localhost/insert.PHP" method="post"> to <form name="input" action="localhost/cgi-bin/script.py" method="post">. I seems that your guess was right, i will never try again to call from a php file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.