How to execute external commands with Python using Apache2 and CGI

854 views Asked by At

I have an Apache web server. I have an html file and a Python script located at var/www/myfolder. I am submitting a form through the html file which is handled by my Python script (when the submit button is clicked). But once handling the form, my Python script executes an external command. (see the following code)

webpage.html

<!DOCTYPE html>
<html>
    <head>
        <title>Webform</title>
    </head>
    <body>
        <h3>Webform</h3>
        <form action="myscript.py" method="POST">
            <p>Name: <input type="text" name="name"></p>
            <input type="submit" value="Submit">
        </form>
    </body>
</html>

myscript.py

#!/usr/bin/python -W

import cgi, cgitb 
import sys
import os

# Get data from fields
form = cgi.FieldStorage() 
name = form.getvalue('name')

print "Content-type:text/html\r\n\r\n"
print '<html>'
print '<head>'
print '<title>Hello Word - First CGI Program</title>'
print '</head>'
print '<body>'
print '<h2>Hello Word! This is my first CGI program</h2></br></br>'
print '<p>Name: %s</p>' % (name)
print '</body>'
print '</html>'

os.system("./other_script.py") # gives me an error with permission

On my server I am user007 but I know that when I click submit on the html file, the Python script is executed as apache2 user. The problem is I don't know the password for this user (can't us sudo). Are the two ideas possible:

1) change from apache2 to user007 when trying to execute other_script.py from myscript.py

2) I know that you can change users using suEXEC but I have no idea how to use it.

Any suggestions?

I should let you know that locally both the python scripts are executing fine.

Edit 1:

I get this message before the error occurs: WARNING: HOME is not set, using root: /

0

There are 0 answers