dongpo5207 2015-05-01 11:46
浏览 31

使用php脚本执行Python代码

I have a python code and I want to execute it from my php. I have seen all the other questions. But my problem is that I am not getting proper directory path. My test python code is,

a=4
b=5
return a+b

I was making a small html like,

<!DOCTYPE html>
<html>
<body>

<?php
    $result = exec("python test.py");
    echo $result;
?>  

</body>
</html>

I was running it from wamp server. So I put both of them in www folder. Their urls are C:\wamp\www\test.py C:\wamp\www\1.php. I have installed python in my C: drive. So what is the proper path setup? And do I have to update my Apache server settings for python running?

  • 写回答

1条回答 默认 最新

  • dqu94359 2015-05-01 12:28
    关注

    in your python code you must have a print not return, and in php code try to use absolute path. Python

    a=4;
    b=5;
    print  a+b;
    

    PHP

    <?php
    $result = exec("C:\\Python27\\python.exe C:\\xampp\\htdocs\\test.py");
    print_r( $result);
    ?>
    

    or

    <?php
    $command = escapeshellcmd('C:/xampp/htdocs/test.py');
    $output = shell_exec($command);
    echo $output;
    ?>
    
    评论

报告相同问题?