简体   繁体   English

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

[英]How Can I print Python output at runtime in PHP

I want to display the output of the python script at runtime on the web browser through php. 我想在运行时通过php在网络浏览器上显示python脚本的输出。 Can someone help upon this..?? 有人可以帮忙吗?

I'm calling a simple python script through php here is the code: run.py: 我通过php调用了一个简单的python脚本,代码如下: 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脚本:

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

Can someone help upon this..?? 有人可以帮忙吗?

use exec instead 使用exec代替

$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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM