forked from adamlaska/datatracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_smtpserver.py
More file actions
92 lines (72 loc) · 2.73 KB
/
test_smtpserver.py
File metadata and controls
92 lines (72 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# Copyright The IETF Trust 2014-2020, All Rights Reserved
# -*- coding: utf-8 -*-
import smtpd
import threading
import asyncore
import debug # pyflakes:ignore
class AsyncCoreLoopThread(object):
def wrap_loop(self, exit_condition, timeout=1.0, use_poll=False, map=None):
if map is None:
map = asyncore.socket_map
while map and not exit_condition:
asyncore.loop(timeout=1.0, use_poll=False, map=map, count=1)
def start(self):
"""Start the listening service"""
self.exit_condition = []
kwargs={'exit_condition':self.exit_condition,'timeout':1.0}
self.thread = threading.Thread(target=self.wrap_loop, kwargs=kwargs)
self.thread.daemon = True
self.thread.daemon = True
self.thread.start()
def stop(self):
"""Stop the listening service"""
self.exit_condition.append(True)
self.thread.join()
class SMTPTestChannel(smtpd.SMTPChannel):
# mail_options = ['BODY=8BITMIME', 'SMTPUTF8']
def smtp_RCPT(self, arg):
if not self.mailfrom:
self.push(str('503 Error: need MAIL command'))
return
arg = self._strip_command_keyword('TO:', arg)
address, __ = self._getaddr(arg)
if not address:
self.push(str('501 Syntax: RCPT TO: <address>'))
return
if "poison" in address:
self.push(str('550 Error: Not touching that'))
return
self.rcpt_options = []
self.rcpttos.append(address)
self.push(str('250 Ok'))
class SMTPTestServer(smtpd.SMTPServer):
def __init__(self,localaddr,remoteaddr,inbox):
if inbox is not None:
self.inbox=inbox
else:
self.inbox = []
smtpd.SMTPServer.__init__(self,localaddr,remoteaddr)
def handle_accept(self):
pair = self.accept()
if pair is not None:
conn, addr = pair
#channel = SMTPTestChannel(self, conn, addr)
SMTPTestChannel(self, conn, addr)
def process_message(self, peer, mailfrom, rcpttos, data, mail_options=[], rcpt_options=[]):
self.inbox.append(data)
class SMTPTestServerDriver(object):
def __init__(self, localaddr, remoteaddr, inbox=None):
self.localaddr=localaddr
self.remoteaddr=remoteaddr
if inbox is not None:
self.inbox = inbox
else:
self.inbox = []
self.thread_driver = None
def start(self):
self.smtpserver = SMTPTestServer(self.localaddr,self.remoteaddr,self.inbox)
self.thread_driver = AsyncCoreLoopThread()
self.thread_driver.start()
def stop(self):
if self.thread_driver:
self.thread_driver.stop()