将 subprocess.Popen 命令的输出获取到网页中? (也许是阿帕奇的问题)

发布于 12-28 00:12 字数 1576 浏览 4 评论 0原文

这是我的程序 >

print "Content-type:text/html\r\n\r\n"
print "File starting to execute"
print "<br>"
proc = subprocess.Popen(["sudo", "python", "test3.py"], stdout=subprocess.PIPE)
output = proc.stdout.read()
print "output is %s" %output

print "<br>"
print "File Executed Awesomely"

因此,当我从命令行运行它时,它工作得很好,如下所示 -

[root@localhost html]# python test2.py
Content-type:text/html


File starting to execute
<br>

output is .
Sent 1 packets.

<br>
File Executed Awesomely
[root@localhost html]# 

这是完美的“。发送了 1 个数据包。”就是我想要的。但是当我从网页运行它时,网页只是有

File starting to execute
output is
File Executed Awesomely

,所以我最初认为这是因为我在抓取输出时做了一些错误的事情,但我用wireshark(我的另一个程序,它调用发送数据包)监听端口,它看起来像通过网页调用没有显示任何数据包,但当我在命令行上调用它时会显示数据包(以相同的方式)。看看我的 apache error_log->

[Wed Jan 18 18:15:11 2012] [notice] suEXEC mechanism enabled (wrapper: /usr/sbin/suexec)
[Wed Jan 18 18:15:11 2012] [notice] Digest: generating secret for digest authentication ...
[Wed Jan 18 18:15:11 2012] [notice] Digest: done
[Wed Jan 18 18:15:11 2012] [warn] ./mod_dnssd.c: No services found to register
[Wed Jan 18 18:15:11 2012] [notice] Apache/2.2.17 (Unix) DAV/2 configured -- resuming normal operations

关于如何修复它以便我的 apache cgi-bin 脚本以与命令行相同的方式运行的任何建议?

编辑:在几次调用后查看日志,它会重复执行此操作

[Wed Jan 18 18:22:37 2012] [error] [client 10.117.153.89] :
[Wed Jan 18 18:22:37 2012] [error] [client 10.117.153.89] sorry, you must have a tty to run sudo 

Here is my program
import subprocess

print "Content-type:text/html\r\n\r\n"
print "File starting to execute"
print "<br>"
proc = subprocess.Popen(["sudo", "python", "test3.py"], stdout=subprocess.PIPE)
output = proc.stdout.read()
print "output is %s" %output

print "<br>"
print "File Executed Awesomely"

So when I run it from the command line it works great, like follows->

[root@localhost html]# python test2.py
Content-type:text/html


File starting to execute
<br>

output is .
Sent 1 packets.

<br>
File Executed Awesomely
[root@localhost html]# 

that is perfect the ". Sent 1 packets." is what I want. But when I run it from the webpage, the webpage just has

File starting to execute
output is
File Executed Awesomely

so I originally thought this was because I was doing something wrong grabbing the output but I listened on the port with wireshark (my other program it calls send a packet) and it looks like no packets shows up via the webpage call, but it does when I call it on the command line (the same way). Looking at my apache error_log->

[Wed Jan 18 18:15:11 2012] [notice] suEXEC mechanism enabled (wrapper: /usr/sbin/suexec)
[Wed Jan 18 18:15:11 2012] [notice] Digest: generating secret for digest authentication ...
[Wed Jan 18 18:15:11 2012] [notice] Digest: done
[Wed Jan 18 18:15:11 2012] [warn] ./mod_dnssd.c: No services found to register
[Wed Jan 18 18:15:11 2012] [notice] Apache/2.2.17 (Unix) DAV/2 configured -- resuming normal operations

any suggestions on how I can fix it so my apache cgi-bin script runs the same way as the command line?

EDIT: looking at the log after a few calls it does this repeatedly

[Wed Jan 18 18:22:37 2012] [error] [client 10.117.153.89] :
[Wed Jan 18 18:22:37 2012] [error] [client 10.117.153.89] sorry, you must have a tty to run sudo 

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

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

发布评论

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

评论(1

很酷又爱笑2025-01-04 00:12:44

消息抱歉,您必须有一个 tty 才能运行 sudo 是关键。首先,让你的 apache 运行 sudo 至少可以说是危险的,但如果你真的想这样做......有一种方法,编辑 /etc/sudoers (visudo< /code>)并找到 Defaults requiretty 部分(man sudoers)。

注意:永远不要让 apache 使用 sudo 运行任何东西,准确指定它需要做什么,仅此而已!

顺便说一句:如果您启用了 SELinux 或其他 LSM 模块,它可能仍然无法工作。

The message sorry, you must have a tty to run sudo is the key. First of all, letting your apache run sudo is dangerous to say the least, but if you really really want to do it... there's a way, edit /etc/sudoers (visudo) and locate the Defaults requiretty part (man sudoers).

NB: Never let apache run anything using sudo, specify exactly what it needs to do and nothing more!

BTW: It may still not work if you have SELinux enabled or other LSM module.

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