Python not executing in browser
<p>I have a cgi script which imports functions from a different python file. I'm using these files to setup a Hadoop cluster. So it's mostly got commands.getstatusoutput codes. The cgi file just calls these functions while passing the parameters. </p>

<p>But it doesn't execute the functions. This works fine when i run it through terminal. I would appreciate any help on this.</p>

<p>cgi file:</p>

<pre><code>#!/usr/bin/python

import cgi
import cgitb
import os
import commands
import base2
cgitb.enable()

print "content-type:text/html"
print ""

dn_iplist=[]

base2.ipscanner();
base2.freemem();
base2.procno();
base2.freehd();

dn_iplist = sorted(base2.spacedict, key=lambda x: float(base2.spacedict[x][:-1]), reverse=True)[:2]

masterdict = base2.memdict.copy()

for key in dn_iplist:
    if key in masterdict:   
        del masterdict[key]

#print masterdict

#####################Resource Manager IP
rmip = max(masterdict, key=lambda key: int(masterdict[key]))
del masterdict [rmip]
#####################Namenode IP
nnip = max(masterdict, key=lambda key: int(masterdict[key]))
del masterdict[nnip]
#####################Client IP
cip = max(masterdict, key=lambda key: int(masterdict[key]))

print """
&lt;html&gt;
&lt;head&gt;
    &lt;title&gt;Hadoop Cluster Services - Create and Manage your Hadoop clusters&lt;/title&gt;
    &lt;link rel="stylesheet" type="text/css" href="/css/style1.css"&gt;
&lt;/head&gt;

&lt;body&gt;
        &lt;br&gt;&lt;b&gt;
        &lt;div class="drop"&gt;
        &lt;ul class="drop_menu"&gt;
            &lt;li&gt;&lt;a href="home.html"&gt;HOME&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href="create.cgi"&gt;CLUSTER&lt;/a&gt;
                &lt;ul&gt;
                    &lt;li&gt;&lt;a href="/loader.html"&gt;Create Cluster&lt;/a&gt;&lt;/li&gt;
                    &lt;li&gt;&lt;a href="#"&gt;Manage Cluster&lt;/a&gt;&lt;/li&gt; 
                &lt;/ul&gt;
            &lt;/li&gt;
            &lt;li&gt;&lt;a href="#"&gt;SERVICES&lt;/a&gt;
                &lt;ul&gt;
                    &lt;li&gt;&lt;a href="#"&gt;Shell&lt;/a&gt;&lt;/li&gt;
                    &lt;li&gt;&lt;a href="#"&gt;Create Cluster&lt;/a&gt;&lt;/li&gt; 
                    &lt;li&gt;&lt;a href="#"&gt;System Admin&lt;/a&gt;&lt;/li&gt;   
                &lt;/ul&gt;
            &lt;/li&gt;
        &lt;/ul&gt;   
        &lt;/div&gt;

    &lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;
    &lt;div id="autoips"&gt;
        Resource Manager IP: """ 
print rmip
print "&lt;br&gt;&lt;br&gt;Namenode IP: "
nport = "9001"
print nnip + ":" + nport 
print "&lt;br&gt;&lt;br&gt;Client IP: "
print cip
print "&lt;br&gt;&lt;br&gt;Datanodes/NodeManagers: "
for k in dn_iplist:
    print k + ","
print """&lt;br&gt;&lt;br&gt;&lt;input type="submit" value="Accept"&gt; """

print "&lt;br&gt;&lt;br&gt;&lt;br&gt; BUILDING CLUSTER"

print"""        

    &lt;/div&gt;

&lt;/body&gt;

&lt;/html&gt;
"""

base2.make_rm(rmip,nnip,nport)
print "&lt;br&gt;"
base2.make_nn(nnip,nport)
print "&lt;br&gt;"
base2.make_client(nnip,nport,rmip,cip)
print "&lt;br&gt;"
for dnip in dn_iplist:
    print dnip  
    base2.make_dnnm(nnip,nport,rmip,dnip)
    print "&lt;br&gt;"
