-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
58 lines (54 loc) · 2.11 KB
/
client.py
File metadata and controls
58 lines (54 loc) · 2.11 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
import socket
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(('localhost', 4000))
print(client.recv(1024).decode())
lastQuestionChecked = 0
while True:
message = input('>')
# Handle the message command
match message:
case "post":
problem = input('Enter the problem: ')
alternative1 = input('Enter the first alternative: ')
alternative2 = input('Enter the second alternative: ')
alternative3 = input('Enter the third alternative: ')
client.send(f"POST_PROBLEM;{problem};{alternative1};{alternative2};{alternative3}".encode())
case "list":
client.send("GET_PROBLEMS".encode())
case "get":
questionId = input('Enter the question ID: ')
client.send(f"GET_PROBLEM;{questionId}".encode())
case "vote":
questionId = input('Enter the question ID: ')
alternative = input('Enter the alternative: ')
client.send(f"VOTE;{questionId};{alternative}".encode())
case "help":
client.send("help".encode())
# Read all incoming messages
while True:
try:
client.settimeout(0.5)
response = client.recv(1024).decode()
parts = response.split(';')
match parts[0]:
case "PROBLEMS":
print(f"{parts[1]}: {parts[2]}")
case "PROBLEM":
print(parts[2])
print(f"1. {parts[3]}, Votes: {parts[4]}")
print(f"2. {parts[5]}, Votes: {parts[6]}")
print(f"3. {parts[7]}, Votes: {parts[8]}")
case "PROBLEM_CREATED":
print(f"Problem created with ID: {parts[1]}")
case "VOTE_SUCCESS":
print("Voted")
case "COMMANDS":
for part in parts[1:]:
print(part)
case "ERROR":
print(parts[1])
if not response:
break
except socket.timeout:
break
client.settimeout(None)