-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
57 lines (49 loc) · 1.85 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
import sys
from socket import socket, AF_INET, SOCK_DGRAM
def run_server(my_port, parent_ip, parent_port, addresses_file_name):
with open(addresses_file_name, "r") as f:
lines = f.read().split("\n")
# Load to dictionary
website_ips = {}
for line in lines:
address, ip = line.strip().split(',')
website_ips[address] = ip
# Open socket
s = socket(AF_INET, SOCK_DGRAM)
s.bind(('0.0.0.0', my_port))
# Set parent info
if parent_ip == "-1" or parent_port == -1:
parent_info = None
else:
parent_info = (parent_ip, parent_port)
# Listen to requests
while True:
data, sender_info = s.recvfrom(2048)
print("Message: ", data, " from: ", sender_info)
if data.decode('utf-8') in website_ips:
# If found in the dictionary
s.sendto(website_ips[data.decode('utf-8')].encode('utf-8'), sender_info)
elif parent_info is None:
# If parent
s.sendto('Unknown', sender_info)
else:
# Ask parent
s.sendto(data, parent_info)
address_ip, parent_info = s.recvfrom(2048)
if address_ip.decode('utf-8') != 'Unknown':
# Send ip to client
s.sendto(address_ip, sender_info)
# Update ips file
website_ips[data.decode("utf-8")] = address_ip.decode('utf-8')
text = ''
for key in website_ips.keys():
text += "%s,%s\n" % (key, website_ips[key])
with open("./ips.txt", "w+") as f:
f.write(text[:-1])
else:
# Parent returned unknown
s.sendto(address_ip, sender_info)
if (len(sys.argv) == 5):
run_server(int(sys.argv[1]), sys.argv[2], int(sys.argv[3]), sys.argv[4])
else:
print("Invalid number of arguments")