-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
71 lines (51 loc) · 2.04 KB
/
client.py
File metadata and controls
71 lines (51 loc) · 2.04 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
import socket
import threading
def connect_to_server(host, port):
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect((host, port))
return client_socket
def receive_data(client_socket):
data = client_socket.recv(1024).decode()
return data
def send_command(client_socket, command):
client_socket.sendall(command.encode())
def process_command_from_server(command):
command = command[2:-2]
print("[Received] received command{} from server:".format(command))
print("[Executing] i am{}ing watch me{}".format(command, command))
def receive_and_execute_command(client_socket):
data = receive_data(client_socket)
rank = int(data.split()[-1])
if data.startswith("R"):
updated_rank = int(data.split()[-1])
if (updated_rank != rank):
print("\nMy new rank is {}".format(updated_rank))
print("\nEnter command: ")
rank = updated_rank
elif int(data.split()[0]) > rank:
print("\nRejected: Cannot execute command from higher rank client")
print("\nEnter command: ")
else:
# process the received command
print("\n[Processing]......")
process_command_from_server(data)
print("\nEnter command: ")
def listen_for_commands(sock):
while True:
receive_and_execute_command(sock)
def prompt_for_command(client_socket):
command = input("Enter command: ")
send_command(client_socket, "C {} {}".format(rank, command))
if __name__ == '__main__':
host = 'localhost' # server address
port = 5555 # server port
client_socket = connect_to_server(host, port)
rank = int(client_socket.recv(1024).decode().split()[-1])
print("Assigned rank: {}".format(rank))
# start the listen_for_commands thread
listen_thread = threading.Thread(target=listen_for_commands, args=(client_socket,))
listen_thread.start()
# main loop for user input
prompt = True
while prompt:
prompt_for_command(client_socket)