Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
cgipython操作系统_Python_Apache_Cgi - Fatal编程技术网

cgipython操作系统

cgipython操作系统,python,apache,cgi,Python,Apache,Cgi,我只是想知道是否有可能从Apache上的CGI脚本运行系统调用。以下是部分代码: print "<input type = 'submit' value = 'Launch Job!'>" print "</form>" print "</body>" print "</html>" system('command') 我正试图在承载网页的同一台计算机上运行此命令。问题是,当执行此函数时,命令无法成功运行。我尝试过在开始时添加sudo,尝试过os

我只是想知道是否有可能从Apache上的CGI脚本运行系统调用。以下是部分代码:

print "<input type = 'submit' value = 'Launch Job!'>"
print "</form>"
print "</body>"
print "</html>"
system('command')
我正试图在承载网页的同一台计算机上运行此命令。问题是,当执行此函数时,命令无法成功运行。我尝试过在开始时添加sudo,尝试过os.system'command'都没有用


那么,这可能吗

命令将以用户身份运行,权限非常有限。

尝试子流程:

如果服务器给出500错误,请尝试cgi:


谢谢Louis,有没有办法改变它?你可以设置可执行文件的setuid位,这意味着它将在文件所有者的权限下运行。警告:如果所有者是root,这可能会造成严重的安全风险。例如,您可以创建apache用户拥有的包装器脚本。您可以使服务器作为普通用户运行,通过DBU或套接字接收执行某些操作的请求。然后cgi脚本可以请求启动作业。这可能有助于解释您正在尝试执行的操作。也许您不需要使用外部实用程序?
#!/usr/bin/python
import subprocess
a = subprocess.check_output(["date"]) # this is your command
#print os_date
print "Content-type:text/html\r\n\r\n"
print '<html>'
print '<head>'
print '<title>Hello Word!</title>'
print '</head>'
print '<body>'
print '<h2>Hello Word! Today is: %s</h2>' % a
print '</body>'
print '</html>'
#!/usr/bin/python

try:
    import cgitb; cgitb.enable()
except:
    pass
import sys, cgi, os
sys.stderr = sys.stdout
from time import strftime
import traceback
from StringIO import StringIO
from traceback import print_exc


if __name__ == '__main__':
    print "Content-type: text/html"         
    print                                   
    form = cgi.FieldStorage()
    thecmd = "python yourscript.py" # this is your command
    if thecmd:
        print '<HR><BR><BR>'
        print '<B>Command : ', thecmd, '<BR><BR>'
        print 'Result : <BR><BR>'
        try:
            child_stdin, child_stdout = os.popen2(thecmd)
            child_stdin.close()
            result = child_stdout.read()
            child_stdout.close()
            print result.replace('\n', '<BR>')

        except Exception, e:                
            print errormess
            f = StringIO()
            print_exc(file=f)
            a = f.getvalue().splitlines()
            for line in a:
                print line