-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.py
377 lines (318 loc) · 13.2 KB
/
database.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
#!/usr/bin/env python
# console command - database imports
import argparse
import csv
from person import *
# Server-client imports
import socket
import sys
import struct
import hashlib
import getpass
# Server code
class database:
HOSTNAME = "0.0.0.0"
PORT = 50000
BUFFER_SIZE = 1024
MAX_BACKLOG = 10
ENCODER = "ascii"
SOCKET_ADDR = (HOSTNAME, PORT)
def __init__(self):
#self.name = name
#self.student_database_file = student_database_file
self.students = {}
self.create_listen_socket()
self.import_student_database()
self.process_connections_forever()
def create_listen_socket(self):
try:
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR,1)
self.socket.bind(database.SOCKET_ADDR)
self.socket.listen(database.MAX_BACKLOG)
print("Listening on port {} ...".format(database.PORT))
except Exception as msg:
print(msg)
sys.exit(1)
def process_connections_forever(self):
try:
while True:
self.connection_handler(self.socket.accept())
except Exception as msg:
print(msg)
except KeyboardInterrupt:
print()
finally:
self.socket.close()
sys.exit(1)
def connection_handler(self, client):
connection, addr_port = client
print("-"*72)
print("Connection with {}".format(addr_port))
print(client)
while True:
try:
recv_bytes = connection.recv(database.BUFFER_SIZE)
if len(recv_bytes) == 0:
print("Closing client connection...")
connection.close()
break
try:
recv_str = recv_bytes.decode(database.ENCODER)
print("-"*72)
print("Received: ", recv_str)
print("-"*72)
except Exception as e:
recv_str = recv_bytes
if recv_str == "get lab average":
self.print_students("GL1", connection)
#print("getting lab 2 average")
elif recv_str == "get lab 2 average":
self.print_students("GL2", connection)
#print("getting lab 2 average")
elif recv_str == "get lab 3 average":
self.print_students("GL3", connection)
#print("getting lab 3 average")
elif recv_str == "get lab 4 average":
self.print_students("GL4", connection)
#print("getting lab 4 average")
elif recv_str == "get midterm average":
self.print_students("GMA", connection)
#print("getting midterm average")
elif recv_str == "":
self.print_students()
else:
self.print_students(recv_str, connection)
#connection.sendall()
#print("Sent: ", self.average)
except KeyboardInterrupt:
print()
print("Closing client connection due to error...")
connection.close()
break
def import_student_database(self):
try:
csv_file = open("course_grades_2022.csv")
read_file = open("course_grades_2022.csv")
reader = csv.reader(csv_file)
read = csv.reader(read_file)
for row in read:
print(row)
next(reader)
self.student_list = [(e[0], int(e[1]), e[2], int(e[3]), int(e[4]), int(e[5]), int(e[6]), int(e[7]), int(e[8]), int(e[9]),int(e[10]), int(e[11])) for e in reader]
#print(self.student_list)
self.create_student_dictionary()
except Exception:
print("Error: reading input file")
exit()
def create_student_dictionary(self):
for student in self.student_list:
#print(student)
try:
# 1.Name, 2.ID Number, 3.Password, 4.Lab 1, 5.Lab 2, 6.Lab 3, 7.Lab 4, 8.Midterm, 9.Exam 1, 10.Exam 2, 11.Exam 3, 12.Exam 4
Studname, student_id, password, l1, l2, l3, l4, mt, e1, e2, e3, e4 = student
new_student = Person(Studname, student_id, password, l1, l2, l3, l4, mt, e1, e2, e3, e4)
self.add_student(student_id, new_student)
except Exception:
print("Error: student creation \"{}\"".format(Studname))
exit()
def add_student(self, student_id, person):
try:
self.students[student_id] = person
#print(self.students[0])
except Exception:
print("Error: adding student")
def print_students(self, command, connection):
count = 0
average = 0
if command == "GL1":
print("Command: ", command)
for id,t in self.students.items():
average += t.l1
count += 1
val = str(average/count)
send_str = val.encode(database.ENCODER)
connection.sendall(send_str)
print("Sent: ", send_str)
elif command == "GL2":
print("Command: ", command)
for id,t in self.students.items():
average += t.l2
count += 1
val = str(average/count)
send_str = val.encode(database.ENCODER)
connection.sendall(send_str)
print("Sent: ", send_str)
#print("Lab 2 averge: ", average/id.length())
elif command == "GL3":
print("Command: ", command)
for id,t in self.students.items():
average += t.l3
count += 1
val = str(average/count)
send_str = val.encode(database.ENCODER)
connection.sendall(send_str)
print("Sent: ", send_str)
#print("Lab 3 averge: ", average/id.length())
elif command == "GL4":
print("Command: ", command)
for id,t in self.students.items():
average += t.l4
count += 1
val = str(average/count)
send_str = val.encode(database.ENCODER)
connection.sendall(send_str)
print("Sent: ", send_str)
#print("Lab 4 averge: ", average/id.length())
elif command == "GMA":
print("Command: ", command)
for id,t in self.students.items():
average += t.mt
count += 1
val = str(average/count)
send_str = val.encode(database.ENCODER)
connection.sendall(send_str)
print("Sent: ", send_str)
else:
#student_num = int(cmd_content[1])
#password = cmd_content[2]
hashPass = command
print("Received ID/Password Hash: ", hashPass)
foundVal = False
for id,t in self.students.items():
if self.get_hash_ID_Password(str(id),t.passw) == hashPass:
user = id
for id1,t1 in self.students.items():
if(user == id1):
send_str = " ".join((
"Lab1:",str(t.l1),"Lab2:",str(t.l2),"Lab3:",str(t.l3),"Lab4:",str(t.l4),
"Midterm:",str(t.mt),"Exam1:",str(t.e1),"Exam2:",str(t.e2),"Exam3:",str(t.e3),"Exam4:",str(t.e4)
)).encode(database.ENCODER)
foundVal = True
print("Correct password, record found")
if(foundVal == False):
send_str = "ID/Password entry does not match record".encode(database.ENCODER)
connection.sendall(send_str)
print("Sent: ", send_str)
#for id,t in self.students.items():
# print("Student ID: {} Name: \"{}\"".format(id, t.StudName))
#print()
def get_hash_ID_Password(self, id, password):
h = hashlib.sha256() # Create sha256 hash object
h.update(id.encode("utf-8")) # Getting full hash with encoded ID
h.update(password.encode("utf-8")) # Getting full hash with encoded Password
return h.digest() # return the ID/Password Hash
# Client code
class Client:
#SERVER_HOSTNAME = "192.168.1.22"
SERVER_HOSTNAME = socket.gethostname()
BUFFER_SIZE = 1024
def __init__(self):
#print("setting up get_socket()")
self.get_socket()
#print("finish get_socket()")
#print("setting up connect_to_server()")
self.connect_to_server()
#print("finish connect_to_server()")
#print("setting up send_console()")
self.send_console()
#print("finish send_console()")
def get_socket(self):
try:
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except Exception as msg:
print(msg)
sys.exit(1)
def connect_to_server(self):
try:
#print(Client.SERVER_HOSTNAME)
#print(database.PORT)
self.socket.connect((Client.SERVER_HOSTNAME, database.PORT))
print("Connected to \"{}\" on port {}".format(Client.SERVER_HOSTNAME, database.PORT))
except Exception as msg:
print(msg)
sys.exit(1)
def send_console(self):
while True:
try:
self.get_console_input()
self.connection_send()
self.connection_recv()
except (KeyboardInterrupt, EOFError):
print()
print("Closing connection with server...")
self.socket.close()
sys.exit(1)
def get_console_input(self):
while True:
self.input_text = input("Input: ")
if self.input_text != "":
break
def connection_send(self):
try:
print("Commend entered: ",self.input_text)
if self.input_text == "get lab average":
print("Fetching lab 1 average...")
self.socket.sendall(self.input_text.encode(database.ENCODER))
elif self.input_text == "get lab 2 average":
print("Fetching lab 2 average...")
self.socket.sendall(self.input_text.encode(database.ENCODER))
elif self.input_text == "get lab 3 average":
print("Fetching lab 3 average...")
self.socket.sendall(self.input_text.encode(database.ENCODER))
elif self.input_text == "get lab 4 average":
print("Fetching lab 4 average...")
self.socket.sendall(self.input_text.encode(database.ENCODER))
elif self.input_text == "get midterm average":
print("Fetching midterm average...")
self.socket.sendall(self.input_text.encode(database.ENCODER))
elif self.input_text == "GG":
self.student_num = input("Student ID: ")
self.password = getpass.getpass(prompt = "Password: ")
#print("Student ID received: ", self.student_num)
#print("Password received: ", self.password)
new_str = self.get_hash_ID_Password(self.student_num,self.password)
print("Sent ID/Password Hash: ", new_str)
#self.socket.sendall(new_str.encode(database.ENCODER))
self.socket.sendall(new_str)
else:
print("Command doesn't exist")
except Exception as msg:
print(msg)
sys.exit(1)
def get_hash_ID_Password(self, id, password):
h = hashlib.sha256() # Create sha256 hash object
h.update(id.encode("utf-8")) # Getting full hash with encoded ID
h.update(password.encode("utf-8")) # Getting full hash with encoded Password
return h.digest() # return the ID/Password Hash
def connection_recv(self):
try:
recv_bytes = self.socket.recv(database.BUFFER_SIZE)
if len(recv_bytes) == 0:
print("Closing server connection...")
self.socket.close()
sys.exit(1)
print("-"*72)
print("Received: ", recv_bytes.decode(database.ENCODER))
print("-"*72)
except Exception as msg:
print(msg)
sys.exit(1)
#terminal run code
if __name__ == "__main__":
#DEFINE_database = './course_grades_2022.csv'
#DEFINE_class = "4DN4"
#new_database = database(DEFINE_class, DEFINE_database)
#print('-'*72)
#print("Course Name: \"{}\".".format(new_database.name))
#print('-'*72)
#functions = {
# 'print': new_database.print_students
#}
roles = {'client': Client, 'server': database}
parser = argparse.ArgumentParser()
parser.add_argument('-r', '--role', choices=roles,
help='server or client role',
required=True, type=str)
args = parser.parse_args()
roles[args.role]()