-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
135 lines (101 loc) · 3.52 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
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
import socket
import os
from _thread import start_new_thread
import threading
HELLO_MESSAGE = "HELLO"
HELLO_MESSAGE_RESPONSE = "HI"
db = dict() # Create empty dictionary (tuple -> list[int])
def deserialize_files(files):
files = files.split('\n')
result = []
for file_config in files:
result.append(file_config.split(','))
return result[:-1]
def append_ip_and_port(files, addr):
for file in files:
file[-1] = addr[0]
file.append(str(addr[1]))
def process_hello_message(conn, addr):
conn.send(HELLO_MESSAGE_RESPONSE.encode())
files = conn.recv(1024).decode("utf-8")
files_to_store = deserialize_files(files)
if (len(files_to_store) > 0):
append_ip_and_port(files_to_store, addr)
print(f"{len(files_to_store)} file(-s) from {addr} has been stored")
db[addr] = files_to_store
else:
print(f"No data from {addr}, not allowing to enter FT Server")
conn.close()
def process_bye_message(conn, addr):
result = db.pop(addr, None)
if result:
print(f"Entry {addr} removed from storage")
else:
print(f"Entry {addr} is not found in storage")
conn.close()
def process_search_message(conn, addr, data):
file_name = data[7:] # removes first part of the message ("SEARCH:")
if not addr in db:
conn.close()
print(f"No data from {addr}, not allowing search FT Server data")
return
files_to_send = []
for key in db.keys():
entry = db[key]
for file_data in entry:
print(f"File Data: {file_data}")
if file_name in file_data:
files_to_send.append(file_data)
if len(files_to_send) == 0:
conn.send("NOT FOUND".encode())
else:
message = "FOUND:"
for file_info in files_to_send:
print(file_info)
file_data = ",".join(file_info)
message = f"{message}{file_data}\n"
conn.send(message.encode())
conn.close()
def process_connection(conn, addr):
while conn:
data = conn.recv(1024)
if not data or len(data) == 0:
print("No data")
break
data = data.decode("utf-8")
print(f"Recieved data: {data} from: {addr}")
if data.startswith("HELLO"):
process_hello_message(conn, addr)
elif data.startswith("BYE"):
process_bye_message(conn, addr)
elif data.startswith("SEARCH"):
process_search_message(conn, addr, data)
break
def main():
print("Launching File Tracker(FT) Server")
port_number = input("Please enter the port number: ")
while not port_number.isnumeric():
port_number = input("Please enter a number e.g. 9999")
port_number = int(port_number)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(('', port_number))
hostname = socket.gethostname()
ip_address = socket.gethostbyname(hostname)
print("=============================")
print(f"IP Address: {ip_address}")
print(f"Port: {port_number}")
print("Please use the information above to connect to FT Server")
print("=============================")
try:
while True:
print("Waiting for connection...")
s.listen()
conn, addr = s.accept()
start_new_thread(process_connection, (conn, addr,))
s.close()
except KeyboardInterrupt:
s.close()
print("Socket is closed")
if __name__ == "__main__":
main()