[Tutor] Problem with a variable in a CGI program

Mark Kels mark.kels at gmail.com
Fri Jan 7 15:14:12 CET 2005


HI all !
I started to read the following code (I will start working on it when
this problem is fixed) and it looks OK while I read it. But for some
reason it doesn't work...
Here is the code:

# MailList is copyright (c) 2000 of Programmed Integration
# You may use this code in any way you see fit, but please
# let use know where it is being used. If you make any changes to
# the source code, please send us a copy so we can incorporate
# the changes in future releases. If you have any other comments
# please feel free to contact us at support at programmedintegration.com

# MailList version 1.0.0 22 August 2000

import sys
sys.path.append('/lib/python1.5')          # My ISP requires this to
correctly locate Python Modules

import cgi, re, string                     # Import all the required modules

try:
   useHead=open("head.txt", "r")           # Open the HTML header file
   useFoot=open("foot.txt", "r")           # Open the HTML footer file

   ContentLine="Content-type: text/html"   # Standard content type for
HTML files

   useform=cgi.FieldStorage()              # Assign all variables on
web form to UseForm variable
   email= useform["email"].value           # Assign from form to local
variables. Proposed email address
   email2= useform["email2"].value         # ditto, verified email address
   password= useform["password"].value     # ditto, proposed password
   password2= useform["password2"].value   # ditto, verified password
   action=useform["action"].value          # ditto, action, i.e
subscribe or unsubscribe

   try:   
      optout=useform["optout"].value       # ditto, optout clause, yes or no
   except:                                 # I've enclosed this in a try/except
      optout='no'                          # as if the checkbox is
unchecked, nothing
                                           # was returned.  This way
'no' is returned

   print ContentLine                       # Print standard content line
   print                                   # Needs a blank line following
   print useHead.read()                    # Print HTML header
   if email!=email2:                       # Checks to see if two
entered email addresses match
      print "Email addresses do not match" # If no match print error 
      sys.exit(0)                          # Exit script with error 0
   elif password!=password2:               # Checks to see if two
entered passwords match
      print "Passwords do not match"       # If no match print error
      sys.exit(0)                          # Exit script with error 0
   
   useMailList=open("maillist.txt", "r")   # Open mailling list
   memMailList=useMailList.readlines()     # Assign mailing list to
internal list
   useMailList.close                       # Close mailing list
   found=0                                 # Create found variable
   counter=0                               # Create counter variable
   for UseLine in memMailList:             # For loop until list end is reached
      if string.find(string.upper(UseLine), string.upper(email))!=-1:
# Checks to see if email is in mailing list
         found=1                           # If yes, found = true (1)
         UseSplit=re.split(',',UseLine)    # Create list of found line
and delimit with a comma
         break                             # Exit for loop
      counter=counter+1                    # If not found incrememnt
count and repeat
   if not found:                           # If email address not
found in mailing list
      if action=="unsubscribe":            # And if action is unsubscribe
         print "Email address <B>"+email+"</B> does not exist on our
database" # Print error message
      else:                                # Otherwise
         lineuse=email+","+password+','+optout+"\n" # Form valid
mailing list entry
         memMailList.append(lineuse)       # Add to internal list
         print "Subscription for <B>"+email+"</B> successful<P>"
#Print success to HTML
         print "Many thanks for subscribing.  Please be sure to make a note"
         print "of the email address you subscribed with.  You will only"
         print "be able to unsubscribe if you use that email address."
   else:                                   # Otherwise if email
address not found in mailing list
      if action=="unsubscribe":            # And if actions is unsubscribe
         if password==UseSplit[1]:         # If password is valid
            memMailList[counter]=''        # Empty internal mailing list entry
            print "Unsubscription for <B>"+email+"</B> successful" #
Print unsubscribe success to HTML
         else:                             # Otherwise if password not valid
            print "Unsubscription for <B>"+email+"</B> unsuccessful. 
Password is invalid" # Print unsubscribe unsuccessful to HTML
            print "Remember that passwords are case sensitive.  i.e.
password is not the same as PassWord"
      else:                                # Otherwise if subscribe
         print "Subscription for <B>"+email+"</B> already exists" #
Print subscription already exists to HTML
       
finally:                                   # Finally
   useMailList=open("maillist.txt", "w")   # Open Mailing List for writing
   useMailList.writelines(memMailList)     # Copy internal mailing list to file
   useMailList.close                       # Close mailing list
   print useFoot.read()                    # Print HTML footer
   useHead.close;                          # Close HTML header
   useFoot.close;                          # Close HTML footer
 
When I run it on my server I get a 500 error, and when I run it as a
normal python script (from windows) I get this error:

Traceback (most recent call last):
  File "C:\maillist.py", line 80, in ?
    useMailList.writelines(memMailList)     # Copy internal mailing list to file

NameError: name 'memMailList' is not defined

Whats wrong ??
Not only that memMailList is defined, its used a couple of times in
previous lines in the code...

BTW, the code was taken from:
http://www.programmedintegration.com/cgi-bin/pages.py?pmaillist

Thanks.
-- 
1. The day Microsoft makes something that doesn't suck is probably the
day they start making vacuum cleaners.
2. Unix is user friendly - it's just picky about it's friends.
3. Documentation is like sex: when it is good, it is very, very good.
And when it is bad, it is better than nothing. - Dick Brandon


More information about the Tutor mailing list