-
Notifications
You must be signed in to change notification settings - Fork 0
/
quiz_participant.py
66 lines (59 loc) · 1.47 KB
/
quiz_participant.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
from time import sleep
import socket
import pickle
'''
OP Codes
0 - Information
1 - Quiz Starting
2 - Sending Question
3 - Sending Answers
4 - Request Answer
5 - Sending Answer
6 - Feedback
7 - Quiz over
EOT - End of transmission
'''
quiz_server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
quiz_server.connect(("0.0.0.0", 2065))
status = "Start"
END_CHAR = chr(8).encode()
score = 0
while status != "END":
receiving = True
message = b''
while receiving:
incoming = quiz_server.recv(1)
if incoming == END_CHAR:
receiving = False
else:
message += incoming
quiz_server.send(chr(6).encode())
data = pickle.loads(message)
if data[0] == 0:
print(data[1])
elif data[0] == 1:
print("Quiz Starting")
elif data[0] == 2:
print("Question incoming")
print(data[1])
elif data[0] == 3:
answers = data[1]
for i in range(len(answers)):
print(str(i) + ". " + answers[i])
elif data[0] == 4:
print('Enter the number of your answer')
my_answer = input(">")
elif data[0] == 5:
feedback = True
if my_answer == data[1]:
print("You were right")
score += 1
else:
print("You were wrong")
feedback = False
reply = (6, feedback)
reply = pickle.dumps(reply)
quiz_server.send(reply)
elif data[0] == 7:
print("Quiz Over")
status = "END"