From 9634040911e1a8bd550c6fcad4ceee192280e7e8 Mon Sep 17 00:00:00 2001 From: Raeen Date: Sat, 23 Jan 2021 15:36:23 +0330 Subject: [PATCH] add new function --- Python/test6.py | 3 +++ README.md | 19 ++++++++++++++++++- core/Python.php | 18 ++++++++++++++++++ php/test6.php | 18 ++++++++++++++++++ 4 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 Python/test6.py create mode 100644 php/test6.php diff --git a/Python/test6.py b/Python/test6.py new file mode 100644 index 0000000..d4cb089 --- /dev/null +++ b/Python/test6.py @@ -0,0 +1,3 @@ +import subprocess +import include.library.phpy as phpy +subprocess.call('ping '+phpy.get_data(1)) \ No newline at end of file diff --git a/README.md b/README.md index a07fc80..16c59de 100644 --- a/README.md +++ b/README.md @@ -113,8 +113,25 @@ $output = $python->gen("../Python/test2.py", $data1, $data2); $output = $python->gen(path,datas...) ``` -In $output you have python result also you can gen_line() function for return last line of result +In $output you have python result also you can ```gen_line()``` function for return last line of result +Also you can use ```gen_live_show()``` for print live output + +Example: + +```php +$site = "google.com"; +$python->gen_live_show( + "../Python/test6.py", //file path + 1, // time sleep between each output + 1024, // bytes from the file pointer referenced by stream + [$site], // your data + function ($res) { //controller + return "
$res
"; + } +); + +``` If result is json , you should use this ```php diff --git a/core/Python.php b/core/Python.php index 7476bcc..dbcff49 100644 --- a/core/Python.php +++ b/core/Python.php @@ -18,6 +18,24 @@ public function gen() $cmd_result = shell_exec($command); return $cmd_result; } + public function gen_live_show($path, $time, $read, array $args, object $fun) + { + $resultargs = null; + foreach ($args as $send) { + $resultargs .= escapeshellarg(json_encode($send)) . " "; + } + $command = self::python_path . " " . $path . " " . $resultargs . " 2>&1"; + $pid = popen($command, "r"); + while (!feof($pid)) { + $res = fread($pid, $read); + $return = call_user_func($fun, $res); + echo $return; + flush(); + ob_flush(); + sleep($time); + } + pclose($pid); + } public function gen_line() { $whattogen = func_get_args(); diff --git a/php/test6.php b/php/test6.php new file mode 100644 index 0000000..3aa643b --- /dev/null +++ b/php/test6.php @@ -0,0 +1,18 @@ +python; + +$site = "google.com"; +$python->gen_live_show( + "../Python/test6.py", + 1, + 1024, + [$site], + function ($res) { + return "
$res
"; + } +);