-
Notifications
You must be signed in to change notification settings - Fork 1
/
fakesmtp.py
94 lines (83 loc) · 2.88 KB
/
fakesmtp.py
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
92
93
94
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = "Vadim Bobrenok"
__email__ = "[email protected]"
__version__ = 1.0
import smtpd
import asyncore
import os, sys
import time
import shutil
class FakeSMTPServer(smtpd.SMTPServer):
def __init__(self, save, *args,**kwargs):
smtpd.SMTPServer.__init__(self, *args,**kwargs)
self.save = save
self.out = StdoutWriter()
self.count = 0
self.start_time = 0.0
self.rate = 0.0
self.out.update(0,0,'00:00:00')
def process_message(self, peer, mailfrom, rcpttos, data):
if self.start_time == 0:
self.start_time = time.time()
self.count += 1
try:
self.rate = float(self.count)/(time.time()-self.start_time)
except ZeroDivisionError:
pass
self.out.update(self.count, self.rate, time.strftime('%H:%M:%S'))
if self.save:
with open(r'mail\mail_%s.txt'%self.count,'w') as f:
f.write(''.join([
'Time: ', time.ctime(), '\n',
'IP: ', peer.__str__(), '\n',
'From: ', mailfrom, '\n',
'To: ', rcpttos.__str__(), '\n',
'Body: ', data, '\n'
]))
return
class StdoutWriter(object):
def __init__(self):
self.last_length = 0
def update(self, count, rate, con_time):
self.__show('\b'*self.last_length)
text = "Count: %4s Rate: %6.2f Last at: %8s"%(count, rate, con_time)
self.last_length = len(text)
self.__show(text)
def __show(self, text):
sys.stdout.write(text)
sys.stdout.flush()
if __name__=='__main__':
if os.sys.path[0].endswith('fakesmtp.exe'):
os.chdir(os.sys.path[0].rstrip('fakesmtp.exe'))
else:
os.chdir(os.sys.path[0])
shutil.rmtree('mail', True)
host = '127.0.0.1'
port = 25
save = True
for i, arg in enumerate(sys.argv[1:]):
if i == 0:
host = arg
if i == 1:
port = int(arg)
if i == 2:
if arg in ['false','False','f','0','off','no']:
save = False
if save:
try:
os.mkdir('mail')
except OSError, IOError:
if not os.path.exists('mail'):
time.sleep(2)
try:
os.mkdir('mail')
except OSError, IOError:
print 'Unable to create email storage directory.'
sys.exit(0)
server = FakeSMTPServer(save, (host, port), None)
try:
asyncore.loop()
except KeyboardInterrupt:
print "\nShutting down fake SMTP Server..."
sys.exit(0)