-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver1.py
185 lines (143 loc) · 4.85 KB
/
server1.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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import os
import socket
import time
from models import *
import pandas as pd
from utils import *
from create_report import *
import re
import hashlib
from _thread import *
thread_count = 0
PORT = 2223
SERVER = socket.gethostname()
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((SERVER, PORT))
HEADER = 1024
server.listen(5)
print(f"[LISTENING] Server is listening on {SERVER}")
server_dir = "./server_data/"
user_dic = {}
live_table = []
def password_check(email, pswd):
hashpswd = hashlib.sha256(pswd.encode("utf-8")).hexdigest()
if email in user_dic:
if user_dic[email] == hashpswd:
live_table.append(email)
return "Login"
elif user_dic[email] != hashpswd:
return "Error"
else:
user_dic[email] = hashpswd
live_table.append(email)
return "Signup"
def client_thread(client, addr):
# email = conn.recv(HEADER).decode()
# initial = email.split('@')[0]
# password = conn.recv(HEADER).decode()
login = False
while login == False:
email = conn.recv(HEADER).decode()
initial = email.split('@')[0]
password = conn.recv(HEADER).decode()
password = hashlib.sha256(password.encode("utf-8")).hexdigest()
pswd_return = password_check(email, password)
print(pswd_return)
if pswd_return == "Login" or pswd_return == "Signup":
conn.send("Thank You!".encode())
login = True
break
else:
conn.send("Error!".encode())
login = False
user_dir = os.path.join(server_dir, initial)
if os.path.isdir(user_dir):
pass
else:
os.mkdir(user_dir)
file_name = f'./server_data/{initial}/received.csv'
file_size = conn.recv(100).decode()
print(file_size)
# Opening and reading file.
# with open(file_name, "wb") as file:
file = open(file_name, "wb")
c = 0
# Starting the time capture.
start_time = time.time()
# Running the loop while file is recieved.
while c < int(file_size):
data = conn.recv(1024)
if not (data):
break
file.write(data)
c += len(data)
file.close()
# Ending the time capture.
end_time = time.time()
print("File transfer Complete.Total time: ", end_time - start_time)
#receive info about model training from client
degree = conn.recv(100).decode()
print(degree)
training_ratio = conn.recv(100).decode()
print(training_ratio)
df = pd.read_csv(file_name)
training_points = int(len(df)*int(training_ratio)/100)
testing_points = len(df) - training_points
reg_training_model(df, degree = int(degree), split_ratio = int(training_ratio)/100, user_dir = user_dir)
tot, maxi, mini, mean, medi, sd = stats_summary(df)
print("model training done......")
#send 1 image to client
file_path = f'./server_data/{initial}/fitting.png'
file_size = os.path.getsize(file_path)
print(file_size)
conn.send(str(file_size).encode())
with open(file_path, "rb") as file:
c = 0
# Starting the time capture.
start_time = time.time()
# Running loop while c != file_size.
while c <= file_size:
data = file.read(1024)
if not (data):
break
conn.sendall(data)
c += len(data)
# Ending the time capture.
end_time = time.time()
file.close()
print("[INFO] Fitting image Transfer Complete.Total time: ", end_time - start_time)
#client "finalising report......"
# conn.send("finalising report....".encode())
#file report downloaded. Local path"receivedreport.pdf"
c_report(tot, maxi, mini, mean, medi, sd, training_points, testing_points, degree, split = training_ratio, user_dir = user_dir)
print(str(tot))
print("report generated now")
file_path = f'./server_data/{initial}/report.pdf'
file_size = os.path.getsize(file_path)
print(file_size)
conn.send(str(file_size).encode())
with open(file_path, "rb") as file:
c = 0
# Starting the time capture.
start_time = time.time()
# Running loop while c != file_size.
while c <= file_size:
data = file.read(1024)
if not (data):
break
conn.sendall(data)
c += len(data)
# Ending the time capture.
end_time = time.time()
file.close()
print("[INFO] Report file Transfer Complete.Total time: ", end_time - start_time)
live_table.remove(email)
# Closing the socket.
conn.close()
print(f"Connection Closed with {addr}")
while True:
conn, addr = server.accept()
print("Connection established with " + str(addr[0]) + ", " + str(addr[1]))
start_new_thread(client_thread, (conn,addr))
thread_count += 1
print('Thread Number: ' + str(thread_count))