feat(Protocol): adjust to support twice \n in protocol.
This commit is contained in:
parent
55191ee728
commit
131808a2c3
|
@ -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'
|
||||
|
|
|
@ -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())
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue