-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
75 lines (67 loc) · 1.69 KB
/
client.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
import socket
import config
import message
import time
delay = None
def requestTickets(kiosk, tickets):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print("Attempting to connect to port: " + str(kiosk))
s.connect((str(kiosk[0]), int(kiosk[1])))
buy_message = message.BuyMessage(tickets)
s.send(buy_message.data)
response = s.recv(16)
time.sleep(delay)
success = message.Message.deserialize(response)
s.close()
return success
def cmdUI(cfg):
done = False
while not done:
#print information
i = 0
for k in cfg.kiosks:
print(str(i) + ". " + str(k))
i = i + 1
print(str(i) + ". exit")
#ask user for kiosk to connect to
try:
a_input = input("Please choose a kiosk or exit: ")
except NameError:
print("Invalid input: expected int")
continue
try:
a_input = int(a_input)
except ValueError:
print("Invalid input")
continue
if a_input < 0 or a_input > i:
print("Input out of range")
continue
if a_input == i:
done = True
break
myKiosk = a_input
#ask user for number of tickets
buytickets = input("How many tickets would you like to purchase? ")
try:
buytickets = int(buytickets)
except ValueError:
print("Invalid input")
continue
#request to purchase tickets from selected kiosk
response = requestTickets(cfg.kiosks[myKiosk], buytickets)
if response.success == message.BUY_SUCCESS:
print("Tickets purchased successfully")
elif response.success == message.BUY_FAIL:
print("Tickets not purchased")
else:
print("Error: unrecognized response")
def main():
print("Starting client...")
cfg = config.Config.from_file("config.txt")
global delay
delay = cfg.delay
cmdUI(cfg)
exit()
if __name__ == "__main__":
main()