php - 从 PHP 执行 python 脚本以打开套接字

标签 php python permissions

我有一个 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('/usr/bin/python PATH_TO_FILE/chatserver.py 2>&1');

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

我已经尝试了很多我在搜索中发现的东西,但我似乎无法让文件执行。

最佳答案

看起来像是权限问题。 chatserver.py 目前需要 root 用户来运行它。

不要只是 chmod 777 chatserver.py。那很危险。

我喜欢的方式是为这些管理任务创建一个组:

groupadd chatserver

然后将自己添加到该组:

usermod -a -G chatserver willroberts

然后将 chatserver.py 更改为该组所有:

chown root:chatserver chatserver.py

当您的用户运行 php 时,它将具有正确的组成员身份来运行 py 文件。此方法为更多“聊天服务器”可执行文件提供了更大的灵 active 。如果您有兴趣,可以阅读更多内容:https://wiki.archlinux.org/index.php/Users_and_Groupshttp://www.tldp.org/LDP/intro-linux/html/sect_03_04.html

关于php - 从 PHP 执行 python 脚本以打开套接字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15836116/

相关文章:

php - INNER JOIN 中 SELECT IF 的 Mysql 结果

php - 如何将数组元素从序列中的第 0 个索引重新排列到结尾?

python - 安装 matplotlib 时出错 - python2.7 (RHEL 5.9)

python - 不使用 ** 计算指数的迭代函数

permissions - wkhtmltopdf用户权限

php - MailChimp 支持 : the final "welcome" email with the link to unsubscribe

php - set_include_path() 的作用是什么?

javascript - 使用 Selenium 关闭浏览器弹出窗口

c# - 使用 Entity Framework 管理实体访问和权限

安卓相机黑屏