1

I have this fairly complex python script im trying to run which imports other self-written modules and things like sqlite. When I go to run the script, I get a 500: internal server error and the log says: Premature end of script headers.

I know this means that I probably don't have my header correctly placed/typed but I believe I do.

#!/usr/bin/env python

import cgi
import cgitb; cgitb.enable(logdir=..., format="text")

print "Content-type: text/html"
print

Looks right, right?

I continue with the script and print the results at the end. All the other modules don't have the header or the hashbang, but I think that's fine.

What could the problem be?

ehsangh
  • 311
  • 2
  • 6
  • 16
  • What happens if you run your script from command line: `python my_script.cgi` – schlamar Jun 05 '12 at 17:23
  • @ms4py when i run the script with the ide i use, i get the wanted output. – ehsangh Jun 05 '12 at 17:29
  • Which web server are you using? Anything useful in the log? – schlamar Jun 05 '12 at 17:33
  • 2
    Has the script executable permissions? `chmod +x my_script.cgi` – schlamar Jun 05 '12 at 17:35
  • the site is being hosted by networksolutions and the only thing in the log is the 'premature end of script headers error'. And yes, all the scripts have a '755' status. – ehsangh Jun 05 '12 at 17:36
  • 1
    If you are developing under Windows, make sure that the file is saved with Unix line endings `\n`. Does a simple CGI script work or does every CGI script fail? – schlamar Jun 05 '12 at 17:42
  • A colleague and I are exchanging scripts. There are developing on a Windows machine and I am on a Mac. A simple script that we both wrote does work. – ehsangh Jun 05 '12 at 17:54
  • Have you tried converting the line endings? – schlamar Jun 05 '12 at 17:55
  • Can you try `import cgitb; cgitb.enable()` (without arguments), any helpful output now? – schlamar Jun 05 '12 at 17:56
  • The cgitb.enable() doesn't appear to be doing anything, with or without arguments. And print Content-type: text/html + print and print Content-type: text/html\n both produce the same error. – ehsangh Jun 05 '12 at 18:03
  • what do you mean by that? yes. that's the thing. a simple does script works. – ehsangh Jun 05 '12 at 18:05
  • 3
    As already said [before](http://stackoverflow.com/questions/10901662/python-cgi-error-500-premature-end-of-script-headers#comment14213937_10901662) the line endings of your script must be the unix style `\n` instead of the Windows endings `\r\n`. The most editors are capable of letting you choose the endings. – schlamar Jun 05 '12 at 18:08
  • Depends on your editor. Just use google for help, this is a very easy task! – schlamar Jun 05 '12 at 18:14
  • My editor is in unix! http://i.imgur.com/plpNI.png – ehsangh Jun 05 '12 at 18:21
  • Well, in this case the error is probably somewhere in your code. Try to find the issue by adding portions of your code to the simple script working until you find the part of the code causing this error. – schlamar Jun 05 '12 at 18:25

1 Answers1

4

I had same problem. This solved it:

#!/usr/bin/env python
# -*- coding: UTF-8 -*-

.
.
.

print "Content-Type: text/plain;charset=utf-8"
print
Kevin J. Chase
  • 3,856
  • 4
  • 21
  • 43