gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 01:44:13 25 4
gpt4 key购买 nike

所以,我想扩展 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()

最佳答案

您需要扩展 SMTPChannel -- 这是 smtp_verb 方法实现的地方; SMTPServer 的扩展只需要返回您自己的 channel 子类。

关于python - 将 SMTP AUTH 支持添加到 Python smtpd 库...无法覆盖该方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1138425/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com