Two ways to implement an interface class
from Import Abcmeta from Import Abstractmethod class Basemessage (metaclass=abcmeta): @abstractmethod def Send (Self,subject,body,to, Name): Pass
Way Two
class Basemessage (object): def Send (self, subject, body, to, name): Raise Notimplementederror (' not implemented Send method ')
Email alerts
ImportSmtplib fromEmail.mime.textImportMimetext fromEmail.utilsImportformataddr from. baseImportBasemessageclassEmail (basemessage):def __init__(self): Self.email="Sender's email address"Self.user="Sender's name"self.pwd=' mailbox password or SMTP authorization code' defSend (self,subject,body,to,name):" ":p Aram Subject: Subject:p Aram Body: Content:p Aram to: Recipient:p Aram Name: Recipient's name: return: " "msg= Mimetext (Body,'Plain','Utf-8')#Send contentmsg[' from'] = FORMATADDR ([Self.user,self.email])#Sendermsg[' to'] = FORMATADDR ([name, to])#Recipientmsg['Subject'] = subject#ThemeServer= Smtplib. SMTP ("smtp.126.com", 25)#SMTP ServiceServer.login (Self.email, Self.pwd)#mailbox user name and passwordServer.sendmail (Self.email, [to,], msg.as_string ())#Sender and receiverServer.quit ()
Dynamic Import Module + reflection
Settings
# ################## Configuration of Custom message reminders ################### message_classes = [ 'utils.message.email.Email', ' UTILS.MESSAGE.MSG.MSG ' , ' Utils.message.wx.WeChat ' , ' utils.message.dingding.DingDing ' ,]
Importimportlib fromDjango.confImportSettingsdefsend_message (to,name,subject,body):"""SMS, email,:p Aram to: Recipient:p Aram Name: Recipient name:p Aram Subject: Subject:p Aram Body: Content: return:""" forCls_pathinchsettings. Message_classes:#Cls_path is a stringModule_path,class_name = Cls_path.rsplit ('.', Maxsplit=1) M=importlib.import_module (module_path) obj=GetAttr (M,class_name) () obj.send (Subject,body,to,name,)
Python implements the interface class in two ways + email reminder + Dynamic Import Module + reflection (refer to Django middleware source code)