string join() method

D

Derek Basch

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"])
 
B

Benjamin Niemann

Derek said:
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"])

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...
 
D

Damjan

but perhaps the webserver sanitizes the output of CGI script and converts
plain "\n" into "\r\n"

Yes apache does this, since it adds its own headers anyway it will replace
all '\n' in the headers with '\r\n' and '\n\n' with '\r\n\r\n'.
 
K

Kent Johnson

Derek said:
Can anyone tell me why this CGI code outputs a blank page?

Maybe because it needs a blank line between the header and the body?
--------------------------------
self.output = []
self.setContentType("text/plain")
ascii_temp.seek(0)
self.output.extend(ascii_temp.read())
self.output.append() is probably what you mean. Try this:

self.output.append('\r\n')
self.output.append(ascii_temp.read())
print ''.join(self.output)

def setContentType(self, type="text/xml"):
self.output.extend(["Content-type: ", type, "\n\r"])
The above line will create a blank line because of the extra newline from print.

Kent
ascii_temp.seek(0)
print ascii_temp.read()

def setContentType(self, type="text/xml"):
self.output.extend(["Content-type: ", type, "\n\r"])
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,228
Messages
2,571,157
Members
47,785
Latest member
deepusaini

Latest Threads

Top