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"
Sign up to request clarification or add additional context in comments.

Comments

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

3 Comments

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 By default, Python's print() function adds a newline character at the end of its output. So just one explicit \n will be enough. There is no need to explicitly add two newline charactors.
that's absolutely true, an additional parameter (in Python 3) or trailing comma (Python 2) is explicitly required to avoid print appending a newline. Think I may have been thinking of sys.stdout.write (which does not append a newline) which I'd gotten in the habit of using in my CGI scripts. Apologies

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.