python 표준 라이브러리 학습 - 13.2smtpd

#python ,chap13

import smtpd
import asyncore

class CustomSMTPServer(smtpd.SMTPServer):
	def process_message(self,peer,mailfrom,rcpttos,data):
		print 'Receiving message from: ',peer
		print 'Message addressed from: ',mailfrom
		print 'Message addressed to  : ',rcpttos
		print 'Message length        : ',len(data)
		return

server=CustomSMTPServer(('localhost',9000),None)
asyncore.loop()
공식 문서에서 제시한 설명은 다음과 같다.
class smtpd.SMTPServer(localaddr, remoteaddr)
server=CustomSMTPServer(('localhost',9000),None)# 

python의 asyncore:https://docs.python.org/2/library/asyncore.html?highlight=asyncore#module-asyncore
이렇게 몇 줄만 있으면 smtp 서버 하나가 완성됩니다.
import smtplib
import email.utils
from email.mime.text import MIMEText

#Create the messate
msg=MIMEText('This is the body of the message.')
msg['To']=email.utils.formataddr(('Recipient','[email protected]'))
msg['From']=email.utils.formataddr(('Author','[email protected]'))
msg['Subject']='Simple test message'

server=smtplib.SMTP('localhost',9000)
server.set_debuglevel(True)
try:
	server.sendmail('[email protected]',
		['[email protected]'],
		msg.as_string())
finally:
	server.quit()

이것은 상기 smtp 서버를 호출하여 디버깅하는 코드입니다

좋은 웹페이지 즐겨찾기