[Tutor] Newbie College Student

Bob Gailer bgailer at alum.rpi.edu
Sun Aug 5 20:42:12 CEST 2007


TheSarge wrote:
> Hi All,
>
> Been tasked with an assignment that is kicking my butt. We have been asked
> to write a py utilizing cgi to ask for simplistic web form guestbook
> information (name and email). I think I am lost on whether or not my actual
> py script needs to link to a cgi script or if I can do it all in a py
> script. It needs to log entries to a text file and everytime I think I have
> it working I go to my text file and it remains empty. I don't know if I can
> post my script code in here so if I can not then my apologies forthcoming.
>   
We welcome code. Without it we can't begin to help. Also if you get an 
exception please post the traceback also.
> Any help or advice is most appreciated.
>
> py script:
>
> #!c:\python25\python.exe
> import cgi, cgitb, os
>
> temp = """
> <html>
> <body>
> <form action=sample.cgi>
> First Name: <input type=text name=fname><br>
> Last Name: <input type=text name=lname><br>
> Email:  <input type=text name=email><br>
> <hr>
> <input type=submit>
> </form>
> </body></html>
>
>
> """
> path = 'c:\\file'
> # Create instance of FieldStorage 
> form = cgi.FieldStorage() 
>
> # Get data from field 'name' 
> fname = form.getvalue('fname') 
>
> # Get data from field 'address' 
> lname = form.getvalue('lname') 
>
> # Get data from field 'email' 
> email = form.getvalue('email')
>
> if not os.path.isdir(path):
>     os.mkdir(path)
>     e=open(path + '\\' + 'logbook.txt','a')
>     e.write("%s#%s#%s\n" % (fname,lname,email))
>     e.close()
> print 'Done'
>
>
> What am I doing wrong? 
I assume you included the form html as a comment, and that the actual 
form is sent to the browser first, then the actual cgi program starts 
with path =

The only problem I see is that the code to write the file is bypassed 
when the path already exists. You should be able to fix that with some 
changes to indentation.
> Been working on this two weekends in a row and the
> assignment is already late.
>   
Next time ask sooner. We don't DO homework for students, but are glad to 
help when we see your effort and specific problems.

It is good that you imported the cgitb module. Just add
cgitb.enable()
to activate it.



-- 
Bob Gailer
510-978-4454 Oakland, CA
919-636-4239 Chapel Hill, NC




More information about the Tutor mailing list