代码之家  ›  专栏  ›  技术社区  ›  Keith Palmer Jr.

将SMTP AUTH支持添加到Python smtpd库中。..无法覆盖该方法?

  •  3
  • Keith Palmer Jr.  · 技术社区  · 17 年前

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

            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)
    

     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 回复  |  直到 17 年前
        1
  •  3
  •   Alex Martelli    17 年前

    你需要扩展 SMTPChannel smtp_ SMTPServer 只需要返回您自己的通道子类。

        2
  •  1
  •   FlyingV    7 年前

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

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

    class FakeSMTPServer(smtpd.SMTPServer):
    
    """A Fake smtp server"""
            smtpd.SMTPChannel.smtp_HELP = smtp_HELP
    

    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