Python服务器脚本与C程序通信错误

发布于 2024-12-23 14:42:06 字数 1122 浏览 1 评论 0原文

我的应用程序的目标是通过以太网链路控制嵌入式目标上的一些 LED。我的嵌入式板支持 lighttpd Web 服务器。从这个网络服务器上,我可以运行 python 脚本来读取我板上的设备,没有问题。当我尝试写入这些设备时,问题就出现了。 lighttpd 服务器作为“www”组运行。我的主板的 root 用户没有密码。我强制 lighttpd 服务器以 root 身份运行的任何尝试都会导致 lighttpd 根本无法启动。因此,我制作了一个 C 程序,作为子进程调用,通过 sudo 从 python 脚本提升到根目录。

我控制 LED 的 C 程序:

int main(int argc, char* args[]){   
string python_message = "";
bool quit = false;

while (!quit)
{
    cin >> python_message;  
    if (python_message == "quit"){
    quit = true;
    }else if (python_message == "1"){
        ledn(1,"1");    
    }else if (python_message == "2"){
        ledn(1,"0");
    }else {
    cout << "Huh?" << endl;
    }   
}   
return 0;
}

cgi-bin 中的 python 脚本

import sys
import time

print "Blinking User LED Program"

import subprocess
proc = subprocess.Popen(["sudo","/usr/bin/slave"], stdin=subprocess.PIPE, stdout=subprocess.PIPE)

print "1"
proc.stdin.write("1\n")
time.sleep(.5)
print "0"
proc.stdin.write("0\n")
time.sleep(.5)

如果我注释 proc.stdin 和 proc.stdout 行,我的程序就会运行并给出所有打印语句输出。 当这些行出现时,我收到 500 服务器错误。

The objective of my application is to control some LEDs on my embedded target from the ethernet link. My embedded board supports lighttpd web server. From this web server, I can run python scripts that read to devices on my board no problem. The problem comes when I am trying to write to those devices. The lighttpd server is running as "www" group. My board's root user has no password. Any attempt i make to force the lighttpd server to run as root results in lighttpd not starting at all. So I made a C program to be called as a subprocess elevated to root via sudo from the python script.

my C program that controls the LEDs:

int main(int argc, char* args[]){   
string python_message = "";
bool quit = false;

while (!quit)
{
    cin >> python_message;  
    if (python_message == "quit"){
    quit = true;
    }else if (python_message == "1"){
        ledn(1,"1");    
    }else if (python_message == "2"){
        ledn(1,"0");
    }else {
    cout << "Huh?" << endl;
    }   
}   
return 0;
}

The python script that is in cgi-bin

import sys
import time

print "Blinking User LED Program"

import subprocess
proc = subprocess.Popen(["sudo","/usr/bin/slave"], stdin=subprocess.PIPE, stdout=subprocess.PIPE)

print "1"
proc.stdin.write("1\n")
time.sleep(.5)
print "0"
proc.stdin.write("0\n")
time.sleep(.5)

If i comment the proc.stdin and proc.stdout lines my program runs and gives me all the print statement outputs.
When those lines are there i get a 500 server error.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

香橙ぽ 2024-12-30 14:42:06

Ricardo Cárdenes 关于更改设备所有权或权限的建议是一个很好的建议,但如果您做不到这一点,只需将 lighttpd 调用的 Python 脚本设为“setuid”脚本,这意味着 lighttpd 会将其作为 www 调用,但是它将以 root 身份运行。

我通常不建议制作一个脚本 setuid (制作编译的 C 程序 setuid 危险性要小一些, 或许)。但就你而言,你似乎并不关心安全性(因为你提到尝试以root身份运行lighttpd),所以我会尝试一下。只是不要忘记您的 setuid 脚本可以做任何它想做的事情!

Ricardo Cárdenes's suggestion to change the ownership or permissions of the device is a good one, but if you can't do that, just make the Python script that lighttpd calls be a "setuid" script, meaning that lighttpd will invoke it as www but it will run as root.

I normally would not suggest making a script setuid (making a compiled C program setuid is a little less dangerous, maybe). But in your case you don't seem to be concerned about security (since you mentioned trying to run lighttpd as root), so I'd give it a shot. Just don't forget that your setuid script can then do anything it wants!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文