Python新手在这里。我正在使用asyncore和smtpd来编写电子邮件服务器。一切似乎都有效,除了我无法弄清楚如何检测连接客户端何时/是否关闭套接字。此外,我似乎无法设置超时以在不活动后自动关闭连接。这是一些示例代码。从不调用CustomSTMPServer.handle_close。而且我不知道如何设置有效的超时。任何帮助将不胜感激。
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()