I'm trying to run python file from PHP
What I'm looking for is :
1- run python file from index.php
2- read the output of python file and show it in index.php
Example :
# test.py
import sys
user = sys.argv[1]
password = sys.argv[2]
def read():
if user == "user@dmoain.tld" and password == "123456":
return "ok, registerd"
else: return "I can\'t"
try: read()
except: pass
And in index.php should be some thing this :
<?php
$read = exec("python test.py user@dmoain.tld 123456");
if($read == "ok, registerd")
{
echo "succesful registerd";
}
else ($read == "I can\'t")
{
echo "failed";
}
?>
I'm not sure what should I do in index.php how can I do it?
First, your last else
is wrong on the php script,
instead of:
else ($read == "I can\'t"){
use:
else if($read == "I can\'t"){
Your python script was not working either. I didn't debug it, simply wrote a new one that works.
test.py
import sys
user = sys.argv[1]
password = sys.argv[2]
if user == "user@domain.tld" and password == "123456":
print "ok, registered"
else:
print "NOT registered"
index.php
<?php
/*
Error reporting helps you understand what's wrong with your code, remove in production.
*/
error_reporting(E_ALL);
ini_set('display_errors', 1);
$read = exec("python test.py user@domain.tld 123456");
if($read == "ok, registered")
{
echo "ok, registered";
}
else if($read == "NOT registered")
{
echo "failed";
}
?>