-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseed.py
executable file
·78 lines (69 loc) · 2.65 KB
/
seed.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
import socket
import threading
MY_IP = "0.0.0.0" # IP for listening
PORT = int(input("Port No. = ")) # Port No. for listening
peer_list = [] # List to store connected Peers
# Write the outputs to the file
def write_output_to_file(output):
try:
file = open("outputseed.txt", "a")
file.write(output + "\n")
except:
print("Write Failed")
finally:
file.close()
# Create socket to connect 2 computers
def create_socket():
global socket
socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
# Bind socket
def bind_socket():
global socket
ADDRESS = (MY_IP, PORT)
socket.bind(ADDRESS)
# It take list of connected peer and convert it to comma separated string to send it through socket
def list_to_string(peer_list):
PeerList = ","
for i in peer_list:
PeerList += i + ","
return PeerList
# Take the dead node message, find address of dead node from it and remove it from peer list if present
def remove_dead_node(message):
print(message)
write_output_to_file(message)
message = message.split(":")
dead_node = str(message[1]) + ":" + str(message[2])
if dead_node in peer_list:
peer_list.remove(dead_node)
# To handle different peers in threads.It receives peer address from peer and add it to its peer list
# If it receives dead node message then pass it remove_dead_node()
def handle_peer(conn, addr):
while True:
try:
message = conn.recv(128).decode('utf-8')
if message:
if "Dead Node" in message[0:9]:
remove_dead_node(message)
else:
message = message.split(":")
peer_list.append(str(addr[0])+":"+str(message[1]))
output = "Received Connection from " + str(addr[0])+":"+str(message[1])
print(output)
write_output_to_file(output)
PeerList = list_to_string(peer_list)
conn.send(PeerList.encode('utf-8'))
except:
break
conn.close()
# To listen at a particular port and create thread for each peer
def begin():
socket.listen(100)
print("Seed is Listening")
while True:
conn, addr = socket.accept()
socket.setblocking(5)
thread = threading.Thread(target=handle_peer, args=(conn,addr))
thread.start()
create_socket()
bind_socket()
begin()