[Web-SIG] Re: Regarding the WSGI draft

Phillip J. Eby pje at telecommunity.com
Sat Aug 28 05:18:17 CEST 2004

At 05:09 PM 8/27/04 -0500, Ian Bicking wrote:
>Though it can cause problems.  E.g., if instead of the cgi server passing 
>sys.stdout.write, it passed:
>def write(s):
>     sys.stdout.write(s)
>That would cause all sorts of problems.  Unless it used 
>sys.__stdout__.write(s); I don't know if that would be a good or bad 
>style.  That's what I did to work around my bug.

There's another way...  make the dummy file object put in for sys.stdout do 

     def write(self,data):
         sys.stdout = self.__oldstdout__
             sys.stdout = self

Voila.  Now, even if the WSGI server is written to use stdout, it still 
works.  The same trick can and should be used for stdin and stderr.

It's messy, but it should suffice.  Actually, to be a really decent 
emulation, the dummy stdout.write() should probably buffer the data, and 
look for flush() before calling the wsgi_writefunc.  Assuming it's not 
still buffering headers.  But I digress.  Clearly, CGI is a pain in the, 
er... gateway.  :)

More information about the Web-SIG mailing list