-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyclient.py
executable file
·87 lines (68 loc) · 2.67 KB
/
myclient.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
#!/usr/bin/env python3
"""Run the client."""
import socket
import sys
from threading import Thread
import random
import time
SERVER_IP = "192.168.122.2"
SERVER_PORT = 8008
BUFFER_SIZE = 2048
ips = [SERVER_IP]
class Client:
"""Create the client and functions to connect with server."""
def __init__(self, host, port, buffer_size=2048):
"""Initialize the client object with host, port and buffer_size."""
self.host = host
self.port = port
self.buffer_size = buffer_size
self._socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
def connect(self):
"""Connect to the client object with host, and port."""
self._socket.connect((self.host, self.port))
def request(self, message):
"""Send request message to the server."""
self._socket.send(f"{message}".encode())
def response(self):
"""Recieve response from the server."""
return self._socket.recv(self.buffer_size).decode()
def handle_connected(thread_num, busy_time):
"""Send request and recieve response from server."""
global ips
while True:
time.sleep(1)
try:
client = Client(random.choice(ips), SERVER_PORT, BUFFER_SIZE)
client.connect()
print(f"[Thread{thread_num}]: Connecting to server {client.host} ... [OK]")
client.request(busy_time)
print(f"[Thread{thread_num}]: Sending message to server {client.host} ... [OK]")
response = client.response()
print(f"[Thread{thread_num}]: Recieved response from server {client.host} ... [OK]")
print(f"[Thread{thread_num}]: Response from {client.host}: {response}")
except Exception as e:
print(f"ERROR: {e}")
def handle_autoscaler_connection():
"""Recieve information from autoscaler."""
global ips
autoscaler_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
autoscaler_socket.bind(("127.0.0.1", 6009))
autoscaler_socket.listen(10)
conn, _ = autoscaler_socket.accept()
new_ip = conn.recv(BUFFER_SIZE).decode()
print(f"[AUTOSCALER]: Recieved new ip: {new_ip}")
ips.append(new_ip)
def main():
"""Run the code."""
try:
thread_count = int(sys.argv[1])
busy_time = int(sys.argv[2])
autoscalerThread = Thread(target=handle_autoscaler_connection)
autoscalerThread.start()
for i in range(thread_count):
clientThread = Thread(target=handle_connected, args=(i+1, busy_time))
clientThread.start()
except Exception as error:
print(f"ERROR: Please type thread count and busy time as arguments. {error}")
if __name__ == "__main__":
main()