天天看點

html轉bvl轉換器,為什麼HTML轉換python輸出'<'=&lt,'>'=&gt?

importos,smtplibfromemailimportencodersfromemail.mime.audioimportMIMEAudiofromemail.mime.baseimportMIMEBasefromemail.mime.imageimportMIMEImagefromemail.mime.multipartimportMIMEMultipartfromemail.mime.textimportMIMETextimportmimetypesclassEmail:__config={}def__init__(self,smtp_server,sender,recipients,cc,subject,body,body_type,attachments=None):self.__config={'smtp_server':smtp_server,'sender':sender,'recipients':recipients,'cc':cc,'subject':subject,'body':body,'body_type':body_type,#plain|html'attachments':attachments#list of files}defgetSmtpServer(self):returnself.__config.get('smtp_server')defgetSender(self):returnself.__config.get('sender')defgetRecipients(self):returnself.__config.get('recipients')defgetCc(self):returnself.__config.get('cc')defgetSubject(self):returnself.__config.get('subject')defgetBody(self):returnself.__config.get('body')defgetBodyType(self):returnself.__config.get('body_type')defgetAttachments(self):returnself.__config.get('attachments')defsetSmtpServer(self,host):self.__config['smtp_server']=smtp_serverreturnselfdefsetSender(self,sender):self.__config['sender']=senderreturnselfdefsetRecipients(self,recipients):self.__config['recipients']=recipientsreturnselfdefsetCc(self,cc):self.__config['cc']=ccreturnselfdefsetSubject(self,subject):self.__config['subject']=subjectreturnselfdefsetBody(self,body):self.__config['body']=bodyreturnselfMIMEMultipartdefsetBodyType(self,body_type):self.__config['body_type']=body_typereturnselfdefsetAttachments(self,attachments):self.__config['attachments']=attachmentsreturnselfdefattachFilesToEmail(self,attachments,msg):ifNone==attachments:tmpmsg=msg

msg=MIMEMultipart()msg.attach(tmpmsg)ifNone!=attachments:forfnameinattachments:ifnotos.path.exists(fname):print"File '%s' does not exist. Not attaching to email."%fnamecontinueifnotos.path.isfile(fname):print"Attachment '%s' is not a file. Not attaching to email."%fnamecontinue# Guess at encoding typectype,encoding=mimetypes.guess_type(fname)ifctypeisNoneorencodingisnotNone:# No guess could be made so use a binary type.ctype='application/octet-stream'maintype,subtype=ctype.split('/',1)ifmaintype=='text':fp=open(fname)attach=MIMEText(fp.read(),_subtype=subtype)fp.close()elifmaintype=='image':fp=open(fname,'rb')attach=MIMEImage(fp.read(),_subtype=subtype)fp.close()elifmaintype=='audio':fp=open(fname,'rb')attach=MIMEAudio(fp.read(),_subtype=subtype)fp.close()else:fp=open(fname,'rb')attach=MIMEBase(maintype,subtype)attach.set_payload(fp.read())fp.close()# Encode the payload using Base64encoders.encode_base64(attach)# Set the filename parameterfilename=os.path.basename(fname)attach.add_header('Content-Disposition','attachment',filename=filename)msg.attach(attach)defsend(self):# Create message container - the correct MIME type is multipart/alternative.msg=MIMEMultipart('alternative')msg['Subject']=self.getSubject()msg['From']=self.getSender()msg['To']=self.getRecipients()msg['CC']=self.getCc()# Record the MIME types of both parts - text/plain and text/html.#part1 = MIMEText(text, 'plain')#part2 = MIMEText(html, 'html')part=MIMEText(self.getBody(),self.getBodyType())# Attach parts into message container.# According to RFC 2046, the last part of a multipart message, in this case# the HTML message, is best and preferred.msg.attach(part)# Add attachments, if anyself.attachFilesToEmail(self.getAttachments(),msg)# Send the message via local SMTP server.s=smtplib.SMTP(self.getSmtpServer())# sendmail function takes 3 arguments: sender's address, recipient's address# and message to send - here it is sent as one string.s.sendmail(self.getSender(),(self.getRecipients()+self.getCc()).split(","),msg.as_string())s.quit()