Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting python to work, Internal Server Error

Tags:

python

I'm trying to get Python scripts, called from a web browser, to work. I keep getting the error:

500 Internal Server Error

When I check my error logs I see the message

Premature end of script headers

The only documentation of this error online says that it can be the result of having improper line return characters in your script, but I wrote my test script right from the shell with pico. Also, when I run the file from the command line it executes just fine. " So far the only change I have made to apache is to add the .py to the "AddHandler cgi-script" line.

Thanks!


Thanks for the quick responses. Here is the latest version of the test code. I added a couple new lines before the output as suggested but still get the same error:

#!/usr/local/bin/python
print "Content-type: text/html\n"
print "\n\n"
print "<HTML>"
print "<HEAD>"
print "<TITLE>Test</TITLE>"
print "</HEAD>"
print "<BODY>"
print "<H2>Hi there.</h2>"
print "</BODY>"
print "</HTML>"

Some other details: I am running Apache 1.3 and don't have mod_python. I set apache to recognize .py as mentioned above. I am running the script from the main public_html folder.


An update. It doesn't seem to matter what I put in the shebang line. I tried all of the suggestions and even if I leave it blank the same error is showing up in the errors log and I'm getting a 500 error.

I'm running Apache/1.3.41 on linux (red hat 3.4) with WHM/Cpanel installed.

like image 249
user47514 Avatar asked Dec 18 '08 18:12

user47514


2 Answers

I had a similar problem, the problem is that you need to have two lines breaks after printing the content type. The following worked for me :

#!/usr/local/bin/python2.6
print('Content-type: text/html\r\n')
print('\r\n')
print('Hello, World!')
like image 94
Faouzi FJTech Avatar answered Oct 01 '22 05:10

Faouzi FJTech


This is the exact behavior you would get if your Python script does not have the executable permission set.

Try:

chmod a+x foo.py

(where foo.py is your script name).

See the Apache tutorial for more information.

like image 37
Matthew Christensen Avatar answered Oct 01 '22 05:10

Matthew Christensen