[Tutor] file overwrite

Kirk D Bailey highprimate@howlermonkey.net
Wed, 2 Jan 02:41:52 2002 -0500


OK, I found 2 ways of turning a big number into a string, and It is a
working program- except:

It stores the entire subject line in the pending file. OK, but it
OVERWRITES previous entries, so there is always only one, the
lastone! Everything else works fine.


Here is the program listing:


ns# list TLcommander.py
Listing of file TLcommander.py in
directory:/www/www.howlermonkey.net/cgi-bin

#!/usr/local/bin/python
# 
import os, sys, string, cgi, random, time, smtplib      # We will use
some functions from these
msg=""                                          #initiate msg as a
null string.
domainname='howlermonkey.net'                   # this is our domain
name, edit to suit
pathtostuff='/www/www.howlermonkey.net/'        # this is the web dir
path2bin=pathtostuff+'cgi-bin/'                 # this is the web
cgi-bin 
path2lists=path2bin+'lists/'                    # this is where the
lists live
CRLF="\r\n"                                     # this is used a lot
in the forming of headers later.
#10
form = cgi.FieldStorage()
nowNUMBER = time.time()                         # thie returns a 9
tuple numberic expression
timestamp=hex(int(nowNUMBER))                   # turn that time
number into a string!
command = form["command"].value                 # of the time this
script runs.
to_addr = form["email"].value
listname = form["listname"].value
print 'Content-Type: text/html'+CRLF            # HTML is following
print '<title>TLwebmanager form input results</title>'
print '</head><body bgcolor="FFFFFF" text="000000"
link="0000FF"><blockquote><P>'
#20
print 
if not (form.has_key("email") and form.has_key("listname") and
form.has_key("command")):
        print "<P><center><H1>Error</H1></center><P>"
        print 'Something is wrong with how you filled out your form
data; I am confused.'
        print 'POSSIBLY there was a transmission error, or a typo?'
        print "Please ensure that you fill in all 3 data fields.(one
is a radio button)."
        print "Nonsense or fraudulent information will result in"
        print "your request being discarded by a robot, unread."
        print "click [BACK] and try again. Thank you."
        print '-Tiny.<P><br></body></html>'     #30
        sys.exit()      
else:
        print '<P><br><center><h2>Submitted request
information</h2></center>'
        print '<P><br><hr width=60% align="center"><P>'
        print "Declared EMail:", to_addr,'<br>'
        print "Requested LIST:", listname,'<br>'
        print "Issued Command:", command,'<p>'
        print '<hr width=60% align="center"><P><br>'
        print "A email is being sent to you with these pieces of
information in the Subject."
#40
        print "For most clients, simply replying to it is sufficent.
The body is ignored,"
        print "only the headers and subject line matter. The Letter
itself explains what"
        print "is happening, and what to do. IN breif, reply to the
message; just click"
        print "[REPLY] and then [SEND] (however your email client
names them)."
        print "<P>"
        print "If you change your mind before doing so, simply delete
the letter."
        print "<P><br>"
        print "Respectfully,"
        print "-Tiny the list robot.<P><br></body></html>"
#50
# keep that above line blank!
subject = command + " " + listname + " " + timestamp
f1=open('./lists/pending','w')          # open the pending file
f1.write(subject+"\n")                  # write the entire stubject
string into it
f1.close()                              # and close the file.
TinyList will read and compare it.
to_addr=form["email"].value
from_addr='nobody@'+domainname          # if the letter bounces, it
returns to this address;
msg=msg+"To: "+to_addr+CRLF             # make sure it is not oing to
waste your time with childish
#                                       # which will EAT bounces. OR,
route to the postmaster. Your call.
#60 Now build that outbound message.
msg=msg + "Subject: " + subject + CRLF
msg=msg + "X-Loop: postmaster@"+ domainname + CRLF
msg=msg + "From: "+ from_addr + CRLF
msg=msg + "Reply-To: TinyList@"+ domainname + CRLF+CRLF
msg=msg + """
To whom it may concern;
Someone (could be you) recently tried to """ + command + """ this
email address to
the email list:""" + listname + """. If you want this to happen,
simply reply
to this letter; do not alter the subject line. Don't worry about your
EMail program sticking a "Re:" in front of the subject material, I
know
how to handle that. My training is FAIL-SAFE; if a message is invalid
or
makes no sense, I simply throw it away and do nothing.

If you do NOT want this to happen, just delete this letter and it's
over. I
clean up dead pendings once a day when they are more than 7 days old,
so a
week from now all record of this will vanish.

IF there is any problem, please feel free to contact the postmaster:
mailto:postmaster@"""+domainname+"""?Subject=List%20Service%20Inquiry
should
work nicely.

Respectfully,
            -Tiny the list robot.

This is a service hosted at http://www."""+domainname+"""/ and we
welcome your
next visit.

"""
#83
#
# and do smtp work in this block.
#
#
#
#
server = smtplib.SMTP('localhost')              # you MIGHT want to
comment this out; Your Milage May Vary.
server.sendmail(from_addr, to_addr, msg)        # 400 send envlope
and msg!
server.quit()                                   # then close the
connection.
#93


end

In total confusion,
                   Kirk D Bailey

 +----------------------------------------------------+
 |   Providing Excellent email service for free!!     |
 |    Webmaster, Howlermonkey Email services Co.      |
 | highprimate@howlermonkey.net www.howlermonkey.net/ |
 +----------------------------------------------------+

ODD#1.8.01/kdb/sigme
VOTE! VOTE EARLY, VOTE OFTEN! PUNCH THE CARD ALL THE WAY THROUGH! LEAN ON IT! HAMMER THE THING HOME!
(A PUBLIC SERVICE ANNOUNCEMENT BROUGHT TO YOU IN SYMPATHY OF THE FLORIDA SUPERVISOR OF ELECTIONS OFFICE.)