string join() method

Benjamin Niemann pink at odahoda.de
Wed Mar 23 20:07:57 EST 2005


Derek Basch wrote:

> Can anyone tell me why this CGI code outputs a blank page?
> --------------------------------
> self.output = []
> self.setContentType("text/plain")
> ascii_temp.seek(0)
> self.output.extend(ascii_temp.read())
> print ''.join(self.output)
> 
> def setContentType(self, type="text/xml"):
>     self.output.extend(["Content-type: ", type, "\n\r"])
> ---------------------------------
> 
> but this code works?:
> ---------------------------------
> self.output = []
> self.setContentType("text/plain")
> print ''.join(self.output)
> ascii_temp.seek(0)
> print ascii_temp.read()
> 
> def setContentType(self, type="text/xml"):
>     self.output.extend(["Content-type: ", type, "\n\r"])
> ---------------------------------
> [snip]

First thing: HTTP header lines must be terminated by "\r\n" not "\n\r".
The headers are terminated by another "\r\n". I'm not sure (but I would bet
an Euro or two that it does), but perhaps the webserver sanitizes the
output of CGI script and converts plain "\n" into "\r\n" - if not then you
shouldn't use print to output the headers, because it only outputs
non-HTTPish "\n".

The output of your first script is

---------------------------------------
Content-type: text/plain\r\n
Allele...
---------------------------------------

whereas the second script outputs

---------------------------------------
Content-type: text/plain\r\n
\n
Allele...
---------------------------------------

The (required) "\n" - that should also be an "\r\n" - seperating the headers
from the content is added by the first print.

-- 
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://www.odahoda.de/



More information about the Python-list mailing list