怎样在php中执行python script?
想通过php ssh到远程服务器,然后执行服务器上的一个python文件,获得结果,然后从php中打印出来。
我的代码如下:
$connection = ssh2_connect('remote-server',22);<br>
ssh2_auth_password($connection, 'root','pwd');<br>
<br>
$string = ssh2_exec($connection, 'python pythonfile.py; sleep 20');<br>
stream_set_blocking($string,true);<br>
echo stream_get_contents($string);
但是php中打印出来的是空值。
如果我把
$string = ssh2_exec($connection, 'python pythonfile.py; sleep 20')
替换为
$string = ssh2_exec($connection, 'ls -l')
则没有问题,程序正常输出。
远程服务器中的python文件运行一般需要15秒,所以我设置了sleep 20,但还是获取不到结果。
想问是不是代码哪里有错?要怎么改正呢?










