无法使用shell_exec php浏览器中的pexpect模块运行python脚本

I have two linux servers, on one server i have test.py script which has pexpect module in it, test.py script ssh on another linux box and perform commands. Now, when i run this from cli it works fine but when i run it via php page from my browser it just doesn't work. Please help!!

#!/usr/bin/env python3

import sys
import pexpect
import os
import imp
import time


#uname = sys.argv[1]
#passwd = sys.argv[2]
try:
        child = pexpect.spawnu('ssh username@reporter')
        child.expect('\?')
        child.sendline('yes')
        child.expect('password:')
        child.sendline('passs@123')
        child.expect('\$')
        child.sendline('sudo su - tftp')
        child.expect('\$')
        child.sendline('uname -a')
        child.expect('\$')
        sys.stdout.write(child.before)
        f = open('j.txt','a')
        f.write(child.before)
except Exception as e:
        print(e)

that's test.py

<!DOCTYPE html>
<html>
<body>





<?php
chdir('/home/WHOIS');
$cmd = "python3 test.py";
while (@ ob_end_flush()); // end all output buffers if any

$proc = popen($cmd, 'r');
echo '<pre>';
while (!feof($proc))
{
    echo fread($proc, 4096);
   @ flush();
   echo '<br>';
}

?>

</body>
</html>

This is the php code. I have checked for the permissions and they are fine.

Permissions of the file.

-rw-rw-r-- 1 www-data www-data  294 Sep  5 12:31 test.php
-rwxrwxr-x 1 www-data www-data  514 Sep  5 13:30 test.py