Commit f55c351f1119c1cad2c88743beb77fde3a372c02

  • avatar
  • Giovanni Misitano <gialmisi @stu…nt.jyu.fi> (Committer)
  • Sat Jan 12 02:37:05 EET 2019
  • avatar
  • Giovanni Misitano <gialmisi @stu…nt.jyu.fi> (Author)
  • Sat Jan 12 02:37:05 EET 2019
Added threading to server.
mail/smtp.py
(112 / 79)
  
44
55import socket
66import re
7import threading
78
8s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
9s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
9def pop3():
10 print("POP3 running")
1011
11port = 1024
12host = '127.0.0.1'
12 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
13 s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
1314
14s.bind((host, port))
15s.listen(1)
15 port = 1025
16 host = '127.0.0.1'
1617
17connection, address = s.accept()
18print("Connection address {}".format(address))
18 s.bind((host, port))
19 s.listen(1)
1920
20greet = bytes("220 {} Mail Transfer Service Ready\r\n".format(host), "utf-8")
21connection.send(greet)
22state = "standby"
23mail = ""
21 connection, address = s.accept()
22 print("Connection address {}".format(address))
2423
25known_users = ["test@test.com"]
24 s.close()
2625
27while True:
28 data = connection.recv(64)
29 print("Got data {}".format(data.strip()))
30 data_s = str(data.strip(), 'UTF8')
31 reply = data
3226
33 print(data_s)
27def smtp():
28 print("SMTP running")
3429
35 if not data_s:
36 reply = b'500 Syntax error, command unrecognized\r\n'
30 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
31 s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
3732
38 elif data_s.upper() == "QUIT":
39 print("QUITTING")
40 reply = bytes("221 {} Service closing transmission channel\r\n".format(host), "utf-8")
41 state = None
33 port = 1024
34 host = '127.0.0.1'
4235
43 elif state == "standby":
44 mailto = []
45 mailfrom = []
46 data_s = data_s.split()
47 if data_s[0].upper() == "HELO":
48 if len(data_s) < 2:
49 reply = b'501 Syntax error in parameters or arguments.\r\n'
50 else:
51 reply = bytes("250 {}\r\n".format(host), 'utf-8')
52 state = "listen"
53 else:
54 reply = bytes("502 Command not implemented\r\n", 'utf-8')
36 s.bind((host, port))
37 s.listen(1)
5538
56 elif state == "listen":
57 data_s = data_s.split()
58 if data_s[0].upper() == "MAIL":
59 if len(data_s) < 2:
60 reply = b'501 Syntax error in parameters or arguments.\r\n'
61 else:
62 m = re.search(r'FROM:<(.*)>', data_s[1], re.IGNORECASE)
63 if m is None:
64 reply = b'501 Syntax error in parameters of arguments.\r\n'
65 else:
66 mailfrom = m.group(1)
67 reply = b'250 OK\r\n'
39 connection, address = s.accept()
40 print("Connection address {}".format(address))
6841
69 elif data_s[0].upper() == "RCPT":
70 if len(data_s) < 2:
71 reply = b'501 Syntax error in parameters or arguments.\r\n'
42 greet = bytes("220 {} Mail Transfer Service Ready\r\n".format(host), "utf-8")
43 connection.send(greet)
44 state = "standby"
45 mail = ""
46
47 known_users = ["test@test.com"]
48
49 while True:
50 data = connection.recv(64)
51 print("Got data {}".format(data.strip()))
52 data_s = str(data.strip(), 'UTF8')
53 reply = data
54
55 print(data_s)
56
57 if not data_s:
58 reply = b'500 Syntax error, command unrecognized\r\n'
59
60 elif data_s.upper() == "QUIT":
61 print("QUITTING")
62 reply = bytes("221 {} Service closing transmission channel\r\n".format(host), "utf-8")
63 state = None
64
65 elif state == "standby":
66 mailto = []
67 mailfrom = []
68 data_s = data_s.split()
69 if data_s[0].upper() == "HELO":
70 if len(data_s) < 2:
71 reply = b'501 Syntax error in parameters or arguments.\r\n'
72 else:
73 reply = bytes("250 {}\r\n".format(host), 'utf-8')
74 state = "listen"
7275 else:
73 m = re.search(r'TO:<(.*)>', data_s[1], re.IGNORECASE)
74 if m is None:
75 reply = b'501 Syntax error in parameters of arguments.\r\n'
76 reply = bytes("502 Command not implemented\r\n", 'utf-8')
77
78 elif state == "listen":
79 data_s = data_s.split()
80 if data_s[0].upper() == "MAIL":
81 if len(data_s) < 2:
82 reply = b'501 Syntax error in parameters or arguments.\r\n'
7683 else:
77 if m.group(1) in known_users:
78 mailto.append(m.group(1))
84 m = re.search(r'FROM:<(.*)>', data_s[1], re.IGNORECASE)
85 if m is None:
86 reply = b'501 Syntax error in parameters of arguments.\r\n'
87 else:
88 mailfrom = m.group(1)
7989 reply = b'250 OK\r\n'
90
91 elif data_s[0].upper() == "RCPT":
92 if len(data_s) < 2:
93 reply = b'501 Syntax error in parameters or arguments.\r\n'
94 else:
95 m = re.search(r'TO:<(.*)>', data_s[1], re.IGNORECASE)
96 if m is None:
97 reply = b'501 Syntax error in parameters of arguments.\r\n'
8098 else:
81 reply = b'550 No such user here\r\n'
99 if m.group(1) in known_users:
100 mailto.append(m.group(1))
101 reply = b'250 OK\r\n'
102 else:
103 reply = b'550 No such user here\r\n'
82104
83 elif data_s[0].upper() == "DATA":
84 if len(data_s) != 1:
85 reply = b'501 Syntax error in parameters or arguments.\r\n'
105 elif data_s[0].upper() == "DATA":
106 if len(data_s) != 1:
107 reply = b'501 Syntax error in parameters or arguments.\r\n'
108 else:
109 mail = ""
110 reply = b'354 Start mail input; end with <CRLF>.<CRLF>\r\n'
111 state = "data"
112
86113 else:
87 mail = ""
88 reply = b'354 Start mail input; end with <CRLF>.<CRLF>\r\n'
89 state = "data"
114 reply = b'500 Syntax error, command unrecognized\r\n'
90115
91 else:
92 reply = b'500 Syntax error, command unrecognized\r\n'
116 elif state == "data":
117 if data_s[0] == '.':
118 reply = b'250 OK\r\n'
119 state = "standby"
120 else:
121 for token in data_s:
122 mail += token
93123
94 elif state == "data":
95 if data_s[0] == '.':
96 reply = b'250 OK\r\n'
97 state = "standby"
98 else:
99 for token in data_s:
100 mail += token
124 reply = None
101125
102 reply = None
126 if reply is not None:
127 connection.send(reply)
103128
104 if reply is not None:
105 connection.send(reply)
129 if state == None:
130 break
106131
107 if state == None:
108 break
132 connection.close()
109133
110connection.close()
134
135if __name__ == "__main__":
136 smtp_thread = threading.Thread(target=smtp)
137 pop3_thread = threading.Thread(target=pop3)
138
139 smtp_thread.start()
140 pop3_thread.start()
141
142 smtp_thread.join()
143 pop3_thread.join()