Python CGI Error 500: Premature end of script head

2020-07-27 20:26发布

问题:

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?

回答1:

I had same problem. This solved it:

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

.
.
.

print "Content-Type: text/plain;charset=utf-8"
print


标签: python cgi