-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
174 lines (132 loc) · 4.29 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
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
import socket
# connection settings
HOST = '127.0.0.1'
PORT = 9999
# starts the client process
def init():
# connects to the server
c_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
c_socket.connect((HOST, PORT))
while True:
choice_display()
choice = input(' ')
# exits the client
if choice == '8':
print('See you later... Alligator.')
break
success = process_choice(choice, c_socket)
# if the form entry was a success, send it to the server;
# otherwise, wait for more input
if success:
result = c_socket.recv(1024)
process_result(choice, result.decode())
else:
print("\nInvalid operation: Please try again.", end="\n\n")
input('Press ENTER to continue...')
c_socket.close()
# displays the main choices menu
def choice_display():
print('''
Python DB Menu - Deluxe
1. Find Customer
2. Add Customer
3. Delete customer
4. Update customer age
5. Update customer address
6. Update customer phone
7. Print report
8. Exit
Select:''', end='')
# processes the server's find_customer response
def process_find_customer(raw_result: str):
if raw_result == 'None':
print('Not found.')
else:
name, age, address, phone = raw_result.split('|')
print(f'''
Found Entry
-----------
Name: {name}
Age: {age}
Address: {address}
Phone #: {phone}
''')
# processes the server's add_customer response
def process_add_customer(raw_result: str):
if raw_result == 'None':
print('Customer already exists.')
else:
print('Successfully added customer.')
# processes the server's delete_customer response
def process_delete_customer(raw_result: str):
if raw_result == 'None':
print('Customer does not exist.')
else:
print('Successfully deleted customer.')
# processes the server's update_customer response
def process_update_customer(raw_result: str):
if raw_result == 'None':
print('Customer does not exist.')
else:
print('Successfully updated customer.')
# processes the server's print_report response
def process_print_report(raw_result: str):
print('Customer DB Report\n------------------\n' + raw_result)
# processes the server's response choice
def process_result(choice: str, result: str):
if choice == '1':
process_find_customer(result)
elif choice == '2':
process_add_customer(result)
elif choice == '3':
process_delete_customer(result)
elif choice == '4' or choice == '5' or choice == '6':
process_update_customer(result)
elif choice == '7':
process_print_report(result)
# processes the user's find_customer response, returns true if the formed succeeded
def process_choice(choice: str, connection: socket.socket) -> bool:
if choice == '1':
name = input('Name? (case-sensitive) ')
connection.send((choice + '|' + name).encode())
return True
if choice == '2':
print('\nNew Customer')
print('------------')
name = input('Name? ')
age = input('Age? ')
address = input('Address? ')
phone = input('Phone #? ')
if name == '':
return False
connection.send(
(choice + '|' + f"{name},{age},{address},{phone}").encode())
return True
if choice == '3':
name = input('Name to delete? (case-sensitive) ')
connection.send((choice + '|' + name).encode())
return True
if choice == '4' or choice == '5' or choice == '6':
print('\nUpdate Customer')
print('------------')
name = input('Name (case-sensitive)? ')
field_to_update = ''
if choice == '4':
field_to_update = input('Age? ')
elif choice == '5':
field_to_update = input('Address? ')
elif choice == '6':
field_to_update = input('Phone #? ')
else:
return False
if field_to_update == '':
return False
connection.send((choice + '|' + name + ',' + field_to_update).encode())
return True
if choice == '7':
connection.send((choice + '|').encode())
return True
if True:
return False
if __name__ == '__main__':
init()