使用Twisted Python SMTP定制对数据的响应?

Custom response to DATA with Twisted Python SMTP?(使用Twisted Python SMTP定制对数据的响应?)
本文介绍了使用Twisted Python SMTP定制对数据的响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用Twisted的SMTP为数据命令指定自定义错误代码/响应?在代码中的eomReceired中,我想用自定义字符串返回一个失败。现在,我可以返回一个带错误返回集的Defered(),它返回一个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

推荐答案

您需要:

  • 子类ESTMP
  • 在您的子类上实现do_data(参见twisted.mail.smtp.SMTP示例)

SMTP基类的lineRecept将接收您的行,并将其传递给STATE_COMMAND(假设它处于COMMAND状态),它将查找以do_开头的方法,将该行的其余部分传递给。

这篇关于使用Twisted Python SMTP定制对数据的响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Leetcode 234: Palindrome LinkedList(Leetcode 234:回文链接列表)
How do I read an Excel file directly from Dropbox#39;s API using pandas.read_excel()?(如何使用PANDAS.READ_EXCEL()直接从Dropbox的API读取Excel文件?)
subprocess.Popen tries to write to nonexistent pipe(子进程。打开尝试写入不存在的管道)
I want to realize Popen-code from Windows to Linux:(我想实现从Windows到Linux的POpen-code:)
Reading stdout from a subprocess in real time(实时读取子进程中的标准输出)
How to call type safely on a random file in Python?(如何在Python中安全地调用随机文件上的类型?)