</code></pre>

<p>a part of the py file:    </p>

<pre><code>#resourcemanager creation

def make_rm(rmip,nip,nport):
    fp = open("temp1.txt",mode="w")
    fp.write('&lt;?xml version="1.0"?&gt;\n&lt;!-- Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at\n\n http://www.apache.org/licenses/LICENSE-2.0\n\n Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.See the License for the specific language governing permissions and limitations under the License. See accompanying LICENSE file.--&gt;\n\n&lt;configuration&gt;\n\n&lt;!-- Site specific YARN configuration properties --&gt;\n\n&lt;property&gt;\n&lt;name&gt;yarn.resourcemanager.resource-tracker.address&lt;/name&gt;\n&lt;value&gt;'+rmip+':8025&lt;/value&gt;\n&lt;/property&gt;\n\n&lt;property&gt;\n&lt;name&gt;yarn.resourcemanager.scheduler.address&lt;/name&gt;\n&lt;value&gt;'+rmip+':8030&lt;/value&gt;\n&lt;/property&gt;\n\n&lt;/configuration&gt;')
    fp.close()
    commands.getstatusoutput("sshpass -p redhat scp temp1.txt "+rmip+":/hadoop2/etc/hadoop/yarn-site.xml")

    fp = open("temp2.txt",mode="w")
    fp.write('&lt;?xml version="1.0" encoding="UTF-8"?&gt;&lt;?xml-stylesheet type="text/xsl" href="configuration.xsl"?&gt;\n&lt;!--  Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at\n\n http://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.See the License for the specific language governing permissions and limitations under the License. See accompanying LICENSE file.--&gt;\n\n&lt;!-- Put site-specific property overrides in this file. --&gt;\n\n &lt;configuration&gt;\n\n&lt;property&gt;\n &lt;name&gt;fs.default.name&lt;/name&gt;\n&lt;value&gt;'+nip+':' +nport+ '&lt;/value&gt;\n&lt;/property&gt;\n&lt;/configuration&gt;\n')
    fp.close()
    commands.getstatusoutput("sshpass -p redhat scp temp2.txt "+rmip+":/hadoop2/etc/hadoop/core-site.xml")
    # print("Resourcemanager setup.\n")

    commands.getstatusoutput("sshpass -p redhat ssh -o StrictHostKeyChecking=no root@"+rmip+" yarn-daemon.sh start resourcemanager")

    a = commands.getstatusoutput("sshpass -p redhat ssh -o StrictHostKeyChecking=no root@"+rmip+" /usr/java/jdk1.7.0_79/bin/jps | grep 'ResourceManager' | awk '{print $2}'")

    if a[1] == "ResourceManager":
        print("ResourceManager setup complete!")
</code></pre>

<p>This is the result i get. It shows Setup complete. But nothing's been done. But it executes perfectly via terminal.</p>

<p><a href="https://i.stack.imgur.com/oPMq3.png" rel="nofollow"><img src="https://i.stack.imgur.com/oPMq3.png" alt="enter image description here"></a></p>

<p>There should be a similar Datanode setup complete! beside the 192.168.100.107 IP at the bottom.
This code isn't copying the temp files to the other VM. </p>

<p>EDIT: Could it be something related to the commands.getstatusoutput command??
Why is it only affecting scp</p>

<p>Finally fixed it. I had to add a root@ipaddress to the scp commands</p>

<p>from</p>

<pre><code>commands.getstatusoutput("sshpass -p redhat scp temp1.txt "+rmip+":/hadoop2/etc/hadoop/core-site.xml")
</code></pre>

<p>to</p>

<pre><code>commands.getstatusoutput("sshpass -p redhat scp temp1.txt root@"+rmip+":/hadoop2/etc/hadoop/core-site.xml")
</code></pre>

<p>This change has to be made to all the ssh/scp commands. 
Without this, httpd tries to ssh using the "apache" user which doesn't have root permissions. This is the reason for the code to fail in the browser.</p>

python apache shell cgi httpd.conf