0

我正在尝试在本地计算机上模拟 stmp 网络流量。我将 smtp 和 asyncore 模块用于服务器脚本,将 smtplib 和电子邮件模块用于客户端脚本。我的大部分代码都是从https://pymotw.com/2/smtpd/复制的。但是,我收到一个错误,指出 asyncore.py 读取正在引发 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])

客户端的调试和错误语句:

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

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

4

1 回答 1

0

用户VPfB在评论中提供了解决方案。我的函数有错字。process_Message 应该是 process_message

于 2017-06-01T17:19:36.270 回答