#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    42
    Rep Power
    3

    Run python scripts on PHP


    Hi, I'm new in PHP and 'm looking for the way to run my Python scripts via PHP. I'm now using WINSCP to edit my index page where I can store my python code here. I have found some simple code to execute like this:

    PHP Code:
    <html>
    <
    body>
    <
    form>
    Test: <input type="button" value="Submit" onclick="exec('TPHP.py');">
    </
    form>
    </
    body>
    </
    html
    But it doesn't work at all, if someone knows this problem, please help me find out. Thank you very much.
  2. #2
  3. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,316
    Rep Power
    7171
    onclick is a JavaScript handler; you cannot run PHP or Python code in it because it executes on the client side.

    In a PHP script, you can run a python script using the exec function:
    PHP Code:
    exec("python TPHP.py"); 
    Depending on your server configuration, invoking the python script using the python binary may or may not be required. You might also be required to specify the full path to the python binary, like:
    PHP Code:
    exec("/usr/bin/python TPHP.py"); 
    If in doubt, ask your web host or server administrator.

    As far as building a form to do it, you need two separate pages. For for the form, and one to process the form. The action attribute of the form tag specifies the PHP file that will be requested when the form is submitted.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    42
    Rep Power
    3
    Thank you for your reply, I'm quite new in PHP and JavaScript so I don't know pretty much to do about this stuff. I have followed your instruction and I put exec like you said but looks like it doesn't work anyway, but when i tried to use "onclick" method to load my python's file, it works. Here's the code:
    PHP Code:
    <body>
    <?php echo "TPHP test";
    exec("python TPHP.py");
    ?>
    <form>
    <input type="button" value="ViewCode" onclick="location="'python TPHP.py'">
    </form>
    </body>
    So my question is if onclick "location" can find my script by just typing the name of my python script, so the location for exec() should be the same right? Please help
  6. #4
  7. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,316
    Rep Power
    7171
    Your form or button or link needs to take the user to a different PHP page. On the second PHP page, you need to run the exec call.

    You cannot call the Python file from the form unless your web server is configured to serve Python scripts and your form takes the user to the URL of that Python script directly. However, in that case, you would not be calling Python from PHP.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    42
    Rep Power
    3
    Hi, sorry to bother you again, I'm really curious about this so i tried to run the python script that can just print "Hello Python" on the screen, and it works, here's the code:

    PHP Code:
    <?php 
    $output 
    null;
    exec('python HelloPython.py'$output$return);
    print_r($output);
    print_r($return)
    ?>
    and the result is Array ( [0] => Hello!Python!Coming! ) 0

    But when I change my python script to "TPHP.py" (This script is about to insert text to database just one line) it said:
    Array ( ) 1 , it returns value 1 and the array is empty, moreover the data doesn't get in the database. Do you know why? Please help

IMN logo majestic logo threadwatch logo seochat tools logo