0

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.

J.Tyc
  • 1
  • 5

1 Answers1

0

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.

Alex Reynolds
  • 95,983
  • 54
  • 240
  • 345
  • Tried that. When I saved it as .cgi, the error was still the same. When I saved that as .py and tried to access it from the web, there was an error that said "This site can't be reached" with the description "ERR_CONTENT_DECODING_FAILED", which is the same message I got, when python ecnountered some error. But when I tried those commands on the server console, it worked just fine. – J.Tyc Oct 09 '17 at 15:17
  • I would double-check your headers, and also double-check your Apache configuration file. Maybe start with a bare minimum configuration in the Apache configuration, so that you know your web server isn't adding additional headers after you send your content. The following SO post may be useful: https://stackoverflow.com/questions/14039804/error-330-neterr-content-decoding-failed – Alex Reynolds Oct 09 '17 at 18:12