dougao2830 2018-11-06 14:39
浏览 68

PHP / Python / Bash模拟器

I am having to accomplish a custom terminal using PHP/JS/Python on a LAMP stack server to be able to execute scripts. I am able to: generate the command to be executed, run the script, and return the output. There are scripts that must be run that have a continuous (endless loop) that are to be killed using kill PID. However, I need to be able to call the output from the Python script that is running in the background. Example:

/*** PHP ***/
  <?php
    // AJAX call return
    $data = (object) array();
    $data->command = $_POST['command'];
    exec('python3 ' . $data->command . ' > /dev/null &', $data->output, $data->err);
    die(json_encore($data));
  ?>

/*** HTML ***/
  <HTML>
    <head>
      function submitForm(){
        //... function submits and returns json_object using AJAX
        var data = JSON.parse(__return__);
        var pre = document.createElement("PRE");
        for(var index = 0; index < data.output.length; index++){
          pre.innerHTML += data.output[index] + "<br>";
        }
        document.getElementById("output").append(pre);
      }
    </head>
    <body>
      <div id='output'></div>
      <input type='TEXT' id='command' onchange='submitForm();'/>
    </body>
  </HTML>

/*** PYTHON ***/
  #!/usr/bin/env python3
  import time;
  n = 0;
  while n < 5:
    print("THIS IS THE OUTPUT I NEED TO RETURN TO JSON");
    time.sleep(1);

Issue: When I send the command as a single command, it returns the output; when I send the command to start a script, it returns null; how can I call the output as a JSON object to get the python script output that is run from the python script above.

  • 写回答

1条回答 默认 最新

  • doucitao2944 2018-11-06 15:39
    关注

    To get full input, output and error detail we need to use proc_open function in php. Php function reference can be consulted here

    <?php
    
        $data = (object) array();
        $data->command = $_POST['command'];
    
        $descriptorspec = array(
            0 => array("pipe", "r"),  // stdin is a pipe that the child will read from
            1 => array("pipe", "w"),  // stdout is a pipe that the child will write to
            2 => array("pipe", "r")   // stderr is a pipe to read from
        );
    
        $process = proc_open('python3 ' . $data->command, $descriptorspec, $pipes);
        //It is always a good idea to put full path e.g /usr/bin/python3
        stream_set_blocking($pipes[2], 0); //setting non-blocking on stderr
        fclose($pipes[0]); //closing input
    
        $data->output = stream_get_contents($pipes[1]); //reading output
        fclose($pipes[1]); //closing output
    
        $data->err = stream_get_contents($pipes[2]); //reading stderr
        fclose($pipes[2]); //closing stderr
    
        die(json_encode($data));
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?