-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bootnode.py
39 lines (32 loc) · 1.3 KB
/
bootnode.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
import socket
import threading
class BootNode:
def __init__(self, host='0.0.0.0', port=5000):
self.host = host
self.port = port
self.peers = []
self.server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.server_socket.bind((self.host, self.port))
self.server_socket.listen(5)
print("Bootnode listening on {}:{}".format(self.host, self.port))
self.run()
def run(self):
while True:
connection, addr = self.server_socket.accept()
print("Connection from:", addr)
client_thread = threading.Thread(target=self.handle_client, args=(connection, addr))
client_thread.start()
def handle_client(self, connection, addr):
try:
while True:
data = connection.recv(1024).decode('utf-8')
if data == 'join':
self.peers.append(addr)
connection.send(b'\x11' + ','.join(['{}:{}'.format(*peer) for peer in self.peers]).encode('utf-8') + b',')
print("Current peers:", self.peers)
break
finally:
connection.close()
if __name__ == "__main__":
bootnode = BootNode()