如何使用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
答案 0 :(得分:0)
你需要:
SMTP基类的lineReceived将接收你的行,将其传递给state_COMMAND(假设它处于命令状态),这将查找以do_开头的方法,将其余部分传递给。