Python CGI Premature end of script error depending on script parameters

4.1k views Asked by At

I have a python script which should parse a file and produce some output to disk, as well as returning a webpage linking to the outputted files. When run with a file posted from the HTML form I get no HTML output back, just a 500 error page and the error_log contains the line:

[Mon Apr 19 15:03:23 2010] [error] [client xxx.xxx.121.79] Premature end of script headers: uploadcml.py, referer: http://xxx.ch.cam.ac.uk:9000/

However, the files which the script should be saving are indeed saved to disk.

If I run it without any arguments, the script returns the correct HTML indicating no file was parsed.

All the information I have found on the web about Premature end of script headers implies it is due to either a missing header, or lack of permissions on the python script but neither can apply to me.

The first lines of the script are:

#!/home/nwe23/bin/bin/python
import cgitb; cgitb.enable()
import cgi
import pybel,openbabel
import random
print "Content-Type: text/html"
print

so when run, I can see no way for it to fail to output the header, and it DOES output the header when run without a file to parse, but when given a file produces the error(but still parsed the file and saves the output to disk!).

Does anyone know how this is happening and what can be done to fix it?

I have tried adding wrongly-indented gibberish (such as foobar) at various points in the file, and this results in adding an indent error to the error_log wherever it is, even if its the very last line in the script. The Premature script headers error remains though. Does this mean the script is executing all the way through?

[EDIT] I'm managed to get it working now, it seems one of the calls to an external C++ library through SWIG was failing, but there was no useful error message produced. I've fixed the problem with that, and now the script is working correctly.

It is surprising that the only error in the error_log was about the script headers, when some remote library call had failed. I suppose that's the danger of invoking non-python code? [/EDIT]

1

There are 1 answers

0
Denis Otkidach On BEST ANSWER

When C library gets segmentation fault or otherwise exits in a bad way, the stdout buffer may not be flushed. Using -u option of Python interpreter or flushing it manually should solve "Premature end of script headers", but it won't help with actual problem.