Home > Backend Development > PHP Tutorial > 怎么在php中执行python script

怎么在php中执行python script

WBOY
Release: 2016-06-13 12:44:24
Original
959 people have browsed it

怎样在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);
Copy after login


但是php中打印出来的是空值。
如果我把
$string = ssh2_exec($connection, 'python pythonfile.py; sleep 20')
替换为
$string = ssh2_exec($connection, 'ls -l')
则没有问题,程序正常输出。

远程服务器中的python文件运行一般需要15秒,所以我设置了sleep 20,但还是获取不到结果。
想问是不是代码哪里有错?要怎么改正呢?

PHP Python SSH2
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template