cgi post problem
jano
notvalid at hotmail.com
Wed Sep 25 22:08:58 EDT 2002
"Jeff Davis" <jdavis at empires.org> wrote in message
news:Q14k9.62350$U7.21809935 at twister.socal.rr.com...
>
>
> These two files worked for me (see below). Let me know if you have any
more
> problems.
>
> Regards,
> Jeff
>
> -------------py2.cgi--------------------------
> #!/usr/bin/python2.2
> import cgi
>
> def main():
> try:
> form = cgi.FieldStorage()
> input = form.keys()
>
> print "Content-type: text/html"
> print
> print("""<?xml version='1.0' encoding='UTF-8'?>
> <!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.1//EN'
> 'http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd'>
> <html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en'>
> <head>
> <title>test</title>
> </head>
> <body>""")
> print "<p>",input,"</p>"
> print "</body></html>"
> except:
> pass
>
> if __name__ == "__main__":
> main()
>
> ---------------py2.html------------------------------------------
> <html>
> <body>
>
> <form action="py2.cgi" method="post">
> <input type="text" name="Name" size="20" />
> <input type="text" name="PartNumber" size="20" />
> <input type="text" name="Inventory" size="20" />
> <input type="text" name="Cost" size="20" />
> <input type="text" name="WholesalePrice" size="20" />
> <input type="submit" value="Update Records" />
> </form>
>
> </body>
> </html>
>
>
>
> jano wrote:
>
> > There was an error in my prior post. See ^^^^
> >
> >
> > "jano" <notvalid at hotmail.com> wrote in message
> > news:amq8av$2cum$1 at agate.berkeley.edu...
> >>
> >>
> >> "Jeff Davis" <jdavis at empires.org> wrote in message
> >> news:9aWj9.39314$V7.10439005 at twister.socal.rr.com...
> >> > Hi,
> >> >
> >> > It looks as though you have a lot of code. It might be helpful to try
> >> > to narrow the problem further by removing a lot of the code unrelated
> >> > to
> > your
> >> > problem, which appears to be that "keys" does not contain any items.
> > After
> >> > you work that out, then put the other code back in.
> >> >
> >> > My first impression is that you have some "weird" form element names
> >> > because they contain the "/" character. That might be perfectly
legal,
> > but
> >> > perhaps the cgi.FieldStorage class does not properly handle such
> >> > element names, or perhaps it considers such names erroneous.
> >> >
> >> > Next, it seems that the line "keys = form.keys()" might be out of
> >> alignment
> >> > with the rest of the code in it's block. Perhaps it's just the way it
> >> > looks after you copied your code into the post.
> >> >
> >> > Regards,
> >> > Jeff
> >> >
> >> >
> >>
> >>
> >>
> >> Yes, the problem is that keys does not contain any items. The 'weird'
> > names
> >> are not the problem, because i tried it also with regular 1-word names
> >> (no
> >> slashes), with the same results. Also, the keys=form.keys() line is
> >> properly indented in the code -- it was just a copy/paste problem.
Here
> > is
> >> the code again, with the extraneous stuff stripped out:
> >>
> >> #!/usr/local/bin/python
> >>
> >> import cgitb; cgitb.enable()
> >> import sys, traceback, cgi
> >>
> >> def main():
> >> try:
> >> form = cgi.FieldStorage()
> >> input = form.keys()
> >>
> >> print "Content-type: text/html\n\n"
> >> print("""<?xml version='1.0' encoding='UTF-8'?>
> >> <!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.1//EN'
> >> 'http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd'>
> >> <html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en'>
> >> <head>
> >> <title>test</title>
> >> </head>
> >> <body>""")
> >> print "<p>",keys,"</p>"
> >
> > # ^^^^ should be 'input' not 'keys', as it
> > # is
> > in my script.
> >
> >> print "</body></html>"
> >> except:
> >> pass
> >>
> >> if __name__ == "__main__":
> >> main()
> >>
> >> And the form is:
> >>
> >> <form action="http://someurl.com/cgi-bin/updatexml.py" method="post">
> >> <input type="text" name="Name" size="20" />
> >> <input type="text" name="PartNumber" size="20" />
> >> <input type="text" name="Inventory" size="20" />
> >> <input type="text" name="Cost" size="20" />
> >> <input type="text" name="WholesalePrice" size="20" />
> >> <input type="submit" value="Update Records" />
> >> </form>
> >>
> >>
> >> The script, again, is currently just supposed to return the keys, which
> >> I
> > am
> >> expecting to be Name, PartNumber, etc., but it returns only an empty
> >> list. Any ideas.
> >>
> >> thanks,
> >>
> >> jano
> >>
> >>
> >>
>
Jeff, did this work for you just on the command line? Or over the internet
too? I get an 'internal server error' when I try to submit from your form,
and I don't have access to the server logs -- it works fine from the command
line, but it seems to be a parsing problem with the form data. The server
is running python 2.2.1 (apache 1.3.26) on solaris 5.6. This is a different
problem than my original problem -- keys() returning [] -- since I can't
even get an empty list anymore, but with my original code, i get the
expected list if I use method="submit" (get), and I get an empty list if I
use method="post".
I am about to give up and do what I need to do in Java, as I have deadlines
approaching.
thanks,
jano
More information about the Python-list
mailing list