views:

70

answers:

3

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!!!

+3  A: 

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>")
Jim Brissom
Thanks Jim! Using the 'sys' methods works! I am puzzled now about the print statements. I tried printing the same line from the 'write' above but I get the usual error.
Christopher
I see the following at (http://www.faqs.org/docs/diveintopython/kgp_stdio.html) stdout is a file-like object; calling its write function will print out whatever string you give it. In fact, this is what the print function really does; it adds a carriage return to the end of the string you’re printing, and calls sys.stdout.write.
Christopher
Indeed it does. I used write because of this exact difference - write does not append anything to your string, thus making it easy to spot what the actual error was.
Jim Brissom
A: 

Changing

print "Content-type: text/html" 
print "<P>Hello, World!</p>"

to

print "Content-type: text/html" 
print
print "<P>Hello, World!</p>"

i.e., just inserting an empty print after you're done with the headers, should work fine for you: you specify you're on Windows, so each print will insert the standards-required terminating \r\n sequence (I believe Apache's tolerant of the "missing-'\r'" problem so that would also work on Unix-y platforms, put I'm not 100% certain of that).

Alex Martelli
(OP's using `-u` so `print` won't write a `\r`... although indeed it does not seem to matter for Apache. In general you do need to use `-u` in Python CGI on Windows, otherwise binary uploads/downloads will be garbled and the Content-Length will be wrong, which typically causes the script to hang mysteriously waiting for more input, which is unpleasant.)
bobince
+1  A: 

It appears that if I use the parenthetical 'print' method it works.

#!C:/Python30/python.exe -u
print("Content-type: text/html\n\n<p>Body</p>")

Researching.

Ok, the answer is obvious now. Python 3.0 made 'print' a function, requiring parenthesis! When I run the script from the command-line it gives an identical syntax error.

I actually DID test from the command-line several times and it printed. However, at that point I was actually using version 2.5.1 from cygwin. During debugging I added Python 3.0 to my path and thus I was running the script from the 3.0 version from that point forward, not testing from the command-line again until now.

Whew! Problem solved. A very time-expensive problem.

I appreciate the input. It helped me find the solution!

Christopher