[Tutor] Script performance: Filtering email

Sheila King sheila@thinkspot.net
Tue, 16 Jan 2001 02:29:17 -0800


On Tue, 16 Jan 2001 10:51:54 +0100, Remco Gerlich <scarblac@pino.selwerd.nl>
wrote about Re: [Tutor] Script performance: Filtering email:

:On Tue, Jan 16, 2001 at 01:18:19AM -0800, Sheila King wrote:
:> Strangely enough, these missing e-mails eventually appeared. Some of them,
:> over an hour from the time they hit my web host's mail servers until they hit
:> my mailbox. Some of them about a half hour. Some about seven minutes.
:
:Maybe the script failed with some exception, and qmail noticed and sent
:the mail again later?
:
:I'm quite sure this is not a performance issue, but there may be a bug
:somewhere.

At this point, it seems likely that there must be a problem or bug in the
script.

:> Code Sample:
:> 
:>  #! /big/dom/xdomain/Python-2.0/python
:> 
:>  import rfc822, sys, string, smtplib
:> 
:>  ##check if to: and cc: addresses are in my approved file
:>  def goodmessage(addresseeslist, rcptlist):
:>       for addr in addresseeslist:
:>            for rcpt in rcptlist:
:>                 if (upper(addr[1]) == upper(rcpt)):
:>                      return 1
:>       return 0
:
:Are you sure you meant addr[1] there? If addresseeslist is simply a name
:of mail addresses, you are comparing the second character of addr to
:the whole recipient. I don't know what rfc822 does though, maybe addr is
:something else.

addresseeslist is a list of tuples, where the first element in the tuple is
the users name (from the address field of an e-mail), such as mine should
appear to be "Sheila King" in the From: field of this message, and the second
element of the tuple is the actual e-mail address, which in this message From:
field should appear to be sheila@thinkspot.net

addr in addresseeslist would be one of those tuples out of the whole list.
addr[1] would be the second element in the tuple, which is the e-mail address.

:This could be written:
:
:def goodmessage(addresseeslist, rcptlist):
:   for rcpt in rcptlist:
:      if rcpt in addresseeslist:
:          return 1
:   return 0
:   
:(if addr[1] was an error)

Hmm. Maybe I should try to just cull the addresses from the addressees list of
tuples, instead of having a nested loop. I could just us the "if blank1 in
blank2" structure if I get the data types to match properly. That might be
more efficient?

...<much script snippage>...
:> 
:>  server=smtplib.SMTP("mysmtp.com")
:> 
:>  if goodmessage(addressees, goodrcptlist):
:>       server.sendmail(sender, fwdgood, mssg)
:>  else:
:>       server.sendmail(sender, fwdbad, mssg)
:>  server.quit()
:
:If the connection fails for some reason or other, an exception will
:be raised.
:
:Maybe you should wrap the whole thing in a try/except to log whatever
:goes wrong, like
:
:try:
:   (the body of your script)
:except:
:   import sys
:   f=open("somelogfile","w")
:   print >> f, sys.exc_info()
:   f.close()
:   
:   raise # Re-raise the exception so that the program doesn't quit silently
:   
:If that log file fills up, you know where to look. I can't really
:say more from here.

I've never worked with exceptions before. Is it really that easy? I will try
it out tomorrow and see what I get in the log file. Surely something.

Is the additional "import sys" command inside the "except" block necessary if
I've already imported that module at the beginning of my script?

Thanks,

--
Sheila King
http://www.thinkspot.net/sheila/
http://www.k12groups.org/