asyncore read NotImplementedError when running with SMTPServer

761 Views Asked by At

I'm trying to simulate stmp network traffic on my local machine. I'm using the smtp and asyncore modules for the server script and smtplib and email modules for the client script. Most of my code was copied from https://pymotw.com/2/smtpd/. However, I'm getting an error that says that asyncore.py read is throwing a NotImplementedError exception

Here is the code i'm using for 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:

#!/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',    '[email protected]'))
msg['From'] = email.utils.formataddr(('Author', '[email protected]'))
msg['Subject'] = 'Simple test message'

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

server.set_debuglevel(True)

try:  
    server.sendmail('[email protected]' , ['[email protected]'],    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:<[email protected]>\r\n'
reply: '250 Ok\r\n'
reply: retcode (250); Msg: Ok
send: 'rcpt TO:<[email protected]>\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  <[email protected]>\r\nFrom: Author <[email protected]>\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. The documentation for SMTPServer class says that the SMTPServer automatically binds to asyncore. How do I resolve this error?

1

There are 1 best solutions below

0
On

User VPfB provided the solution in the comments. my function had a typo. process_Message should be process_message