Logging SMTP connections with Twisted(使用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
推荐答案
connectionMade是twisted.internet.interfaces.IProtocol的一部分,不是twisted.mail.smtp.IMessageDelivery的一部分。邮件服务器实现中没有任何代码关心消息传递实现上的connectionMade方法。
将每个连接逻辑放在工厂中更好。具体地说,解决这一问题的一个好方法是使用工厂包装器,将有关连接限制和日志记录的逻辑与有关服务SMTP连接的逻辑隔离。
Twisted附带了几个工厂包装。您可能特别感兴趣的一对是twisted.protocols.policies.LimitConnectionsByPeer和twisted.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连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用Twisted记录SMTP连接
基础教程推荐
- 求两个直方图的卷积 2022-01-01
- 使用大型矩阵时禁止 Pycharm 输出中的自动换行符 2022-01-01
- 包装空间模型 2022-01-01
- Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙? 2022-01-01
- 在同一图形上绘制Bokeh的烛台和音量条 2022-01-01
- PANDA VALUE_COUNTS包含GROUP BY之前的所有值 2022-01-01
- PermissionError: pip 从 8.1.1 升级到 8.1.2 2022-01-01
- 无法导入 Pytorch [WinError 126] 找不到指定的模块 2022-01-01
- 在Python中从Azure BLOB存储中读取文件 2022-01-01
- 修改列表中的数据帧不起作用 2022-01-01
