CGI module does not parse data

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

    CGI module does not parse data

    I am writing a webserver, and I want it to be able to run python
    scripts. But when I set sys.stdin to self.rfile (using the
    BaseHTTPServer class, self.rfile is a input stream containing the
    request), the cgi module does not parse the data.
    Example script:
    import cgi
    form = cgi.FieldStorag e()
    print form["testfield"]

    The above script would print nothing. Should i be setting sys.stdin to
    something else, or is there anthoer solution entirely?

  • Fredrik Lundh

    #2
    Re: CGI module does not parse data

    "amfr" wrot3e:
    [color=blue]
    > I am writing a webserver, and I want it to be able to run python
    > scripts. But when I set sys.stdin to self.rfile (using the
    > BaseHTTPServer class, self.rfile is a input stream containing the
    > request), the cgi module does not parse the data.
    > Example script:
    > import cgi
    > form = cgi.FieldStorag e()
    > print form["testfield"]
    >
    > The above script would print nothing. Should i be setting sys.stdin to
    > something else, or is there anthoer solution entirely?[/color]

    are you writing your own CGIHTTPServer ?



    why ?

    </F>



    Comment

    • amfr

      #3
      Re: CGI module does not parse data

      I have included some of the content of that file, I am writing this as
      an extension to my ebserver which is based on BaseHTTPServer. This
      part of the code was taken directly from the CGIHTTPServer file,
      nothing changed

      Comment

      • amfr

        #4
        Re: CGI module does not parse data

        I just read somewhere that the CGIHTTPServer module does not work on
        mac (which I am currently using), is this true?

        Comment

        • Peter Hansen

          #5
          Re: CGI module does not parse data

          amfr wrote:[color=blue]
          > I just read somewhere that the CGIHTTPServer module does not work on
          > mac (which I am currently using), is this true?[/color]

          It might help a lot if you could include a link to "somewhere" , so we'd
          know what "does not work" meant... often it means one particular feature
          is not perfect, as opposed to "crashes and burns" or "does nothing at
          all"...

          Also, someone who has it "working" on a Mac hasn't necessarily tried out
          the entire range of functionality so the thing you read might still be
          correct.

          -Peter

          Comment

          • Mardy

            #6
            Re: CGI module does not parse data

            Le die Thu, 01 Dec 2005 15:08:14 -0800, amfr ha scribite:
            [color=blue]
            > I have included some of the content of that file, I am writing this as
            > an extension to my ebserver which is based on BaseHTTPServer. This
            > part of the code was taken directly from the CGIHTTPServer file,
            > nothing changed[/color]

            I did the same, it is working for me. About Mac problems, the only thing I
            know is that in the code of CGIHTTPServer itself a comment says that
            execfile() is the only way to run a CGI on Mac. But it should work.

            Maybe it's just your browser, which doesn't show you the output of the
            CGI. Did you try connecting to the webserver by telnet?


            --
            Saluti,
            Mardy


            Comment

            • amfr

              #7
              Re: CGI module does not parse data

              I am using execfile, setting stdin and stdout like this:
              sys.stdin = self.wfile
              sys.stdout = self.rfile
              execfile(filena me)

              Its the same code used in the CGIHTTPServer module. I know that the
              python is executing corretly, a script with this content would work:

              print "<html>"
              print "<head>"
              print "<title>"
              print "blah"
              print "</title>"
              print "</head>
              print "<body>"
              print "test"
              print "</body>"
              print "</html>"

              but this would not (lets say i submitted a form with the value of test
              being "hello world"):

              import cgi
              form = cgi.FieldStorag e()
              print form["test"]
              print "test"

              I would only be able to see "test", not "hello world"
              I am sure its not my browser

              Comment

              • Tim Roberts

                #8
                Re: CGI module does not parse data

                "amfr" <amfr.org@gmail .com> wrote:[color=blue]
                >
                >import cgi
                >form = cgi.FieldStorag e()
                >print form["test"]
                >print "test"
                >
                >I would only be able to see "test", not "hello world"
                >I am sure its not my browser[/color]

                Did you mean:

                print form["test"].value
                --
                - Tim Roberts, timr@probo.com
                Providenza & Boekelheide, Inc.

                Comment

                • Mardy

                  #9
                  Re: CGI module does not parse data

                  Le die Fri, 02 Dec 2005 12:18:28 -0800, amfr ha scribite:[color=blue]
                  > import cgi
                  > form = cgi.FieldStorag e()
                  > print form["test"]
                  > print "test"
                  >
                  > I would only be able to see "test", not "hello world"
                  > I am sure its not my browser[/color]

                  As Tim said, you have tu use "form['test'].value", because "print
                  form['test']" will make pythoun output something like this:
                  <form object at ....>
                  and your browser won't show it, mistaking it as an HTML tag.


                  --
                  Saluti,
                  Mardy


                  Comment

                  • amfr

                    #10
                    Re: CGI module does not parse data

                    Neither work

                    Comment

                    • Steve Holden

                      #11
                      Re: CGI module does not parse data

                      amfr wrote:[color=blue]
                      > Neither work
                      >[/color]
                      But you don't give us any further information to go on.

                      Are you importing cgitb and calling cgitb.enable() to trap and print any
                      errors that might occur?

                      Are you looking in the browser at the HTM source (view source) of the
                      page your server is returning to see more closely waht your browser is
                      receiving, rather than what it's displaying?

                      regards
                      Steve
                      --
                      Steve Holden +44 150 684 7255 +1 800 494 3119
                      Holden Web LLC www.holdenweb.com
                      PyCon TX 2006 www.python.org/pycon/

                      Comment

                      • Devan L

                        #12
                        Re: CGI module does not parse data


                        amfr wrote:[color=blue]
                        > I am writing a webserver, and I want it to be able to run python
                        > scripts. But when I set sys.stdin to self.rfile (using the
                        > BaseHTTPServer class, self.rfile is a input stream containing the
                        > request), the cgi module does not parse the data.
                        > Example script:
                        > import cgi
                        > form = cgi.FieldStorag e()
                        > print form["testfield"]
                        >
                        > The above script would print nothing. Should i be setting sys.stdin to
                        > something else, or is there anthoer solution entirely?[/color]
                        try this before trying to print:
                        print "Content-Type: text/html\n"

                        Comment

                        • Tim Roberts

                          #13
                          Re: CGI module does not parse data

                          "amfr" <amfr.org@gmail .com> wrote:[color=blue]
                          >
                          >Neither work[/color]

                          Yes, they do.

                          Post your form HTML and the Python code you're using, and we'll show you
                          what you're doing wrong.
                          --
                          - Tim Roberts, timr@probo.com
                          Providenza & Boekelheide, Inc.

                          Comment

                          Working...