feat(Protocol): adjust to support twice \n in protocol.
This commit is contained in:
parent
55191ee728
commit
131808a2c3
|
@ -1,26 +1,22 @@
|
||||||
|
|
||||||
class Calculator:
|
class Calculator:
|
||||||
def __init__(self, client):
|
def __init__(self, client, string_control='\n'):
|
||||||
self.client = client
|
self.client = client
|
||||||
|
self.string_control = string_control
|
||||||
|
|
||||||
def calc(self, operation):
|
def calc(self, operation):
|
||||||
fixed_operation = self.fix_operation(operation)
|
fixed_operation = operation + self.string_control
|
||||||
self.client.connect()
|
self.client.connect()
|
||||||
self.client.send(
|
self.client.send(
|
||||||
fixed_operation
|
fixed_operation
|
||||||
)
|
)
|
||||||
response = self.client.getText(512)
|
response = self.client.getText(
|
||||||
|
self.string_control
|
||||||
|
)
|
||||||
self.client.close()
|
self.client.close()
|
||||||
result = Result(response)
|
result = Result(response)
|
||||||
return result.get_content()
|
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:
|
class Result:
|
||||||
ok = 'OK'
|
ok = 'OK'
|
||||||
error = 'ERROR'
|
error = 'ERROR'
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
import socket
|
import socket
|
||||||
|
|
||||||
class ClientJorobany:
|
class ClientJorobany:
|
||||||
|
coding = 'utf-8'
|
||||||
|
|
||||||
def __init__(self, host, port):
|
def __init__(self, host, port):
|
||||||
self.host = host
|
self.host = host
|
||||||
self.port = port
|
self.port = port
|
||||||
|
@ -9,11 +11,15 @@ class ClientJorobany:
|
||||||
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
self.socket.connect((self.host, self.port))
|
self.socket.connect((self.host, self.port))
|
||||||
|
|
||||||
def getText(self, buffersize):
|
def getText(self, string_control):
|
||||||
binaryText = self.socket.recv(buffersize)
|
buffersize=len(string_control)
|
||||||
if binaryText:
|
complete_text = ''
|
||||||
return binaryText.decode('utf-8')
|
while True:
|
||||||
return ''
|
text = self.socket.recv(buffersize).decode(self.coding)
|
||||||
|
if text == string_control:
|
||||||
|
break
|
||||||
|
complete_text += text
|
||||||
|
return complete_text
|
||||||
|
|
||||||
def send(self, data):
|
def send(self, data):
|
||||||
self.socket.sendall(data.encode())
|
self.socket.sendall(data.encode())
|
||||||
|
|
|
@ -1,22 +1,26 @@
|
||||||
from calculator import Calculator
|
from calculator import Calculator
|
||||||
from client_jorobany import ClientJorobany
|
from client_jorobany import ClientJorobany
|
||||||
|
|
||||||
host = 'localhost'
|
host = 'mob.bit4bit.in'
|
||||||
port = 65517
|
port = 65519
|
||||||
|
#port = 65520
|
||||||
|
#string_control = '\n\n'
|
||||||
|
string_control = '\n'
|
||||||
client = ClientJorobany(host, port)
|
client = ClientJorobany(host, port)
|
||||||
calculator = Calculator(client)
|
calculator = Calculator(client, string_control)
|
||||||
|
|
||||||
def sanitize_result(result):
|
def sanitize_result(result):
|
||||||
target = '\n'
|
target = string_control
|
||||||
if result and result[-1] == target:
|
if result and result[-1] == target:
|
||||||
return result[:-1]
|
return result[:-1]
|
||||||
return result
|
return result
|
||||||
|
|
||||||
input_message = "Ingrese operacion (q para salir): "
|
input_message = "Ingrese operacion (q para salir): "
|
||||||
|
|
||||||
user_input = input(input_message);
|
user_input = input(input_message)
|
||||||
while not user_input == 'q':
|
while not user_input == 'q':
|
||||||
result = calculator.calc(user_input)
|
operation = user_input
|
||||||
|
result = calculator.calc(operation)
|
||||||
|
# print(result)
|
||||||
print(sanitize_result(result))
|
print(sanitize_result(result))
|
||||||
user_input = input(input_message)
|
user_input = input(input_message)
|
||||||
|
|
Loading…
Reference in New Issue