dongmeng4742 2015-09-02 22:22
浏览 109
已采纳

使用PhP执行一些python脚本无法正常工作

Would like to ask , as I am designing a webpage whereby there will be a few buttons that when press will run different python scripts.

For now I have tried a basic example by using both jquery and php to pass the command to run just a single python script first that prints out hello world in the terminal console but it isn't working. I have tried searching but could not seem to find the solution. Any help? (One of them was about using fast-cgi? anyone can shed some light on this?)

Note: -I am coding on Mac OS X and using XAMPP but will be porting this code to raspberry pi with LAMPP installation. (Hence command line and advice if differ for the two OSes will be greatly appreciated)

My code:

Below are the codes where by i create a button when on press it runs a simple python script

Main.php

<!DOCTYPE html>
<html>
<head>
 <script type="text/javascript" src="js/general_v2.js"></script>
</head>

    <body>

        <button id="button_py1" type="button">Press to test Py</button>

 </body>
 </html>

below is the code from general_v2.js

$(document).ready(function(){
  $('#button_py1').click(function() {
  e.preventDefault();

  $.ajax({
   type: "POST",
   url: "z_py/py_run.php",


   success: function(output_string){
    console.log("js_success");

   },
   error:function(err){
    //handle error
    alert('Error submitting form' + err);
   }

});    
});
});

below is the code for py_run.php

<?php
        $command = escapeshellcmd("python z_py/testPyscript.py");
        $output = shell_exec($command);
        echo $output;
        echo  "php_works";

?>

the python file:

#! /usr/bin/env python

print "hello world python script"

Edit: -I have tried executing the script directly by keying in the url path name it does't work either(it does print to echo out a random test message so it is not permission error on the file i believe.) -Regarding the php code on executing python, im not familiar with using it(i found that on the forumn(can't remember where but i tried and doesnt work), found out about exec command as well and tried it as shown below and it doesn't work either :

This is my alternative Attempt on using exec (py_run.php)

<?php
        $command = "python z_py/testPyscript.py";
        exec($command);

?>

SOLVED! thanks to @Shawn Mehan

The solution would be that the python file is already in the same directory as the php file, hence removing the path name solves the issue.

updated code:

<?php
        $command = escapeshellcmd("/usr/bin/python testPyscript.py");
        $output = shell_exec($command);
        echo $output;
        echo  "php_works";

?>
  • 写回答

2条回答 默认 最新

  • dongwu8653 2015-09-02 22:38
    关注

    Well, your code works for me if I change your py_run.php slightly to include the path to python, e.g.:

    <?php
            $command = escapeshellcmd("/path/to/python/python z_admin_face/testPyscript.py");
            $output = shell_exec($command);
            echo $output;
            echo  "php_face2";
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 Hyper-v虚拟机相关问题,求解答。
  • ¥15 TSM320F2808PZA芯片 Bootloader
  • ¥45 谷歌浏览器出现开发者工具无法显示已创建的,但您可以调试已部署的代码。 状态代码 404, net::ERR HTTP RESPONSE CODE FAILURE
  • ¥15 如何解决蓝牙通话音频突发失真问题
  • ¥15 安装opengauss数据库报错
  • ¥15 【急】在线问答CNC雕刻机的电子电路与编程
  • ¥60 在mc68335芯片上移植ucos ii 的成功工程文件
  • ¥15 笔记本外接显示器正常,但是笔记本屏幕黑屏
  • ¥15 Python pandas
  • ¥15 蓝牙硬件,可以用哪几种方法控制手机点击和滑动