9

I am running to a problem a vps I recently moved to. I am trying to run a python cgi script, but I am getting an apache Premature end of script headers Error.

(I chmod +x the script file)

The script is pretty simple:

#!/usr/bin/env python                                      
import cgi, cgitb
cgitb.enable()

print "Content-type: text/html"
print "<html><body>hello scritp</body></html>"

Now if I name the script as test**.py** it runs fine on server. But if I do it the correct way, calling it test**.cgi** I receive a Internal Server Error.

I run the script from the terminal

./test.cgi

I get no errors

 Content-type: text/html
 <html><body>hello scritp</body></html>

Did anyone encountered before this issue? And a solution for it? :) Cheers

2 Answers 2

10

change the header as:

print "Content-type: text/html\n\n"
0
1

There must be at least an empty line between HTTP headers and body. So

print "Content-type: text/html\n" will work just fine

Reference: Wikipedia

1
  • That's not an empty line. That's a line including text starting with "Content-type". An empty line is a line with nothing between the previous newline character and the newline character coming at the end of the line. So yes, \n\n is required not just \n.
    – Blair
    Commented Mar 25 at 6:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.