-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdir.py
57 lines (37 loc) · 1.21 KB
/
dir.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 socket
import pickle
hosts = {}
tmp = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
tmp.connect(("8.8.8.8", 80))
ipAddr = tmp.getsockname()[0]
tmp.close()
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #dhmiourgia sundeshs TCP
s.bind((ipAddr, 0))
port = s.getsockname()[1]
print('Directory for keeping info about remote users!')
print("Directory -- IP: " + str(ipAddr) + " port: " + str(port))
print('\n')
s.listen(100)
while True:
connection, host_addr = s.accept() #apodoxh sundesewn
try:
data = connection.recv(256) #apokwdikopihsh kai apothikeush se katallhlo pinaka
data = data.decode("utf-8")
info, rest_packet = data.split(',', 1)
if info == 'List':
packet = pickle.dumps(hosts)
connection.send(packet) #send listto sender
elif info == 'Join':
runtime, rest_packet = rest_packet.split(',',1)
ip_addr, port = rest_packet.split(',',1)
runtime = str(runtime)
ip_addr = str(ip_addr)
port = int(port)
hosts[runtime] = [ip_addr, port]
message = ('OK').encode("utf-8")
connection.send(message) #send OK to sender
finally:
connection.close()
print('------------- Hosts in directory -------------')
print(hosts)
print('\n')