[英]Custom response to DATA with Twisted Python SMTP?
如何使用Twisted的SMTP指定自定義錯誤代碼/對DATA命令的響應? 在代碼中的eomReceived中,我想使用自定義字符串返回失敗。 現在,我可以返回帶有errback設置的Deferred(),它發送回550代碼,但是我無法指定響應字符串,並且在日志中收到未處理的錯誤。
class ConsoleMessageDelivery:
implements(smtp.IMessageDelivery)
def receivedHeader(self, helo, origin, recipients):
myHostname, clientIP = helo
headerValue = "by %s from %s with ESMTP ; %s" % (myHostname, clientIP, smtp.rfc822date())
# email.Header.Header used for automatic wrapping of long lines
return "Received: %s" % Header(headerValue)
def validateFrom(self, helo, origin):
# All addresses are accepted
return origin
def validateTo(self, user):
if user.dest.local == "console":
return lambda: ConsoleMessage()
raise smtp.SMTPBadRcpt(user)
class ConsoleMessage:
implements(smtp.IMessage)
def __init__(self):
self.lines = []
def lineReceived(self, line):
self.lines.append(line)
def eomReceived(self):
# do something here, return defer.succeed(None) on success
# right now on error, I do the following:
d = defer.Deferred()
d.errback("An error occurred")
return d
def connectionLost(self):
# There was an error, throw away the stored lines
self.lines = None
class ConsoleSMTPFactory(smtp.SMTPFactory):
protocol = smtp.ESMTP
def __init__(self, *a, **kw):
smtp.SMTPFactory.__init__(self, *a, **kw)
self.delivery = ConsoleMessageDelivery()
def buildProtocol(self, addr):
p = smtp.SMTPFactory.buildProtocol(self, addr)
p.delivery = self.delivery
return p
你需要:
SMTP基類的lineReceived會收到您的行,並將其傳遞給state_COMMAND(假定它處於命令狀態),該行將查找以do_開頭的方法以將其余的行傳遞給該方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.