简体   繁体   中英

php calling python script with argument but getting no output

Can someone please point the error as to why I am not getting any output in echo. The php script echo works fine with commands like ls or whoami. But, when I echo my output there is nothing displayed. My python code works fine. So, here is the html/php code. ignore the variable names since I was just trying to test the functionality and have created a simple script that calls a python function to add two numbers.

Test1 Database Query

<p><b>
Primary ref-ctr:</b>
<select size =2 name="formPrimctr">
    <option value ="">Select ...</option>
    <option value = "pctr1">1</option>
    <option value = "pctr2">2</option>
  </select>
</p>

<p><b>
  Secondary ref-ctr:</b>
  <select size =2 name="formSecctr">
      <option value ="">Select ...</option>
      <option value = "sctr1">5</option>
      <option value = "sctr2">10</option>
    </select>
</p>

  <div class = emailadd>
    <br/><b>
    E-mail: </b><input type="email" required /><br/>
    <br/>

   <input type="submit" name="formSubmit" value="Submit" class="btn-         Submit"><br>

  <?php
      if(isset($_POST['formSubmit'])){
        $varPrimctr= $_POST['formPrimctr'];
        $varSecctr = $_POST['formSecctr'];
        $errorMessage = "";
      }


    $output = exec("python test1.py .$varPrimctr .$varSecctr");
    echo $output;

    ?>

Here is the simple test python script:

#!/usrbin/env python

import sys
import os
import string
import time

sum =0
sum = int(sys.argv[1]) + int(sys.argv[2])
print ('sum:',sum)
lfp=open('testfile.csv','w')
lfp.write("Sum is %d\n\n" %sum)

Right now, I just want to call python script with two arguments and echo the output. In actual application my php will call python script with 4 arguments(one of which is going to be email" and others are user selected input from dropdown. The python script is actually a wrapper script which calls another script that does some processing and generates a csv file which should be emailed to the user. For testing purpose, I have written this simple python script to test how to pass arguments to python from php.

Deeply appreciate any guidance here.

Try adding 2>&1 to the end of your exec statement, which will give more detailed error messages, like so:

$output = exec("python test1.py .$varPrimctr .$varSecctr 2>&1");

In addition, try adding this line to the top of PHP file:

putenv("PYTHONIOENCODING=utf-8");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM