forked from gregoriorobles/ptavi-p4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
107 lines (89 loc) · 3.27 KB
/
server.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
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
Clase (y programa principal) para un servidor de eco en UDP simple
"""
import socketserver
import sys
import json
import os.path
from datetime import datetime, timedelta
class SIPRegisterHandler(socketserver.DatagramRequestHandler):
client_dicc = {}
def register2json(self):
"""
Creates json file
"""
with open('registered.json', 'w') as json_file:
json.dump(self.client_dicc, json_file)
def json2registered(self):
"""
Checks if there's a json file,
if not, it creates it
"""
if os.path.exists('registered.json'):
with open('registered.json', 'w') as json_file:
json.dump(self.client_dicc, json_file)
else:
self.register2json()
def timeout(self):
"""
Searchs for expired users
"""
user_list = []
for user in self.client_dicc:
expiration = datetime.strptime(
self.client_dicc[user][1][1], '%Y-%m-%d %H:%M:%S')
actual_time = datetime.now()
if actual_time >= expiration:
user_list.append(user)
for user in user_list:
del self.client_dicc[user]
def handle(self):
"""
handle method of the server class
(all requests will be handled by this method)
"""
self.wfile.write(b"Hemos recibido tu peticion \n")
for line in self.rfile:
ip = self.client_address[0]
if line.decode('utf-8') == '\r\n':
continue
else:
request = line.decode('utf-8').split(" ")
if request[0] == 'REGISTER':
email = request[1][request[1].find(':')+1:]
elif request[0] == 'Expires:':
if int(request[1]) == 0 and email != "":
try:
print("\n" + "Recibida petición de borrado")
del self.client_dicc[email]
self.wfile.write(b'SIP/2.0 200 OK' + b'\r\n\r\n')
except KeyError:
print("Este usuario no existe" + "\n")
elif email != "":
print("\n" + "Recibida petición de registro")
seconds = timedelta(seconds=int(request[1]))
expiration = datetime.now() + seconds
self.client_dicc[email] = [
("address:", ip),
("expires:", str(expiration)[:-7])]
self.wfile.write(b'SIP/2.0 200 OK' + b'\r\n\r\n')
elif request[0] != 'REGISTER' and request[0] != 'Expires':
email = ""
print("\n" + "Recibida petición inválida")
self.wfile.write(b'Peticion invalida' + b'\r\n\r\n')
self.timeout()
self.json2registered()
print(self.client_dicc)
if __name__ == "__main__":
if len(sys.argv) != 2:
print("Usage: server.py port")
sys.exit()
port = int(sys.argv[1])
serv = socketserver.UDPServer(('', port), SIPRegisterHandler)
print("Lanzando servidor UDP...")
try:
serv.serve_forever()
except KeyboardInterrupt:
print("Finalizado servidor")