0
votes

I have a VPS server with Apache on it. I want to execute simple CGI script. I created a python script, saved it as .cgi file and placed it to the folder cgi-bin, but it only displays error message: "End of script output before headers".

However when I saved this script as .py file and did not place it into cgi-bin folder, it worked, but whenever there was an error, it did not show any error message, just server error. Command cgitb.enable() did not show any error.

I tried to give the file 755 permission, but that still did not solve my problem.

Where could be a problem?

Source code:

#!/usr/local/bin/python3
print("Content-type:text/html")
print("")
print ("<html><head><title>CGI</title></head>")
print ("<body>")
print ("hello cgi")
print ("</body>")
print ("</html>")

Thank you for your answers.

1

1 Answers

0
votes

It might be more useful to use sys.stdout.write and add \n so that you know exactly what gets written to standard output:

#!/usr/bin/env python

import sys
import os

sys.stdout.write('Status: 200 OK\n')
sys.stdout.write('Content-Type: text/html; charset=utf-8\n\n')
sys.stdout.write('<html><body>Hello, world!</body></html>\n')
sys.exit(os.EX_OK)

Run the script with python script.py | cat -e so that you can verify line endings.

Make sure you aren't sending any more HTTP headers after you start sending content.