Reputation: 11
I have two Ubuntu PCs set up and connected as shown, they are able to ping each other.
PC1<========================>PC2
PC2 has apache2 running on it. I have set up a python script as shown below to be run when PC1 makes a request to server using PC2's IP.
The Python script works fine and generates a simple web page as indicated in it, but the moment I uncomment the line
subprocess.call("./test.sh", shell=True)
of code to run a bash script, two things happen:
When I run ./index.py
locally on PC2, it works and also executes the bash script file ./test.sh
as indicated in it and hence also executes the top
command mentioned in the ./test.sh
but...
When I request this through PC1, the apache2 crashes and gives the following in browser:
Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator at webmaster@localhost to inform them of the time this error occurred, and the actions you performed just before this error.
More information about this error may be available in the server log.
Apache/2.4.7 (Ubuntu) Server at localhost Port 80
The error log file has following detail:
[Wed Feb 10 21:40:51.866832 2016] [cgi:error] [pid 9747] [client
127.0.0.1:34506] malformed header from script 'index.py': Bad header:
THIS COMES FROM TEST
[Wed Feb 10 21:40:51.867920 2016] [cgi:error] [pid 9747] [client
127.0.0.1:34506] AH01215: TERM environment variable not set.
The Python script ./index.py
is as follows:
#!/usr/bin/python
import subprocess
subprocess.call("./test.sh", shell=True)
# Turn on debug mode.
import cgitb
cgitb.enable()
# Print necessary headers.
print("Content-Type: text/html\n")
print('<html>')
print('<head>')
print('<title>IoT Based Electric SubStation Monitoring</title>')
print('</head>')
print('<body>')
print('</br>')
print('<marquee><b><h1>IoT Based Electric SubStation Monitoring</h1>
</b></marquee>')
print(' <button type="button">Click here to STOP</button> ')
print('</body>')
print('</html>')
The bash script ./test.sh
is as follows:
#!/bin/bash
#echo THIS COMES FROM TEST
firefox
#echo STOPPPPPPPPPPPPPPPPPPPP
Upvotes: 1
Views: 341
Reputation: 601529
You are running the bash script before writing the content type header. Do it the other way around.
The bash script writes to the same stdout as your Python script, so the first thing the Apache server sees is the output of the bash script. That output isn't a valid HTTP header, hence the error message.
Upvotes: 2