从 PHP 执行 Python 脚本来打开套接字

从 PHP 执行 Python 脚本来打开套接字

我有一个 python 脚本 chatserver.py,其中包括:-

#!/usr/bin/python
from twisted.internet.protocol import Protocol, Factory
from twisted.internet import reactor
<SNIP>
reactor.listenTCP(3800, factory)
print "Server Started"
reactor.run()

这将打开端口 3800 上的一个套接字,如果我从 SSH 启动,它就可以工作,但我想检查开放端口,如果它已关闭,则使用 PHP 重新打开,但我似乎无法执行 python 脚本。

以下是我目前通过 PHP 调用它的方式

function serverCheck() {
$host = "MYHOST";
$port = 3800;
$connection = @fsockopen($host, $port);

if ( !is_resource($connection) ) { // port not open
exec('/var/www/vhosts/httpdocs/chat/chatserver.py',$output);
var_dump($output);

if($connection)
fclose($connection);
}
}

我尝试了很多在搜索中找到的方法,但似乎无法执行该文件。

一位好心人在 SO 上帮助我,但无法找到答案,所以将我重定向到这里。

我的文件的 chmod 正确,并且函数“serverCheck” 100%被命中,但是 python 脚本似乎从未被执行!

相关内容