Run Python CGI Script on Windows XP
Question: This exact question has been asked before but I am at my wits end! I've spend
4 hours trying to get a SIMPLE Python CGI script to work on Windows XP but I
get errors. Please save my sanity!

Python Script register.py

    
    
    #!c:/Python30/python.exe -u
    
    print "Content-type: text/html" 
    print "<P>Hello, World!</p>"
    

Script is located in:  
C:\Program Files\Apache Software Foundation\Apache2.2\cgi-bin\alerter

Apache Error Log:

    
    
    [Tue Sep 21 19:06:36 2010] [error] [client 127.0.0.1] Premature end of script headers: register.py
    [Tue Sep 21 19:06:36 2010] [error] [client 127.0.0.1]   File "C:/Program Files/Apache Software Foundation/Apache2.2/cgi-bin/alerter/register.py", line 3\r
    [Tue Sep 21 19:06:36 2010] [error] [client 127.0.0.1]     print "Content-type: text/html"\r
    [Tue Sep 21 19:06:36 2010] [error] [client 127.0.0.1]                                   ^\r
    [Tue Sep 21 19:06:36 2010] [error] [client 127.0.0.1] SyntaxError: invalid syntax\r
    

httpd.conf:

    
    
    LoadModule cgi_module modules/mod_cgi.so
    
    <Directory "C:/Program Files/Apache Software Foundation/Apache2.2/cgi-bin">
        AllowOverride None
        Options None
        Order allow,deny
        Allow from all
        Options +ExecCGI
        AddHandler cgi-script .py
    </Directory>
    

This should be VERY simple. Yes? I mus be missing that ONE thing that will
make it finally work. I got PHP working a while back with no problems.

Any ideas? Thanks!!!


Answer: Your error is:

> Premature end of script headers

Note that the HTTP protocol specifies that the body of a HTTP response is
separated from it's headers by a blank line (i.e. two times a carriage return
and line feed). I'd go for something like:

    
    
    import sys
    sys.stdout.write("Content-type: text/html\r\n\r\n<p>Body</p>")
    

