cgi problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Paul Rubin

    cgi problem

    I'm trying to write a simple cgi that reads a post from the user's
    browser, does some stuff with the form data, and redirects the browser
    back to the originating url, i.e. I want the cgi to send a 302
    redirect.

    There's no obvious way in the cgi module to set the response code to
    anything but 200. I.e. the run_cgi method automatically sends the 200
    response without giving any way to suppress it (like nph-whatever in
    Apache). Is that a missing feature that I should add, or am I trying
    to do something dumb?

    Thanks.
  • Kent Johnson

    #2
    Re: cgi problem

    Paul Rubin wrote:[color=blue]
    > I'm trying to write a simple cgi that reads a post from the user's
    > browser, does some stuff with the form data, and redirects the browser
    > back to the originating url, i.e. I want the cgi to send a 302
    > redirect.
    >
    > There's no obvious way in the cgi module to set the response code to
    > anything but 200. I.e. the run_cgi method automatically sends the 200
    > response without giving any way to suppress it (like nph-whatever in
    > Apache). Is that a missing feature that I should add, or am I trying
    > to do something dumb?[/color]

    Set the Location: header to the new URL.



    Kent

    Comment

    • Thomas Guettler

      #3
      Re: cgi problem

      Am Wed, 08 Mar 2006 00:19:55 -0800 schrieb Paul Rubin:
      [color=blue]
      > I'm trying to write a simple cgi that reads a post from the user's
      > browser, does some stuff with the form data, and redirects the browser
      > back to the originating url, i.e. I want the cgi to send a 302
      > redirect.[/color]

      Hi,

      I have this setup for a small script (for bigger things I use quixote)



      def cgi_main():
      stdout=sys.stdo ut
      sys.stdout=sys. stderr # print soll zu Apache-Log
      try:
      html=cgi_html()
      except ReturnThis, r:
      stdout.write(st r(r))
      return
      stdout.write("C ontent-Type: text/html\n\n%s" % html)

      CGI-Script *very* small
      ............
      # Python Imports
      import os
      import sys

      import cgitb
      cgitb.enable()
      import foo

      if __name__=="__ma in__":
      foo.cgi_main()

      .............

      file foo:

      def cgi_main():
      stdout=sys.stdo ut
      sys.stdout=sys. stderr # print should go to Apache-Log
      try:
      html=cgi_html()
      except ReturnThis, r:
      stdout.write(st r(r))
      return
      stdout.write("C ontent-Type: text/html\n\n%s" % html)


      class ReturnThis(Exce ption):
      pass

      class Redirect(Return This):
      def __init__(self, destination):
      if os.environ.get( "HTTPS")==" on":
      http="https://"
      else:
      http="http://"
      url="%s%s%s%s" % (http, os.environ["SERVER_NAM E"], os.environ["SCRIPT_NAM E"],
      destination)
      header="Status: 302 Moved Temporarily\nLo cation: %s\n\n" % (
      url)
      ReturnThis.__in it__(self, header)


      Now you can 'raise Redirect("myloc ation")' anywhere in your code.

      HTH,
      Thomas

      --
      Thomas Güttler, http://www.thomas-guettler.de/
      E-Mail: guettli (*) thomas-guettler + de
      Spam Catcher: niemand.leerman n@thomas-guettler.de

      Comment

      • Paul Rubin

        #4
        Re: cgi problem

        Thomas Guettler <niemand.leerma nn@thomas-guettler.de> writes:[color=blue][color=green]
        > > back to the originating url, i.e. I want the cgi to send a 302 redirect.[/color]
        >
        > I have this setup for a small script (for bigger things I use quixote)...[/color]

        Thanks. It looks like you've written your cgi completely from
        scratch. I was hoping to use the cgi module, which has some
        convenient features for reading the query parameters and POST content.
        I should look into Quixote and some of the other Python web frameworks
        but this particular task is pretty simple so I thought I'd just use a cgi.

        Comment

        • Tim Roberts

          #5
          Re: cgi problem

          Paul Rubin <http://phr.cx@NOSPAM.i nvalid> wrote:[color=blue]
          >
          >Thomas Guettler <niemand.leerma nn@thomas-guettler.de> writes:[color=green][color=darkred]
          >> > back to the originating url, i.e. I want the cgi to send a 302 redirect.[/color]
          >>
          >> I have this setup for a small script (for bigger things I use quixote)...[/color]
          >
          >Thanks. It looks like you've written your cgi completely from
          >scratch. I was hoping to use the cgi module, which has some
          >convenient features for reading the query parameters and POST content.[/color]

          Yes, but the CGI module doesn't write anything, so the advice of writing a
          "Location:" header still applies.
          --
          - Tim Roberts, timr@probo.com
          Providenza & Boekelheide, Inc.

          Comment

          • Paul Rubin

            #6
            Re: cgi problem

            Tim Roberts <timr@probo.com > writes:[color=blue]
            > Yes, but the CGI module doesn't write anything, so the advice of writing a
            > "Location:" header still applies.[/color]

            Aha, it's coming from CGIHTTPServer.p y:CGIHTTPReques tHandler.run_cg i()
            where it says

            self.send_respo nse(200, "Script output follows")

            I got the two modules confused. This still leaves me with the same
            basic problem, how to suppress the sending of that header.

            Thanks.

            Comment

            • Kent Johnson

              #7
              Re: cgi problem

              Paul Rubin wrote:[color=blue]
              > Tim Roberts <timr@probo.com > writes:
              >[color=green]
              >>Yes, but the CGI module doesn't write anything, so the advice of writing a
              >>"Location:" header still applies.[/color]
              >
              >
              > Aha, it's coming from CGIHTTPServer.p y:CGIHTTPReques tHandler.run_cg i()
              > where it says
              >
              > self.send_respo nse(200, "Script output follows")
              >
              > I got the two modules confused. This still leaves me with the same
              > basic problem, how to suppress the sending of that header.[/color]

              Ah, now I get it. This does look like a bug in CGIHTTPServer to me. As
              you note, it hardcodes the 200 status response. The CGI spec says that
              the CGI program can use the Status: header to tell the server what
              status code to send but CGIHTTPServer doesn't do that.

              ISTM the spec requires the server to buffer and interpret the HTTP
              headers from the CGI so the Status header can be set based on the CGI
              response.

              Comment

              • Thomas Guettler

                #8
                Re: cgi problem

                Am Thu, 09 Mar 2006 00:35:25 -0800 schrieb Paul Rubin:
                [color=blue]
                > Tim Roberts <timr@probo.com > writes:[color=green]
                >> Yes, but the CGI module doesn't write anything, so the advice of writing a
                >> "Location:" header still applies.[/color]
                >
                > Aha, it's coming from CGIHTTPServer.p y:CGIHTTPReques tHandler.run_cg i()
                > where it says
                >
                > self.send_respo nse(200, "Script output follows")
                >
                > I got the two modules confused. This still leaves me with the same
                > basic problem, how to suppress the sending of that header.[/color]

                I had this problem, too:



                CGIHTTPServer writes "200" before the script gets executed!

                You can return this:
                """<html>
                <head>
                <meta http-equiv="refresh"
                content="1; url=...">
                </head>
                </html>"""


                Thomas

                --
                Thomas Güttler, http://www.thomas-guettler.de/
                E-Mail: guettli (*) thomas-guettler + de
                Spam Catcher: niemand.leerman n@thomas-guettler.de

                Comment

                Working...