添加SMTP AUTH支持到Python smtpd库...不能覆盖该方法?

时间:2009-07-16 15:27:56

标签: python smtp smtpd smtp-auth

所以,我想扩展Python smtpd SMTPServer类,以便它可以处理SMTP AUTH连接。看似简单......

所以,看起来我可以这样开始:

def smtp_EHLO(self, arg):
    print 'got in arg: ', arg
    # do stuff here...

但出于某种原因,永远不会被召唤。 Python smtpd库调用其他类似的方法:

        method = None
        i = line.find(' ')
        if i < 0:
            command = line.upper()
            arg = None
        else:
            command = line[:i].upper()
            arg = line[i+1:].strip()
        method = getattr(self, 'smtp_' + command, None)

为什么不调用我的方法?

之后,我认为我可能只是覆盖整个found_terminator(self):方法,但这似乎也不起作用。

 def found_terminator(self):
     # I add this to my child class and it never gets called... 

我做了些蠢事还是......?也许我今天还没有完全醒来......

import smtpd
import asyncore

class CustomSMTPServer(smtpd.SMTPServer):

    def smtp_EHLO(self, arg):

        print 'got in arg: ', arg

    def process_message(self, peer, mailfrom, rcpttos, data):
        print 'Receiving message from:', peer
        print 'Message addressed from:', mailfrom
        print 'Message addressed to  :', rcpttos
        print 'Message length        :', len(data)
        print 'HERE WE ARE MAN!'
        return

    # Implementation of base class abstract method
    def found_terminator(self):
        print 'THIS GOT CALLED RIGHT HERE!'

        line = EMPTYSTRING.join(self.__line)
        print >> DEBUGSTREAM, 'Data:', repr(line)
        self.__line = []
        if self.__state == self.COMMAND:
            if not line:
                self.push('500 Error: bad syntax')
                return
            method = None
            i = line.find(' ')
            if i < 0:
                command = line.upper()
                arg = None
            else:
                command = line[:i].upper()
                arg = line[i+1:].strip()
            method = getattr(self, 'smtp_' + command, None)

            print 'looking for: ', command
            print 'method is: ', method

            if not method:
                self.push('502 Error: command "%s" not implemented' % command)
                return
            method(arg)
            return
        else:
            if self.__state != self.DATA:
                self.push('451 Internal confusion')
                return
            # Remove extraneous carriage returns and de-transparency according
            # to RFC 821, Section 4.5.2.
            data = []
            for text in line.split('\r\n'):
                if text and text[0] == '.':
                    data.append(text[1:])
                else:
                    data.append(text)
            self.__data = NEWLINE.join(data)
            status = self.__server.process_message(self.__peer,
                                                   self.__mailfrom,
                                                   self.__rcpttos,
                                                   self.__data)
            self.__rcpttos = []
            self.__mailfrom = None
            self.__state = self.COMMAND
            self.set_terminator('\r\n')
            if not status:
                self.push('250 Ok')
            else:
                self.push(status)

server = CustomSMTPServer(('127.0.0.1', 1025), None)

asyncore.loop()

2 个答案:

答案 0 :(得分:3)

你需要扩展SMTPChannel - 这就是实现smtp_动词方法的地方;你SMTPServer的扩展只需要返回你自己的频道子类。

答案 1 :(得分:0)

TL&DR:要将其他功能添加到SMTPChannel,您只需要声明一个功能,然后将其直接添加到smtpd.SMTPChannel

说明:

SMTPChannel类旨在响应用户在开放端口(通常是端口25)上输入的命令。它搜索“可以回答哪些命令”的方式基于“自省”,它会检查功能的所有可用属性。

请注意,SMTPChannel中的功能需要以“ smtp_”开头。例如,如果要响应帮助,则可以创建smtpd.SMTPChannel.smtp_HELP。

下面的函数来自详细说明内省的源代码

class SMTPChannel(asynchat.async_chat):
  method = getattr(self, 'smtp_' + command, None)

CodeThatWorks

第1步:声明一个称为

的功能
def smtp_HELP(self,arg):
  self.push("[8675309] GPT Answers to HELP")

第2步:将以下功能添加到smtpd.SMTPChannel

class FakeSMTPServer(smtpd.SMTPServer):

"""A Fake smtp server"""
        smtpd.SMTPChannel.smtp_HELP = smtp_HELP

第3步:Telnet到localhost 25并进行测试

Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
220 vics-imac.fios-router.home ESMTP Sendmail 6.7.4 Sunday 17 March 2019
HELP
[8675309] GPT Answers to HELP