[mod_python] Solution (bug): cgihandler repeats the whole response for each print command

Mike Looijmans mike.looijmans at asml.com
Wed Feb 19 11:56:07 EST 2003

Found the cause, the problem is in "apache.py" (installed as
/usr/local/lib/python2.2/site-packages/mod_python/apache.py on my system).

Line # 693 says (in function write() of CGIStdout:

        if not self.headers_sent:

But in the code, it NEVER sets 'self.headers_sent' to "1", thus it will never
execute the "else" part of this if.

To fix it, I inserted the following at line # 724 (the last two lines were
already there):

                ## BUG FIX: Send out duplicates....
                self.headers_sent = 1
                # write the body if any at this point

Mike Looijmans
ASML: http://www5nl.asml.nl/~mlooijma
Private: http://www.milosoftware.com

-----Original Message-----
From: Mike Looijmans <mike.looijmans at asml.com>
To: mod_python list <mod_python at modpython.org>
Date: Wednesday, February 19, 2003 11:08 AM
Subject: [mod_python] cgihandler repeats the whole response for each print

>To allow a quick migration of thousands of CGI code lines, I intended to use
>the cgihandler.py handler to run existing CGI script.
>However, if I setup things that way, and use a small CGI script like this
>print 'status: 200 OK'
>print 'content-type: text/html'
>print '<HTML><HEAD><TITLE>Hello world</TITLE></HEAD>'
>print '<BODY><P>Hello Mod_Python world!</P></BODY>'
>print '</HTML>'
>This would yield a simple 'hello world' page, but the output that is sent to
>the browser is as follows:
>HTTP/1.1 200 OK
>Date: Wed, 19 Feb 2003 09:58:12 GMT
>Server: Apache/2.0.44 (Unix) mod_python/3.0.1 Python/2.2.2
>Content-Type: text/html; charset=ISO-8859-1
>Connection: close
><HTML><HEAD><TITLE>Hello world</TITLE></HEAD>
><BODY><P>Hello Mod_Python world!</P></BODY><HTML><HEAD><TITLE>Hello
><BODY><P>Hello Mod_Python world!</P></BODY>
><HTML><HEAD><TITLE>Hello world</TITLE></HEAD>
><BODY><P>Hello Mod_Python world!</P></BODY>
><BODY><P>Hello Mod_Python world!</P></BODY>
>It seems that (except for the headers) the "sys.stdout" from the cgihandler
>sends the whole output history again on each "print" command, like this class
>class FaultyIO:
>    def __init__(self, req)
>        self.msg = ''
>        self.req = req
>    def write(self, s):
>        self.msg += s
>        req.send(self.msg)
>Mod_python mailing list
>Mod_python at modpython.org

More information about the Mod_python mailing list