我试图模拟本地计算机上的网络流量。我使用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
我认为相关的错误陈述是例外。未完成错误&#39;&gt;:[/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/asyncore.py|read来自客户端错误输出。 SMTPServer类的文档说SMTPServer自动绑定到asyncore。如何解决此错误?