string join() method

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Derek Basch

    #1

    string join() method

    Can anyone tell me why this CGI code outputs a blank page?
    --------------------------------
    self.output = []
    self.setContent Type("text/plain")
    ascii_temp.seek (0)
    self.output.ext end(ascii_temp. read())
    print ''.join(self.ou tput)

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

    but this code works?:
    ---------------------------------
    self.output = []
    self.setContent Type("text/plain")
    print ''.join(self.ou tput)
    ascii_temp.seek (0)
    print ascii_temp.read ()

    def setContentType( self, type="text/xml"):
    self.output.ext end(["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

  • Benjamin Niemann

    #2
    Re: string join() method

    Derek Basch wrote:
    [color=blue]
    > Can anyone tell me why this CGI code outputs a blank page?
    > --------------------------------
    > self.output = []
    > self.setContent Type("text/plain")
    > ascii_temp.seek (0)
    > self.output.ext end(ascii_temp. read())
    > print ''.join(self.ou tput)
    >
    > def setContentType( self, type="text/xml"):
    > self.output.ext end(["Content-type: ", type, "\n\r"])
    > ---------------------------------
    >
    > but this code works?:
    > ---------------------------------
    > self.output = []
    > self.setContent Type("text/plain")
    > print ''.join(self.ou tput)
    > ascii_temp.seek (0)
    > print ascii_temp.read ()
    >
    > def setContentType( self, type="text/xml"):
    > self.output.ext end(["Content-type: ", type, "\n\r"])
    > ---------------------------------
    > [snip][/color]

    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/

    Comment

    • Damjan

      #3
      Re: string join() method

      > but perhaps the webserver sanitizes the output of CGI script and converts[color=blue]
      > plain "\n" into "\r\n"[/color]

      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

      Comment

      • Kent Johnson

        #4
        Re: string join() method

        Derek Basch wrote:[color=blue]
        > Can anyone tell me why this CGI code outputs a blank page?[/color]

        Maybe because it needs a blank line between the header and the body?
        [color=blue]
        > --------------------------------
        > self.output = []
        > self.setContent Type("text/plain")
        > ascii_temp.seek (0)
        > self.output.ext end(ascii_temp. read())[/color]
        self.output.app end() is probably what you mean. Try this:

        self.output.app end('\r\n')
        self.output.app end(ascii_temp. read())
        [color=blue]
        > print ''.join(self.ou tput)
        >
        > def setContentType( self, type="text/xml"):
        > self.output.ext end(["Content-type: ", type, "\n\r"])
        > ---------------------------------
        >
        > but this code works?:
        > ---------------------------------
        > self.output = []
        > self.setContent Type("text/plain")
        > print ''.join(self.ou tput)[/color]
        The above line will create a blank line because of the extra newline from print.

        Kent
        [color=blue]
        > ascii_temp.seek (0)
        > print ascii_temp.read ()
        >
        > def setContentType( self, type="text/xml"):
        > self.output.ext end(["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
        >[/color]

        Comment

        Working...