3

Python newbie here. I'm using asyncore and smtpd to write an email server. Everything seems to work except that I can't figure out how to detect when/if the connecting client closes the socket. Also, I can't seem to set a timeout to automatically close connections after inactivity. Here's some example code. CustomSTMPServer.handle_close is never called. And I don't know how to set a timeout that works. Any help would be greatly appreciated.

import smtpd
import asyncore

class CustomSMTPServer(smtpd.SMTPServer):

    def handle_accept(self):
        pair = self.accept()
        if pair is None:
            pass
        conn, addr = pair
        print 'Incoming connection from %s' % addr[0]
        channel = smtpd.SMTPChannel(self, conn, addr)

    def handle_close(self):
        print 'Received close'

    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()
Mark Fletcher
  • 701
  • 1
  • 14
  • 36

0 Answers0