tags:

views:

419

answers:

9

Hi,

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!

+4  A: 

Two things spring immediately to mind.

  1. Make sure you are outputting the Content-Type: text/html header
  2. Make sure you are adding two newlines ("\n") after the headers before you output "Hello, world" or whatever.
Triptych
+2  A: 

do you have something like this at the top before you print anything else?

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

If you already have this, then post your code.

Jack
Two \n (HTTP requires an empty line after the headers)
bortzmeyer
A: 

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.

what are your permissions on the file?
Jack
Can you run the script by hand from the command-line? It would allow to test all the issues related with the shebang line.
bortzmeyer
please edit your original question if you have additional info to add
hop
A: 

You may also get a better error message by adding this line at the top of your Python script:

import cgitb; cgitb.enable()

Also, the header should be capitalized Content-Type, not Content-type, although I doubt that that is breaking anything.

Triptych
I added that line to the top of the script and capitalized "Type" but still am getting the same error and no additional information.
What does your shebang line look like? And what's your platform?
Triptych
Indeed, capitalization does not matter for header names. The HTTP standard, RFC 2616, says in section 4.2 that HTTP headers have the same general syntax as the headers of RFC 822. And RFC 822, section 3.4.7, says that header names are case-insensitive.
bortzmeyer
+4  A: 

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.

Matthew Christensen
heh, this is probably right. Cant' believe I didn't think of that.
Triptych
A: 

OK last guess:

Trying changing that shebang line to:

#!/usr/bin/env python

or

#!/usr/bin/local/env python

It would also be helpful to know your platform / hosting provider.

Triptych
A: 

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.

A: 

I had a similiar problem running my own local server, but got it solved following this guide step-by-step: HOWTO Use Python in the web

Aputsiaq
A: 

Sounds to me like you're using a script written in Windows on a Unix machine, without first converting the line-endings from 0d0a to 0a. It should be easy to convert it. One way is with your ftp program; transfer the file in ASCII mode. The way I use with Metapad is to use File->FileFormat before saving.