-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.py
More file actions
171 lines (120 loc) · 4.19 KB
/
node.py
File metadata and controls
171 lines (120 loc) · 4.19 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import socket
import time
import json
import glob
import sys
from info import *
message_size = 2048
approved_directories = ('cache', 'latin_dictionary', 'noun_adjective_dictionary', 'timed_vocabulary_dictionary', 'timed_morphology_dictionary', 'reading_keys')
approved_directory_path = f'.{subDirectory}data{subDirectory}'
run_server = True
server_running = False
def handle_connection(conn, addr) -> None:
"""
Handles incoming connections from clients.
:param conn: The connection socket.
:param addr: The client's address.
:return: None
"""
raw_data = conn.recv(1024)
data = json.loads(str(raw_data.decode()))
request = data.get('request')
command = None
sub_command = None
if data.get('user') is None:
conn.send('{}')
conn.close()
return None
print(f"Connected by {addr} | user: {data.get('user')} | request: {request}")
if request is not None:
request = request.split(' ')
command = request[0]
if len(request) >= 2:
sub_command = request[1]
response = None
match command:
case "list":
response = {}
if sub_command in approved_directories:
files = glob.glob(f'.{subDirectory}data{subDirectory}{sub_command}{subDirectory}*.json')
for temp_file in files:
with open(temp_file, mode='r', encoding='utf-8') as file:
file_data = json.load(file)
response[temp_file.replace(subDirectory, '(sub)')] = sys.getsizeof(str(file_data).encode())
case "get":
response = None
for approved_directory in approved_directories:
sub_command = sub_command.replace('(sub)', subDirectory)
if f'{approved_directory_path}{approved_directory}{subDirectory}' in sub_command and response is None and os.path.exists(sub_command) == True:
with open(sub_command, mode='r', encoding='utf-8') as file:
response = json.load(file)
offset = 0
response = str(response).encode()
while offset < len(response):
chunk = response[offset:offset+message_size]
sent = conn.send(chunk)
if sent == 0:
raise RuntimeError("socket connection broken")
offset += sent
conn.close()
def server(host: str, port: int) -> None:
"""
Starts a server and listens for incoming connections.
:param host: The host address to bind to.
:param port: The port to listen on.
:return: None
"""
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind((host, port))
sock.listen()
print('starting server!')
server_running = True
while run_server == True:
conn, addr = sock.accept()
try:
handle_connection(conn, addr)
except Exception as error:
print(error)
time.sleep(.5)
server_running = False
def stop_server() -> None:
"""
Stops the server.
:return: None
"""
while (server_running == True):
run_server = False
time.sleep(.5)
run_server = True
def connect_to_peer(peer_host, peer_port, message: dict) -> None:
"""
Connects to a peer and sends a message.
:param peer_host: The peer's host address.
:param peer_port: The peer's port.
:param message: The message to send.
:return: None
"""
peer_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
peer_sock.connect((peer_host, peer_port))
peer_sock.sendall(message.encode())
response = b''
while True:
chunk = peer_sock.recv(message_size)
if not chunk:
break
response += chunk
print(f"Received response: {response.decode()}")
peer_sock.close()
def get_local_ip() -> str:
"""
Gets the local IP address of the machine.
:return: The local IP address as a string.
"""
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.connect(("8.8.8.8", 80))
ip_address = sock.getsockname()[0]
sock.close()
return ip_address
except socket.error:
return None