-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
813 lines (677 loc) · 30.5 KB
/
server.py
File metadata and controls
813 lines (677 loc) · 30.5 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
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
import socket
from config import *
import threading
import sys
import sqlite3
class Server:
def __init__(self):
self.sock=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
self.addr=(ip,port)
self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.sock.bind(self.addr)
self.conn=sqlite3.connect("./database/database.db", check_same_thread=False,timeout=100)
self.chat_details = {}
self.sock.listen()
self.run()
def send_message(self,message,client_sock):
message=message+sep
try:
client_sock.send(bytes(message+'0'*(chunk_size-len(message)),encoding="utf-8"))
except Exception as E:
print("Exception in sending message")
print(E)
# self.sock.close()
# self.conn.close()
sys.exit(0)
def receive_message(self,client_sock):
# print(type(client_sock))
try:
return client_sock.recv(chunk_size).decode().split(sep)
except Exception as E:
print("Exception in recieving message")
print(E)
# self.sock.close()
# self.conn.close()
sys.exit(0)
def run(self):
try:
while(True):
# try:
client_sock,client_addr=self.sock.accept()
client_sock = client_sock
threading.Thread(target=self.connect,args=(client_sock,)).start()
# except socket.error:
# self.run()
# print("Got socket error")
except Exception as e:
print("An Exception occured")
print(e)
sys.exit(1)
finally:
self.sock.close()
def connect(self,client_sock):
reg_msg="Choose [1]Register or [2]Login\n Type the corresponding no."+sep
client_sock.send(bytes(reg_msg+'0'*(chunk_size-len(reg_msg)),encoding="utf-8"))
msg_rcvd=client_sock.recv(chunk_size).decode()
msg_rcvd=int(msg_rcvd.split(sep)[0])
self.next_steps(client_sock,msg_rcvd)
# return msg_rcvd
def next_steps(self,client_sock,msg_rcvd):
registration= 0
login_status = 0
if(msg_rcvd == 1):
while(True):
if(self.register(client_sock)):
registration = 1
break
if(registration == 1 or msg_rcvd==2):
while(True):
values = self.login(client_sock)
if(values[0]):
login_status =1
break
print("User Logged IN")
self.functions(values[1],values[2],client_sock)
self.sock.close()
return
def register(self,client_sock):
val = (client_sock.recv(chunk_size).decode().split(sep))
# print(val)
usr,passwrd,name,typ,_= val
try:
cursor=self.conn.cursor()
tmp=(usr,passwrd,name,int(typ),)
cursor.execute('select * from user_password where user_name== ?',(usr,))
res=cursor.fetchall()
# print(res)
if(len(res)!=0):
self.send_message("not ok",client_sock)
return False
# print(tmp)
cursor.execute('insert into user_password("user_name","passwrd","name","type") values(?,?,?,?)',tmp)
self.conn.commit()
self.send_message("ok",client_sock)
cursor.close()
return True
except Exception as e:
self.sock.close()
print("exception in registering")
print(e)
sys.exit(0)
def login(self,client_sock):
try:
usr,passwrd,_=client_sock.recv(chunk_size).decode().split(sep)
cursor=self.conn.cursor()
tmp=(usr,passwrd,)
cursor.execute('select * from user_password where user_password.user_name==? and user_password.passwrd==?',tmp)
res=cursor.fetchone()
# print(res)
if(len(res)==0):
self.send_message("not ok",client_sock)
return (False,1,usr)
cursor.close()
self.send_message("ok",client_sock)
self.send_message(str(res[4]),client_sock)
if(res[4]==1):
return (True,1,usr)
else:
return (True,0,usr)
except Exception as e:
print("exception in login")
print(e)
sys.exit(0)
def functions(self,check,user_name,client_sock):
if(check==1):
self.home_teacher(user_name,client_sock)
else:
self.home_student(user_name,client_sock)
def view_post(self,client_sock,username,course_code):
self.send_message("do you want to filter by keywords",client_sock)
filter = int(self.receive_message(client_sock)[0])
if(filter):
self.send_message("please write keyword",client_sock)
keyword = self.receive_message(client_sock)[0]
try:
cursor=self.conn.cursor()
tmp=(course_code,keyword)
cursor.execute('select * from posts where course_code == ? and keyword == ?',tmp)
res=cursor.fetchall()
string = "Course_Code Post Keyword TimePosted\n"
for i in res:
string+= str(i[1])+" " +i[2]+" "+i[3]+" "+i[4]+"\n"
self.send_message(string,client_sock)
except Exception as e:
self.send_message("Failed to show posts",client_sock)
print(e)
cursor.close()
else:
try:
cursor=self.conn.cursor()
tmp=(course_code,)
cursor.execute('select * from posts where course_code == ?',tmp)
res=cursor.fetchall()
string = "Course_Code Post Keyword TimePosted\n"
for i in res:
string+= str(i[1])+" " +i[2]+" "+i[3]+" "+i[4]+"\n"
self.send_message(string,client_sock)
except Exception as e:
self.send_message("Failed to show posts",client_sock)
print(e)
cursor.close()
def add_post(self,client_sock,user_name,course_code):
self.send_message("please type post to add to classroom",client_sock)
temp= self.receive_message(client_sock)
post_details = temp[0]
if(len(temp)>2):
keyword = temp[1]
else:
keyword = None
cursor=self.conn.cursor()
try:
tmp=(user_name,course_code,post_details,keyword,)
cursor.execute('insert into posts("user_name","course_code","post_details","keyword", "add_time") values(?,?,?,?,datetime(\'now\', \'localtime\'))',tmp)
self.conn.commit()
self.send_message("ok",client_sock)
except Exception as e:
self.send_message("Failed to add posts",client_sock)
print("Exception")
print(e)
def broadcast(self,message,client_sock,course_code,user_name):
print("ALOINDAWOINEFAEORFNAERNFO")
try:
for i in self.chat_details[course_code]["clients"]:
if(i!= client_sock):
self.send_message(message,i)
self.chat_details[course_code]["messages"].append(user_name+"---->"+message)
except Exception as e:
print(e)
print("ALOINDAWOINEFAEORFNAERNFO")
def chat_session(self,user_name,course_code,client_sock):
if course_code not in self.chat_details:
self.chat_details[course_code] = {"clients":[],"messages":[]}
self.chat_details[course_code]["clients"].append(client_sock)
# print('asdasdasdasdasd')
while(True):
message = self.receive_message(client_sock)[0]
# print('asdasdasdasdasd')
print(message)
if(message == "exit"):
self.broadcast("i am closing chat session",client_sock,course_code,user_name)
self.chat_details.pop(course_code)
break
elif(message == "showall"):
final = ""
message = self.chat_details[course_code]["messages"]
for i in message:
final+=i+"\n"
# print(final)
self.send_message(final,client_sock)
else:
self.broadcast(message,client_sock,course_code,user_name)
self.home_teacher(user_name,client_sock)
def show_posts_and_courses(self,client_sock,user_name):
self.send_message("Please enter Course Code to view course details",client_sock)
course_code = int(self.receive_message(client_sock)[0])
self.send_message("""please enter 1 to add post and 2 to view posts and 3 to start a chat session and 4 create GD and 5 for viewing GD
and 6 for private_convo""",client_sock)
value = int(self.receive_message(client_sock)[0])
if(value == 1):
self.add_post(client_sock,user_name,course_code)
elif(value ==2 ):
self.view_post(client_sock,user_name,course_code)
elif(value ==3):
# print(3,'asdasdass')
self.chat_session(user_name,course_code,client_sock)
elif value==4:
self.create_gd(course_code,client_sock,user_name)
elif value==5:
self.view_gd_teacher(course_code,client_sock,user_name)
elif value==6:
self.teacher_private_conv(user_name,client_sock,course_code)
self.home_teacher(user_name,client_sock)
return
def teacher_private_conv(self,user_name,client_sock,course_code):
cursor=self.conn.cursor()
sql='select distinct student_name from private_conv where teacher_name==? and course_code==?'
try:
cursor.execute(sql,(user_name,course_code,))
res=cursor.fetchall()
if(len(res)!=0):
string="choose private_conv with student from "
for i in res:
string+=i[0]+" "
string+="\n"
self.send_message("ok",client_sock)
self.send_message(string,client_sock)
else:
self.send_message("nok",client_sock)
self.send_message("No student conversation",client_sock)
self.home_teacher(user_name,client_sock)
return
except Exception as e:
print(e)
self.send_message("nok",client_sock)
self.send_message("Not",client_sock)
self.home_teacher(user_name,client_sock)
return
student_name=self.receive_message(client_sock)[0]
self.send_message("Please select 1 to view message and 2 to send message",client_sock)
choose=self.receive_message(client_sock)[0]
# print(teacher_name,type(teacher_name))
if choose=='1':
try:
cursor.execute("select * from private_conv where student_name==? and teacher_name == ? and course_code==?",(student_name,user_name,course_code,))
res=cursor.fetchall()
string="Message time\n"
for i in res:
string+=i[2]+" "+i[3]+"\n"
self.send_message(string,client_sock)
except Exception as e:
self.send_message("No convo",client_sock)
print(e)
else:
"""insert name of sender"""
try:
msg=self.receive_message(client_sock)[0]
msg=f"{user_name}--->"+msg
cursor.execute("insert into private_conv values(?,?,?,datetime(\'now\', \'localtime\'),?)",(student_name,user_name,msg,course_code,))
self.conn.commit()
self.send_message("ok",client_sock)
except Exception as e:
self.send_message("No",client_sock)
print(e)
cursor.close()
self.home_teacher(user_name,client_sock)
def create_gd(self,course_code,client_sock,user_name):
sql='insert into group_discussion("topic","course_code","user_name","message","post_time") values(?,?,?,?,datetime(\'now\', \'localtime\'))'
temp= self.receive_message(client_sock)
topic=temp[0]
cursor=self.conn.cursor()
try:
cursor.execute("select * from group_discussion where course_code==? and topic==? and user_name==?",(course_code,topic,user_name,))
res=cursor.fetchall()
if len(res)>0:
self.send_message("not ok",client_sock)
self.send_message("Already available",client_sock)
self.home_teacher(user_name,client_sock)
return
cursor.execute(sql,(topic,course_code,user_name,"Created GD",))
self.conn.commit()
self.send_message("ok",client_sock)
self.send_message("Created GD",client_sock)
except Exception as E:
print(E)
self.send_message("not ok",client_sock)
self.send_message("Not created GD",client_sock)
self.home_teacher(user_name,client_sock)
def view_gd_teacher(self,course_code,client_sock,user_name):
self.send_message("Please input topic",client_sock)
topic=self.receive_message(client_sock)[0]
cursor=self.conn.cursor()
try:
cursor.execute("select * from group_discussion where course_code==? and topic==? and user_name==?",(course_code,topic,user_name,))
res=cursor.fetchall()
if len(res)<=0:
self.send_message("not",client_sock)
self.home_teacher(user_name,client_sock)
return
except Exception as e:
print(e)
self.send_message("not",client_sock)
self.home_teacher(user_name,client_sock)
return
self.send_message("ok",client_sock)
self.send_message("input 1 for viewing messages in gd and 2 for adding messages to gd",client_sock)
v=int(self.receive_message(client_sock)[0])
sql1="select user_name,message,post_time from group_discussion where course_code == ? and topic == ?"
sql2='insert into group_discussion("topic","course_code","user_name","message","post_time") values(?,?,?,?,datetime(\'now\', \'localtime\'))'
if v==1:
try:
cursor.execute(sql1,(course_code,topic,))
res=cursor.fetchall()
string = "user_name message post_time\n"
for i in res:
string+=i[0]+" "+i[1]+" "+i[2]+"\n"
self.send_message(string,client_sock)
except Exception as E:
print(E)
self.send_message("Not able to fetch",client_sock)
elif v==2:
self.send_message("please type message to add to gd",client_sock)
temp= self.receive_message(client_sock)
message = temp[0]
try:
cursor.execute(sql2,(topic,course_code,user_name,message,))
self.conn.commit()
self.send_message("ok",client_sock)
except Exception as E:
print(E)
self.send_message("not ok",client_sock)
cursor.close()
self.home_teacher(user_name,client_sock)
return
def home_teacher(self,user_name,client_sock):
self.send_message(f"Hi {user_name}\nPress 1 for showing existing courses\nPress 2 for creating new course \n press 3 for viewing course",client_sock)
instruction=self.receive_message(client_sock)[0]
print(instruction)
if instruction=="1":
self.view_courses_teacher(user_name,client_sock)
elif instruction=="2":
self.create_course(user_name,client_sock)
elif instruction == "3":
self.show_posts_and_courses(client_sock,user_name)
else:
self.home_teacher(user_name,client_sock)
def view_courses_teacher(self,user_name,client_sock):
try:
cursor=self.conn.cursor()
tmp=(user_name,)
cursor.execute('select * from courses where courses.user_name== ?',tmp)
res=cursor.fetchall()
string = "Course_Code Course_Name\n"
for i in res:
string+= str(i[0])+" " +i[1]+"\n"
self.send_message(string,client_sock)
except Exception as e:
self.send_message("Failed to show courses",client_sock)
print(e)
cursor.close()
self.home_teacher(user_name,client_sock)
def create_course(self,user_name,client_sock):
# message = "Please input course code and course name to create course "
# self.send_message(message,client_sock)
course_code,course_name,_ = self.receive_message(client_sock)
cursor=self.conn.cursor()
try:
tmp=(user_name,course_code,course_name,)
cursor.execute('insert into courses("user_name","course_code","course_name") values(?,?,?)',tmp)
self.conn.commit()
self.send_message("ok",client_sock)
except Exception as e:
self.send_message("Failed to create course",client_sock)
print("Exception")
print(e)
# self.sock.close()
# sys.exit(0)
cursor.close()
self.home_teacher(user_name,client_sock)
def home_student(self,user_name,client_sock):
self.send_message("""Hi, welcome to the classroom\nHere you can view and enroll for courses \n
input 1 for showing existing courses \n
input 2 for enrollig new course \n
input 3 for viewing updates for registered courses\n
input 4 for viewing details of a registered course\n
""",client_sock)
message = self.receive_message(client_sock)[0]
message=int(message)
if message==1:
self.view_courses_student(user_name,client_sock)
elif message==2:
self.enroll_course(user_name,client_sock)
elif message==3:
self.view_updates(user_name,client_sock)
elif message==4:
self.check_course_reg(user_name,client_sock)
else:
self.home_student(user_name,client_sock)
def check_course_reg(self,user_name,client_sock):
self.send_message("please input the registered course's code",client_sock)
course_code=self.receive_message(client_sock)[0]
cursor=self.conn.cursor()
sql1="select course_name from student_courses where user_name==? and course_code==?"
try:
cursor.execute(sql1,(user_name,course_code,))
res=cursor.fetchone()
if len(res):
self.send_message("ok",client_sock)
self.send_message(f"Welcome to Course {res[0]} with course_code {course_code}",client_sock)
self.all_details(user_name,client_sock,course_code)
else:
self.send_message("not ok",client_sock)
self.send_message("Input correct code",client_sock)
self.home_student(user_name,client_sock)
except Exception as e:
print(e)
cursor.close()
self.home_student(user_name,client_sock)
def all_details(self,user_name,client_sock,course_code):
self.send_message("please select 1 for seeing posts and 2 for GD and 3 for chat session and 4 for private conversation with teacher",client_sock)
value = self.receive_message(client_sock)[0]
if(int(value)==1):
sql1="select keyword,post_details,add_time from posts where course_code == ? order by add_time desc"
# conn=se
try:
cursor=self.conn.cursor()
cursor.execute(sql1,(course_code,))
res=cursor.fetchall()
string ="keyword post_details add_time\n"
for i in res:
# print(i)
string+=" ".join([j for j in i]) +"\n"
self.send_message(string,client_sock)
except Exception as e:
self.send_message("Not able to see post")
print(e)
elif int(value)==2:
self.view_gd(course_code,client_sock,user_name)
elif int(value)==3:
self.chat_session_student(user_name,course_code,client_sock)
elif int(value)==4:
self.private_conv(user_name,client_sock,course_code)
cursor.close()
self.home_student(user_name,client_sock)
def private_conv(self,user_name,client_sock,course_code):
cursor=self.conn.cursor()
try:
cursor.execute("select user_name from courses where course_code==?",(course_code,))
res=cursor.fetchone()
teacher_name=res[0]
except:
self.send_message("Not able to fetch teacher's name",client_sock)
self.home_student(user_name,client_sock)
return
self.send_message("Please select 1 to view message and 2 to send message",client_sock)
choose=self.receive_message(client_sock)[0]
# print(teacher_name,type(teacher_name))
if choose=='1':
try:
cursor.execute("select * from private_conv where student_name==? and teacher_name == ? and course_code==?",(user_name,teacher_name,course_code,))
res=cursor.fetchall()
string="Message time\n"
for i in res:
string+=i[2]+" "+i[3]+"\n"
self.send_message(string,client_sock)
except Exception as e:
self.send_message("No convo",client_sock)
print(e)
else:
"""insert name of sender"""
try:
msg=self.receive_message(client_sock)[0]
msg=f"{user_name}--->"+msg
cursor.execute("insert into private_conv values(?,?,?,datetime(\'now\', \'localtime\'),?)",(user_name,teacher_name,msg,course_code,))
self.conn.commit()
self.send_message("ok",client_sock)
except Exception as e:
self.send_message("No",client_sock)
print(e)
cursor.close()
self.home_student(user_name,client_sock)
def view_gd(self,course_code,client_sock,user_name):
self.send_message("Please input topic",client_sock)
topic=self.receive_message(client_sock)[0]
cursor=self.conn.cursor()
try:
cursor.execute("select * from group_discussion where course_code==? and topic==?",(course_code,topic,))
res=cursor.fetchall()
if len(res)<=0:
self.send_message("not",client_sock)
self.home_student(user_name,client_sock)
return
except Exception as e:
print(e)
self.send_message("not",client_sock)
self.home_student(user_name,client_sock)
return
self.send_message("ok",client_sock)
self.send_message("input 1 for viewing messages in gd and 2 for adding messages to gd",client_sock)
v=int(self.receive_message(client_sock)[0])
sql1="select user_name,message,post_time from group_discussion where course_code == ? and topic == ?"
sql2='insert into group_discussion("topic","course_code","user_name","message","post_time") values(?,?,?,?,datetime(\'now\', \'localtime\'))'
if v==1:
try:
cursor.execute(sql1,(course_code,topic,))
res=cursor.fetchall()
string = "user_name message post_time\n"
for i in res:
string+=i[0]+" "+i[1]+" "+i[2]+"\n"
self.send_message(string,client_sock)
except Exception as E:
print(E)
self.send_message("Not able to fetch",client_sock)
elif v==2:
self.send_message("please type message to add to gd",client_sock)
temp= self.receive_message(client_sock)
message = temp[0]
try:
cursor.execute(sql2,(topic,course_code,user_name,message,))
self.conn.commit()
self.send_message("ok",client_sock)
except Exception as E:
print(E)
self.send_message("not ok",client_sock)
cursor.close()
self.home_student(user_name,client_sock)
return
def chat_session_student(self,user_name,course_code,client_sock):
course_code=int(course_code)
print("ahbdjsfkn")
if course_code not in self.chat_details:
self.send_message("nok",client_sock)
self.send_message("No active chat session",client_sock)
else:
self.send_message("ok",client_sock)
self.send_message("inside chat session",client_sock)
self.chat_details[course_code]["clients"].append(client_sock)
self.broadcast("i joined the chat session",client_sock,course_code,user_name)
while(True):
print('1')
message = self.receive_message(client_sock)[0]
if(message == "exit"):
self.broadcast("i am leaving the session",client_sock,course_code,user_name)
self.chat_details[course_code]["clients"].remove(client_sock)
elif(message == "showall"):
final = ""
message = self.chat_details[course_code]["messages"]
for i in message:
final+=i+"\n"
self.send_message(final,client_sock)
else:
self.broadcast(message,client_sock,course_code,user_name)
self.home_student(user_name,client_sock)
def view_updates(self,user_name,client_sock):
sql="select * from posts where (select course_code from student_courses where user_name==?) order by add_time desc limit 10"
cursor=self.conn.cursor()
try:
tmp=(user_name,)
cursor.execute(sql,tmp)
res=cursor.fetchall()
print(res)
string="course_code post_details keyword add_time \n"
for i in res:
string+= str(i[1])+" "+i[2]+i[3]+i[4]+"\n"
self.send_message(string,client_sock)
except Exception as e:
print(e)
self.send_message("Failed to show courses",client_sock)
cursor.close()
self.home_student(user_name,client_sock)
def view_courses_student(self, user_name,client_sock):
try:
cursor=self.conn.cursor()
tmp=(user_name,)
cursor.execute('select * from student_courses where user_name== ?',tmp)
res=cursor.fetchall()
string="course_code course_name\n"
for i in res:
string+= str(i[1])+" "+i[2]+"\n"
self.send_message(string,client_sock)
except:
self.send_message("Failed to show courses",client_sock)
cursor.close()
self.home_student(user_name,client_sock)
def enroll_course(self,user_name,client_sock):
self.send_message("please input course code to register",client_sock)
course_code = int(self.receive_message(client_sock)[0])
cursor=self.conn.cursor()
tmp=(course_code,)
cursor.execute('select * from courses where course_code== ?',tmp)
course_details=cursor.fetchall()
if len(course_details)==0:
self.send_message("not ok",client_sock)
self.send_message("No Course Available",client_sock)
return self.home_student(user_name,client_sock)
teacher = course_details[0][2]
course_name = course_details[0][1]
course_code= course_details[0][0]
try:
tmp=(user_name,course_code,course_name,)
cursor.execute('insert into student_courses("user_name","course_code","course_name") values(?,?,?)',tmp)
self.conn.commit()
self.send_message("ok",client_sock)
self.send_message("succefully enrolled to the course",client_sock)
except Exception as e :
print(e)
self.send_message("not ok",client_sock)
self.send_message("Failed to enroll to the courses",client_sock)
cursor.close()
self.home_student(user_name,client_sock)
# cursor=self.conn.cursor()
# try:
# tmp=(username)
# cursor.executemany(f'select * from student_courses where user_name== ?',tmp)
# res=cursor.fetchall()
# self.send_message("".join(res),client_sock)
# except:
# self.send_message("Failed to show courses",client_sock)
# pass
# def back_to_home_student(self,username):
# pass
# def back_to_home_teacher(self,username):
# pass
# def add_post(self,username,post,course_code):
# pass
# def view_post_student(self,username,course_code):
# pass
# def create_assignment(self, user_name,course_code):
# pass
# self.functions(values[1],values[2])
# def connect(self,client_sock):
# if(first):
# msg_rcvd=self.cons_msg(client_sock)
# if(msg_rcvd==1):
# if self.register(client_sock):
# check = self.login(client_sock)
# if(check[0]==True):
# self.functions(client_sock,check[1],check[2])
# else:
# self.connect(client_sock,first=0)
# else:
# self.connect(client_sock,first=0)
# elif msg_rcvd==2:
# if(self.login(client_sock)):
# self.functions(client_sock)
# else:
# self.connect(client_sock,first=0,msg_rcvd=2)
# return
# def add_course(self,client_sock):
# post,username =client_sock.recv(chunk_size).decode().split(sep)
# def add_post(self,client_sock):
# post,username =client_sock.recv(chunk_size).decode().split(sep)
# try:
# cursor=self.conn.cursor()
# except :
# pass
# pass
if __name__ == "__main__":
s=Server()