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条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?