Where O Where Did My Data Buffer Go?

Steve Holden sholden at holdenweb.com
Wed May 2 20:59:35 EDT 2001


"Ben Ocean" <zope at thewebsons.com> wrote in message
news:mailman.988843445.14892.python-list at python.org...
> 0At 09:54 PM 5/2/2001 +0000, you wrote:
> >"Ben Ocean" <zope at thewebsons.com> wrote in ...
> > > Hi,
> > > I'm trying (for several days now) to capture data in a data buffer and
> > > parse it. Someone suggested I use the *parse* functions in the cgi
module
> > > (which include *parse_qs* and *parse_qsl*). Okay, great. Now, how do I
get
> > > the freakin data buffer in there? In other words...
> > > parse_qsl(???)
> > > and I need to fill in the ??? It's not as easy as just tossing in
*form*
> > > from this line:
> > > form = cgi.FieldStorage()
> > > So, what do I do?
> > > TIA,
> > > BenO
> > >
> >Ben:
> >
> >I should say the first thing you needed was a clue. I've responded to
your
> >inquiries on the newsgroup and by email, without ever having any reply
>
> Really?? I thought I had gratefully responded to everyone that had helped
> me! Apologies.
>
No problem.

> >, or
> >any indication that your insight into the problem had improved.
> >Are you simply stabbing around blindly, with no knowledge of the
> >architecture of the system you are trying to build a component for?
What's
> >going on here? Asking the same question using five different wordings
does
> >not enlighten the unenlightened :-)
> >
> >Where IS "the freaking data buffer"? We aren't mind readers (though some
on
> >c.l.py do well at psychic debugging). Is the page you are framing from
some
> >other site a form?
>
> I *have* made that fact perfectly clear in other posts, Steve.
>
OK, so let's assume you have to write a page which handles the submission
from a form.

> >If it is, then all it can send is CGI data, and the CGI
> >module will meet your needs. What else do you need to know?
>
> Um, maybe *how* to utilize the module? Try as I may, I haven't gotten it
to
> spit out the data. That's why I asked above, what variable do I put into
> the parser to parse? And what kind of variable is it? A tuple? A
dictionary?
>
Right. I've seen a couple of examples which might help you. Do you have any
insight into the KBB form? Have you looked at the HTML source? That will
tell you what the individual fields are called. Look for <INPUT ...
NAME="FieldName"...> to build up a list of fieldnames the form will (or
might) be submitting.

> >We can't help you solve your problem until we understand it. Until *you*
> >understand it, that goal seems some distance off.
>
> Well, of course, that *is always* the problem with people who are new to
> programming. If you can remember back in the distant past when you were
new
> to this, unless you were blessed to have someone there to teach you, well,
> it's a bitch, huh? You can be a little understanding, right? If you care
to
> point out where this newbie's screwing up, here's the source code, such as
> it is. Your help (or anyone's!) is appreciated.

Be happy to try. Sorry if my last posting seemed a little ungracious, but
you keep asking questions, and none of the answers seem to do you any good!

> TIA,
> BenO
>
>  >>>
> #!/usr/bin/python
> import os, sys, cgi, string, htmllib, xdrlib

I'm guessing you will need cgi (to read the form data) and smtplib (since
you seem to want to send mail containing the form data). string is a
possibility. Forget htmllib, which is used in web client software to parse
the HTML you get from a web server. You are getting CGI data from a client.
xdrlib is to handle a presentation layer used in Sun's Remote Procedure Call
protocol.

> print "Content-type: text/html\n\n"

Nothing wrong with this - this is an HTTP header to be sent back to the
browser frame in which the user clicked the submit button on the KBB form.

> form = cgi.FieldStorage()

This is also good. Form is now an object, an instance of type FieldStorage.
You can extract the values of individual fields from this quite easily.
Suppose the form has <INPUT NAME="FirstName" ...> in it. You will be able to
extract the value of that field from the transmitted data with the
expression

        form["FirstName"].value

> Form = sys.stdin.read()

Forget standard input. By this time it's already been consumed by the
creation of the FieldStorage instance, which has doen all the hard work for
you.

> formstuff = ""
>
I'll let you off with that, because I can see you want to build a message.
This seems a bit ambitious until you can see in the browser pahe that you
have captured the input you need. However.

> for i in range(len(Form)):

Erm, presumably you wanted to scan every character in the input stream?
Forget it!

> FormData = sys.stdin.readline()

Even if stdin was any use, the read() above would have taken all the
characters out of it.

> safe = string.maketrans("`|~!@#$%^&*()_+=:;{}[]",
"----------------------")

Safety is good, but it's only really an issue if you are using form inputs
in system commands.

> line =
> string.replace(string.replace(string.replace(string.translate(FormData,
> safe), "<input type-\"hidden\" name-\"", "."), "\" value-\"", "---"),
> "\">",".")

Sorry, you lost me here.

> formstuff = string.join([formstuff,line])

Not really sure what this is supposed to do either.

> FormData.close() # close the file

Usually you close a file, not a line you read in from it.

> import smtplib
> server = smtplib.SMTP("localhost")

This would be ok, but before you call sendmail you need a message in a
suitable format. There's a module called mimewriter that can help with that,
but I think you need to concentrate first of all on verifying that you are
correctly getting data from the KBB form before you start trying to do
anything too ambitious with it.

> server.sendmail("beno at thewebsons.com", "beno at thewebsons.com", formstuff)
> server.quit()

If formstuff were a valid RFC822 message, this would be fine. Oh, but we
never sent any HTML to the browser...

> <<<
>
OK, let's see. Assume you have fields in the form called "FirstName" and
"LastName". Let's write a CGI web page which will allow you to see their
values IN THE BROWSER WINDOW. This might be enough to get you going, and we
can cover the mail stuff later -- that's easy!

#!/usr/bin/python

import cgi

form = cgi.FieldStorage
print """Content-Type: text/html

<HTML>
<HEAD>
</HEAD>
<BODY>
"""
for fieldname in ["FirstName", "LastName"]:
    print fieldname, "is", form[fieldname].value, "<BR>"
print """
</BODY>
</HTML>
"""

That ought to do it if you replace the list of fieldnames with names from
the actual form. Report back and tell us what happened.

regards
 Steve







More information about the Python-list mailing list