From 131808a2c30a8e6a71c0569c70770c86f647219c Mon Sep 17 00:00:00 2001 From: Francisco Date: Sat, 29 Apr 2023 15:47:46 -0500 Subject: [PATCH] feat(Protocol): adjust to support twice \n in protocol. --- calculator.py | 16 ++++++---------- client_jorobany.py | 16 +++++++++++----- user_calculator.py | 18 +++++++++++------- 3 files changed, 28 insertions(+), 22 deletions(-) diff --git a/calculator.py b/calculator.py index 0409411..7549680 100644 --- a/calculator.py +++ b/calculator.py @@ -1,26 +1,22 @@ class Calculator: - def __init__(self, client): + def __init__(self, client, string_control='\n'): self.client = client + self.string_control = string_control def calc(self, operation): - fixed_operation = self.fix_operation(operation) + fixed_operation = operation + self.string_control self.client.connect() self.client.send( fixed_operation ) - response = self.client.getText(512) + response = self.client.getText( + self.string_control + ) self.client.close() result = Result(response) return result.get_content() - def fix_operation(self, operation): - if not operation: - return '\n' - if operation[-1] != '\n': - return operation + '\n' - return operation - class Result: ok = 'OK' error = 'ERROR' diff --git a/client_jorobany.py b/client_jorobany.py index 6b79189..4fb514e 100644 --- a/client_jorobany.py +++ b/client_jorobany.py @@ -1,6 +1,8 @@ import socket class ClientJorobany: + coding = 'utf-8' + def __init__(self, host, port): self.host = host self.port = port @@ -9,11 +11,15 @@ class ClientJorobany: self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.socket.connect((self.host, self.port)) - def getText(self, buffersize): - binaryText = self.socket.recv(buffersize) - if binaryText: - return binaryText.decode('utf-8') - return '' + def getText(self, string_control): + buffersize=len(string_control) + complete_text = '' + while True: + text = self.socket.recv(buffersize).decode(self.coding) + if text == string_control: + break + complete_text += text + return complete_text def send(self, data): self.socket.sendall(data.encode()) diff --git a/user_calculator.py b/user_calculator.py index d80e6eb..93cf1a0 100644 --- a/user_calculator.py +++ b/user_calculator.py @@ -1,22 +1,26 @@ from calculator import Calculator from client_jorobany import ClientJorobany -host = 'localhost' -port = 65517 - +host = 'mob.bit4bit.in' +port = 65519 +#port = 65520 +#string_control = '\n\n' +string_control = '\n' client = ClientJorobany(host, port) -calculator = Calculator(client) +calculator = Calculator(client, string_control) def sanitize_result(result): - target = '\n' + target = string_control if result and result[-1] == target: return result[:-1] return result input_message = "Ingrese operacion (q para salir): " -user_input = input(input_message); +user_input = input(input_message) while not user_input == 'q': - result = calculator.calc(user_input) + operation = user_input + result = calculator.calc(operation) +# print(result) print(sanitize_result(result)) user_input = input(input_message)