'" End of script output before headers" error in Apache
I'm trying to use python cgi module with apache, but I'm getting "500 Internal Server Error ". I've configurated the server following those instructions. I tried running script with sudo python3 my_script.py and there is no any error.
getAllSubjects.py file:
#!/usr/bin/python3
print("Content-Type: text/html\n")
import cgi
from bot.puescBot import Session
fieldData = cgi.FieldStorage()
data = []
data.append(fieldData.getvalue('i1'))
data.append(fieldData.getvalue('i2'))
data.append(fieldData.getvalue('i3'))
data.append(fieldData.getvalue('i4'))
s = Session()
s.logIn()
puesc = s.collectSubjects(str(data[0]), str(data[1]), str(data[2]), str(data[3]))
if puesc['status'] == 0:
p = puesc['message']
content = f'<p>{p}</p>'
else:
p = puesc['data']
content = f'<p>{p}</p>'
print(f"""
<html>
<head>
<meta charset="UTF-8">
<title>PUESC BOT</title>
</head>
<body>
{content}
</body>
</html>
""")
index.html file:
<html>
<head>
<meta charset="UTF-8">
<title>PUESC BOT</title>
</head>
<body>
<form action="../cgi-bin/getAllSubjects.py" method="get" id="form">
<p>NIP:
<input type="text" name="i1"/>
</p>
<p>EORI:
<input type="text" name="i2"/>
</p>
<p>Nazwa skrócona:
<input type="text" name="i3"/>
</p>
<p>Nazwa pełna:
<input type="text" name="i4"/>
</p>
<input type="submit" value="Opłać i wyszukaj"/>
</form>
</body>
</html>
Full error from apache2/error.log:
[Tue Mar 22 19:39:30.837790 2022] [cgid:error] [pid 1649:tid 3069833024] (2)No such file or directory: AH01241: exec of '/var/www/cgi-bin/getAllSubjects.py' failed
[Tue Mar 22 19:39:30.842430 2022] [cgid:error] [pid 826:tid 2852025344] [client 192.168.1.13:53964] End of script output before headers: getAllSubjects.py, referer: http://192.168.1.30/
Solution 1:[1]
I had faced the same error and I found there can be few possible reasons.
- Wrong interpreter: First of all, you should check if the script is error free by running it separately. This you have done already so we are good here. As it happened in my case, when I tried this, it gave me back
/usr/bin/python: bad interpreter: No such file or directory
error. Problem was that I had used an old script that was using an interpreter as#!/usr/bin/python
instead of#!/usr/bin/python3
. - Permissions and users: Secondly, It can be a permissions issue. See this answer
- Encoding issues: At times it can be an issue with encoding if you have a script that has windows encoding. Try
dos2unix
to remove any encoding issues
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Dhaval D |