def run(self): """ This function executes the client requests in multithreaded fashion. Controls the transfer of bytes between client and server. First, the length of the request is received which in turn is used to receive the request data. This request data is passed to the requestHandler function and we obtain the response. This response is then further forwarded to the respective client in the same manager as the request was received by the server from the client. """ databytes = b"" to_recieve = 1024 while len(databytes) < to_recieve: bytes_read = self.obj.client_socket.recv(to_recieve - len(databytes)) databytes += bytes_read request_data_length = int(databytes.decode("utf-8").strip()) databytes = b"" to_recieve = request_data_length while len(databytes) < to_recieve: bytes_read = self.obj.client_socket.recv(to_recieve - len(databytes)) databytes += bytes_read request_data = databytes.decode("utf-8") request = Request.from_json(request_data) response = self.obj.requestHandler(request) response_data = response.to_json() self.obj.client_socket.sendall( bytes(str(len(response_data)).ljust(1024), encoding="utf-8")) self.obj.client_socket.sendall(bytes(response_data, "utf-8")) self.obj.client_socket.close()
def run(self): data_bytes = b'' to_receive = 1024 while len(data_bytes) < to_receive: bytes_read = self.client_socket.recv(to_receive - len(data_bytes)) data_bytes += bytes_read request_data_length = int(data_bytes.decode("utf-8").strip()) data_bytes = b'' to_receive = request_data_length while len(data_bytes) < to_receive: bytes_read = self.client_socket.recv(to_receive - len(data_bytes)) data_bytes += bytes_read request_data = data_bytes.decode("utf-8") request = Request.from_json(request_data) response = self.requestHandler(request) response_data = response.to_json() self.client_socket.sendall( bytes(str(len(response_data)).ljust(1024), 'utf-8')) self.client_socket.sendall(bytes(response_data, 'utf-8')) self.client_socket.close()
from common.hr import Designation from network_common.wrappers import Request, Wrapper r1 = Request("DesignationManager", "getDesignationByCode", Wrapper(10)) str = r1.to_json() print(str) print("*" * 25) r2 = Request.from_json(str) print("Manager : ", r2.manager) print("Action : ", r2.action) print("JSON String : ", r2.json_string) print("*" * 25) value = Wrapper.from_json(r2.json_string) print(value, type(value))