[Pymilter] Variables across call backs

Stuart D Gathman stuart at bmsi.com
Mon May 6 14:39:32 EDT 2013


On 05/06/2013 08:16 AM, C.S.R.C.Murthy expounded in part:
> I would like to know whether it is possible to define a variable in a 
> call back and access it in next call back?. How the defined variable 
> will be unique for different SMTP connections?
>
A new python object is created for each SMTP connection.  Like any other 
python class instance, you create instance variables by assigning 
them.   Here is an example from spfmilter.py (in the milter package):

class spfMilter(Milter.Base):
   "Milter to check SPF.  Each connection gets its own instance."

   def log(self,*msg):
     syslog.syslog('[%d] %s' % (self.id,' '.join([str(m) for m in msg])))
.....
   def hello(self,hostname):
     self.hello_name = hostname
     self.log("hello from %s" % hostname)
     if not self.internal_connection:
       # Allow illegal HELO from internal network, some email enabled 
copier/fax
       # type devices (Toshiba) have broken firmware.
       if ip4re.match(hostname):
         self.log("REJECT: numeric hello name:",hostname)
         self.setreply('550','5.7.1','hello name cannot be numeric ip')
         return Milter.REJECT
     return Milter.CONTINUE
------------------8<-----------------8<----------------------------------

Instance variable hello_name is available for all subsequent callbacks.

If you need to share between connection, then use a global variable,  
BUT be careful as each SMTP connection runs in its own thread!




More information about the Pymilter mailing list