在Apache服务器上运行具有Sudo权限的Python CGI脚本

时间:2014-02-01 03:43:52

标签: python linux apache ubuntu cgi

我是Apache 2.2.22 http服务器上使用Ubuntu 12.04的python CGI新手。我正在编写一个脚本,将创建其他python脚本。这些创建的脚本必须具有" chmod + x"权限执行。创建脚本的脚本必须提供这些权限,我无法弄清楚如何让它来执行此操作。

在创建文件的代码中,我很好奇:

 os.system('sudo chmod +x %s.py' %(fname))

这是尝试给出创建的文件" chmod + x"权限。 os.system命令不提供创建的脚本" chmod + x"特权可能是因为它不是由具有sudo权限的服务器运行的。如何让服务器为脚本创建脚本sudo权限?

完整的代码如下。它是用户帐户创建系统的一部分:

#!/usr/bin/python
import cgi, os
import cgitb
cgitb.enable()

# Retrieve form fields
form   = cgi.FieldStorage()                     # Get POST data
fname  = form.getfirst("fname")                 # Pull fname field data
passw  = form.getfirst("passw")                 # Pull lname field data

# Begin HTML generation
print "Content-Type: text/html; charset=UTF-8"  # Print headers
print ""

try:
    with open('Users/%s.py' %(fname)):
            print '''
            <!DOCTYPE html>
            <html>
            <head>
            <link media="screen" type="text/css" rel="stylesheet" href="/style.css"></linK>
            <meta charset="UTF-8">
            <META http-equiv="refresh" content="3;URL=/cgi-bin/createAccount/createAccount.html">
            <meta name="keywords" content="chat, chat.ngrok.com">
            <title>Please Wait</title>
            <body bgcolor="black">
            <div id="navbar">
            <ul>
            <li><a href="/">Home</a></li>
            <li><a href="#">About</a></li>
            <li><a href="/">Create Account</a></li>
            <li><a href="/">Login</a></li>
            <li><a href="/">LinkHere</a></li>
            </ul>
            </div>
            <div class="Container">
            <div class="Header"></div>
            <div id="fire">
            <br>
            <h1>Sorry, this username is already taken!</h1>
            </div>
            </body>
            </html>
            '''
except IOError:
    createU = open('Users/%s.py' %(fname),'w')
    createU.write('''#!/usr/bin/python
    print "Content-type: text/html; charset=UTF-8" 
    print ""
    print "<!DOCTYPE html>"
    print "<html>"
    print "<head>"
    print "<link media='screen' type='text/css' rel='stylesheet' src='/style.css'></link>"
    print "</title>Test</title>"
    print "</head>"
    print "<body bgcolor='black'>"
    print "<div id='navbar'>"
    print "<ul>"
    print "<a href='/'>Home</a></li>"
    print "<a href='/'>About</a></li>"
    print "<a href='/'>Create Account</a></li>"
    print "<a href='/'>Login</a></li>"
    print "<a href='/'>LinkHere</a></li>"
    print "</ul>"
    print "</div>"
    print "<div class='Container'>"
    print "<div class='Header'></div>"
    print "</body>"
    print "</html>"''')
    createU.close()
    os.system('sudo chmod +x %s.py' %(fname))

    print '''
    <!DOCTYPE html>
    <html>
    <head>
    <link media="screen" type="text/css" rel="stylesheet" href="/style.css"></linK>
    <meta charset="UTF-8">
    <meta name="keywords" content="chat, chat.ngrok.com">
    <title>Please Wait</title>
    <body bgcolor="black">
    <div id="navbar">
    <ul>
    <li><a href="/">Home</a></li>
    <li><a href="#">About</a></li>
    <li><a href="/">Create Account</a></li>
    <li><a href="/">Login</a></li>
    <li><a href="/">LinkHere</a></li>
    </ul>
    </div>
    <div class="Container">
    <div class="Header"></div>
    <div id="fire">
    <br>
    <h1>Loading... Please Wait!</h1>
    </div>
    <form action="./test.py" name="FNAME" method="post">
    '''
    print '<input type="hidden" name="passw" value="%s" />' %(passw)
    print '''
    </form>
    <SCRIPT TYPE="text/JavaScript">document.forms["FNAME"].submit();</SCRIPT>
    </form>
    </body>
    </html>
    '''

1 个答案:

答案 0 :(得分:0)

您正在Users /目录中创建该文件,但之后尝试在该目录之外进行chmod。

你的chmod命令应该是os.system('chmod +x Users/%s.py' %(fname))