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 有偿求码,CNN+LSTM实现单通道脑电信号EEG的睡眠分期评估
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路