当前位置: 动力学知识库 > 问答 > 编程问答 >

linux - python os.sytem kill process from cgi

问题描述:

I am trying to kill a process from a webpage.

import os

os.system(“killall Laser”)

print("Content-Type: text/HTML;charset=utf-8")

print("")

print("Laser has been restarted.")

The file permission is set to 777 and I have set the setuid and setgid. The script works when I run it from the command line but not in the browser.

This is running on ubuntu and the webserver is lighthttp.

The process is owned by root.

I have also tried using:

import subprocess as sub, cgitb

cgitb.enable(display=0, logdir="/tmp")

p = sub.Popen(['killall;', 'Laser'],shell=True,stdout=sub.PIPE,stderr=sub.PIPE)

output, errors = p.communicate()

print("Content-Type: text/HTML;charset=utf-8")

print("")

print(output)

and:

import subprocess as sub, cgitb

cgitb.enable(display=0, logdir="/tmp")

cmd1 = "su root"

cmd2 = "killall Laser"

p = sub.Popen("{}; {}".format(cmd1, cmd2),shell=True,stdout=sub.PIPE,stderr=sub.PIPE, close_fds=True)

output, errors = p.communicate()

print("Content-Type: text/HTML;charset=utf-8")

print("")

print(output)

Any ideas?

Matt

网友答案:

You should assign .py files to be ran as CGI/fastcgi-scripts through ligghtpd in it's config:

$HTTP["url"] =~ "^/cgi-bin/" {
        cgi.assign = ( ".py" => "/usr/bin/python" )
}

And don't forget about shebang (#!/usr/bin/env python) on the first line of python script. Read here for more details: http://ubuntuforums.org/showthread.php?t=1166401

分享给朋友:
您可能感兴趣的文章:
随机阅读: