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.
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!')
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With