By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,001 Members | 1,176 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,001 IT Pros & Developers. It's quick & easy.

string join() method

P: n/a
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"])
---------------------------------

ascii_temp is just some tab seperated data:
---------------------------------
Allele Seq:Start-End Length Sequence Score
A01 1: 1-8 8 SSSSSSSS 1410538.0
A01 1: 2-9 8 SSSSSSSS 1410538.0
---------------------------------

Thanks everyone.
Derek Basch

Jul 18 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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/
Jul 18 '05 #2

P: n/a
> 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'.
--
damjan
Jul 18 '05 #3

P: n/a
Derek Basch wrote:
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"])
---------------------------------

but this code works?:
---------------------------------
self.output = []
self.setContentType("text/plain")
print ''.join(self.output) 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"])
---------------------------------

ascii_temp is just some tab seperated data:
---------------------------------
Allele Seq:Start-End Length Sequence Score
A01 1: 1-8 8 SSSSSSSS 1410538.0
A01 1: 2-9 8 SSSSSSSS 1410538.0
---------------------------------

Thanks everyone.
Derek Basch

Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.