使用Twisted记录SMTP连接 [英] Logging SMTP connections with Twisted

查看:11
本文介绍了使用Twisted记录SMTP连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是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

推荐答案

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)

这篇关于使用Twisted记录SMTP连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