【问题标题】:Logging SMTP connections with Twisted使用 Twisted 记录 SMTP 连接
【发布时间】:2012-08-28 17:35:59
【问题描述】:

这里是 Python 新手。我正在使用 Twisted 和 twisted.mail.smtp 编写 SMTP 服务器。我想记录传入的连接,并在并发连接太多时可能会转储它们。基本上,我希望在建立新连接时调用 ConsoleMessageDelivery.connectionMade() 方法:

class ConsoleMessageDelivery:
    implements(smtp.IMessageDelivery)

    def connectionMade(self):
        # This never gets called

    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):
        return defer.succeed(None)

    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

【问题讨论】:

    标签: python twisted smtpd


    【解决方案1】:

    connectionMadetwisted.internet.interfaces.IProtocol 的一部分,而不是twisted.mail.smtp.IMessageDelivery 的一部分。邮件服务器实现中没有任何代码关心消息传递实现中的 connectionMade 方法。

    放置每个连接逻辑的更好地方是在工厂中。具体来说,解决此问题的一个好方法是使用工厂包装器,将有关连接限制和日志记录的逻辑与有关服务 SMTP 连接的逻辑隔离开来。

    Twisted 带有一些工厂包装器。您可能会特别感兴趣的一对是twisted.protocols.policies.LimitConnectionsByPeertwisted.protocols.policies.LimitTotalConnectionsFactory

    不幸的是,我不知道任何解释 twisted.protocols.policies 的文档。幸运的是,它并不太复杂。模块中的大多数工厂都包装了另一个任意工厂以添加一些行为。因此,例如,要使用 LimitConnectionsByPeer,您可以执行以下操作:

    from twisted.protocols.policies import LimitConnectionsByPeer
    ...
    factory = ConsoleSMTPFactory()
    wrapper = LimitConnectionsByPeer(ConsoleSMTPFactory(...))
    reactor.listenTCP(465, wrapper)
    

    这就是让LimitConnectionsByPeer 完成工作所需要的一切。

    编写自己的包装器只涉及一点点复杂性。首先,子类WrappingFactory。然后实现您有兴趣自定义的任何方法。在您的情况下,如果您想拒绝来自某个 IP 的连接,这将意味着覆盖 buildProtocol。然后,除非您还想自定义构建的协议(在这种情况下您不需要),否则调用基本实现并返回其结果。例如:

    from twisted.protocols.policies import WrappingFactory
    
    class DenyFactory(WrappingFactory):
        def buildProtocol(self, clientAddress):
            if clientAddress.host == '1.3.3.7':
                # Reject it
                return None
             # Accept everything else
             return WrappingFactory.buildProtocol(self, clientAddress)
    

    这些包装器堆叠在一起,因此您也可以将它们组合起来:

    from twisted.protocols.policies import LimitConnectionsByPeer
    ...
    factory = ConsoleSMTPFactory()
    wrapper = LimitConnectionsByPeer(DenyFactory(ConsoleSMTPFactory(...)))
    reactor.listenTCP(465, wrapper)
    

    【讨论】:

    • 非常感谢您的帮助。我想我现在明白了,但我看到了几个问题。一个是我得到了 LimitConnectionsByPeer 和 DenyFactory 示例的“exceptions.TypeError: 'IPv4Address' object does not support indexing”错误。我正在使用 Python 2.7;这仅与 Python 3 兼容吗?此外,LimitTotalConnectionsFactory() 似乎不带任何参数。
    • 糟糕。我活在过去。现在更新示例。
    • LimitTotalConnectionsFactory(当然与 LimitConnectionsByPeer 不同)由于某种原因不是 WrappingFactory,尽管它显然应该是。相反,它只是一个带有一些额外行为的 ServerFactory,因此很难将它与其他工厂结合起来。不过,您可以使用它和 LimitConnectionsByPeer 作为示例来指导您构建您自己的具有总连接限制行为的 WrappingFactory 子类。或许还可以向 Twisted 提出功能请求,要求提供基于 WrappingFactory 的 LimitTotalConnectionsFactory 版本。
    • 感谢您的帮助。不幸的是,我坚持最后一部分,我已经在link 上发布了关于资源清理的内容。任何帮助将不胜感激!
    猜你喜欢
    • 2023-03-27
    • 1970-01-01
    • 2011-01-30
    • 2018-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-09
    • 1970-01-01
    相关资源
    最近更新 更多