dqqs64238 2016-05-03 13:15
浏览 230
已采纳

如何在PHP中运行时打印Python输出

I want to display the output of the python script at runtime on the web browser through php. Can someone help upon this..??

I'm calling a simple python script through php here is the code: run.py:

  import os
    import subprocess
    import sys
    import time
    print "<br> Hi from Python"
    sys.stdout.flush()
    for i in range(0,5):
        time.sleep(1)
        print "<br> Hi from Python"
        sys.stdout.flush()

this is the php script:

<?php
$handle = popen("python run.py ", 'r');
while(!feof($handle)) {
$buffer = fgets($handle);
echo "$buffer<br/>
";
ob_flush();
}
pclose($handle);
?>

Can someone help upon this..??

  • 写回答

1条回答 默认 最新

  • douhan8581 2016-05-03 13:24
    关注

    use exec instead

    $res = NULL;
    
    exec("python full/path/to/script.py 2>&1" ,$res); //2>&1 @comments.KEK
    
    if(isset($res)){
        foreach($res as $rowRes){
            echo $rowRes;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥15 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services