there is a problem with popen2: by default, this is called in binary mode,
but then, all linefeeds and carriage returns are stripped from the python script's output by popen2. Using this dumb example in a cgi-bin subdrectory, this is easily demonstrated:

Code:

testdoc = """<html><body>\n\n
<h1>Huh?</h1>\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
Where are my line feeds?</body></html>\n\n"""

print "Content-type: text/plain\r\n\r\n"
print
print testdoc

The result is an empty reply and/or a hanging client.
A telnet to the server port demonstrates this too: