Python 使用SMTPServer运行时,异步内核读取未实现错误

Python 使用SMTPServer运行时,异步内核读取未实现错误,python,smtp,asyncore,notimplementedexception,Python,Smtp,Asyncore,Notimplementedexception,我试图在本地计算机上模拟stmp网络流量。服务器脚本使用smtp和asyncore模块,客户端脚本使用smtplib和电子邮件模块。我的大部分代码都是从中复制的。但是,我得到一个错误,该错误表示asyncore.py read正在引发NotImplementedError异常 以下是我用于emailServer.py的代码: #!/usr/bin/python import smtpd import asyncore class CustomSMTPServer(smtpd.SMTPServe

我试图在本地计算机上模拟stmp网络流量。服务器脚本使用smtp和asyncore模块,客户端脚本使用smtplib和电子邮件模块。我的大部分代码都是从中复制的。但是,我得到一个错误,该错误表示asyncore.py read正在引发NotImplementedError异常

以下是我用于emailServer.py的代码:

#!/usr/bin/python
import smtpd
import asyncore

class CustomSMTPServer(smtpd.SMTPServer):

    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)
        return

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

asyncore.loop()
emailClient.py的代码:

#!/usr/bin/python

import smtplib
from email.mime.text import MIMEText
import email.utils

msg = MIMEText('This is the body of the message')
msg['To'] = email.utils.formataddr(('Recipient',    'recipient@example.com'))
msg['From'] = email.utils.formataddr(('Author', 'author@example.com'))
msg['Subject'] = 'Simple test message'

server = smtplib.SMTP('127.0.0.1', 1025)

server.set_debuglevel(True)

try:  
    server.sendmail('author@example.com' , ['recipient@example.com'],    msg.as_string())

except Exception as err:
    print(err)

finally:
   server.quit()
我在服务器端收到的错误:

error: uncaptured python exception, closing channel <smtpd.SMTPChannel    connected 127.0.0.1:51374 at 0x10bf0bb00> 
(<type 'exceptions.NotImplementedError'>: [/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/asyncore.py|read|83]
[/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/asyncore.py|handle_read_event|449] 

[/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/asynchat.py|handle_read|165] 

[/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtpd.py|found_terminator|181] 

[/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtpd.py|process_message|327])
错误:未捕获的python异常,正在关闭通道
(:[/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/asyncore.py | read | 83]
[/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/asyncore.py | handle | read | event | 449]
[/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/asynchat.py | handle|u read | 165]
[/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtpd.py | find|u terminator | 181]
[/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtpd.py | process|u message | 327])
客户端的调试和错误语句:

send: 'ehlo username.local\r\n'
reply: '502 Error: command "EHLO" not implemented\r\n'
reply: retcode (502); Msg: Error: command "EHLO" not implemented
send: 'helo username.local\r\n'
reply: '250 username.local\r\n'
reply: retcode (250); Msg: username.local
send: 'mail FROM:<author@example.com>\r\n'
reply: '250 Ok\r\n'
reply: retcode (250); Msg: Ok
send: 'rcpt TO:<recipient@example.com>\r\n'
reply: '250 Ok\r\n'
reply: retcode (250); Msg: Ok
send: 'data\r\n'
reply: '354 End data with <CR><LF>.<CR><LF>\r\n'
reply: retcode (354); Msg: End data with <CR><LF>.<CR><LF>
data: (354, 'End data with <CR><LF>.<CR><LF>')
send: 'Content-Type: text/plain; charset="us-ascii"\r\nMIME-Version:  1.0\r\nContent-Transfer-Encoding: 7bit\r\nTo: Recipient  <recipient@example.com>\r\nFrom: Author <author@example.com>\r\nSubject:     Simple test message\r\n\r\nThis is the body of the message\r\n.\r\n'
Connection unexpectedly closed
send: 'quit\r\n'
Traceback (most recent call last):
  File "emailClient.py", line 23, in <module>
    server.quit()
  File     "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtpl ib.py", line 767, in quit
    res = self.docmd("quit")
  File   "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtplib.py", line 393, in docmd
    self.putcmd(cmd, args)
  File    "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtplib.py", line 341, in putcmd
    self.send(str)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtplib.py", line 333, in send
    raise SMTPServerDisconnected('please run connect() first')
smtplib.SMTPServerDisconnected: please run connect() first
send:'ehlo username.local\r\n'
答复:“502错误:未执行命令“EHLO”\r\n”
答复:retcode(502);消息:错误:未执行命令“EHLO”
发送:“helo用户名。本地\r\n”
答复:“250用户名。本地\r\n”
答复:retcode(250);Msg:username.local
发送:'邮件发件人:\r\n'
答复:“250正常\r\n”
答复:retcode(250);味精:好的
发送:'rcpt至:\r\n'
答复:“250正常\r\n”
答复:retcode(250);味精:好的
发送:“数据\r\n”
答复:“354使用结束数据。\r\n”
答复:retcode(354);;Msg:使用结束数据。
数据:(354,“结束数据与”。)
发送:'内容类型:文本/普通;charset=“us ascii”\r\n时间版本:1.0\r\n内容传输编码:7bit\r\n收件人\r\n发件人\r\n对象:简单测试邮件\r\n\r\n这是邮件正文。\r\n
连接意外关闭
发送:“退出\r\n”
回溯(最近一次呼叫最后一次):
文件“emailClient.py”,第23行,在
server.quit()
文件“/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtpl ib.py”,退出中第767行
res=self.docmd(“退出”)
docmd中的文件“/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtplib.py”,第393行
self.putcmd(cmd,args)
putcmd中的文件“/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtplib.py”,第341行
self.send(str)
文件“/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtplib.py”,第333行,在send中
升起SMTPServerDisconnected('请先运行connect()')
smtplib.SMTPServerDisconnected:请先运行connect()

我相信相关的错误语句是“exceptions.NotImplementedError”>:[/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/asyncore.py | read | 83]来自客户端错误输出。SMTPServer类的文档说明SMTPServer自动绑定到asyncore。如何解决此错误?

用户在评论中提供了解决方案。我的函数有一个输入错误。进程消息应该是进程消息

我认为您的代码中有输入错误<代码>处理消息应全部用小写字母书写。出现此错误时,不会替换引发
NotImplementedError
的默认虚拟方法。谢谢!