简体   繁体   English

与SMTPServer一起运行时asyncore读取NotImplementedError

[英]asyncore read NotImplementedError when running with SMTPServer

I'm trying to simulate stmp network traffic on my local machine. 我正在尝试在本地计算机上模拟stmp网络流量。 I'm using the smtp and asyncore modules for the server script and smtplib and email modules for the client script. 我正在为服务器脚本使用smtp和asyncore模块,为客户端脚本使用smtplib和email模块。 Most of my code was copied from https://pymotw.com/2/smtpd/ . 我的大部分代码都是从https://pymotw.com/2/smtpd/复制的。 However, I'm getting an error that says that asyncore.py read is throwing a NotImplementedError exception 但是,我收到一条错误消息,指出asyncore.py读取引发NotImplementedError异常

Here is the code i'm using for emailServer.py: 这是我用于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()

Code for emailClient.py: 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()

The error I'm receiving on the server side: 我在服务器端收到的错误:

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

debugging and error statements for client: 客户端的调试和错误声明:

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

I believe the relevant error statements is 'exceptions.NotImplementedError'>: [/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/asyncore.py|read|83] from the client error output. 我相信相关的错误声明是'exceptions.NotImplementedError'>:[/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/asyncore.py|read|83]来自客户端错误输出。 The documentation for SMTPServer class says that the SMTPServer automatically binds to asyncore. SMTPServer类的文档说SMTPServer自动绑定到asyncore。 How do I resolve this error? 如何解决此错误?

User VPfB provided the solution in the comments. VPfB用户在评论中提供了解决方案。 my function had a typo. 我的功能有错字。 process_Message should be process_message process_Message应该是process_message

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM