feat(calculator): initial version.

This commit is contained in:
Francisco 2023-04-22 12:54:05 -05:00
parent 19cf45e387
commit 400a8af900
6 changed files with 173 additions and 0 deletions

43
calculator.py Normal file
View File

@ -0,0 +1,43 @@
class Calculator:
def __init__(self, client):
self.client = client
def calc(self, operation):
fixed_operation = self.fix_operation(operation)
self.client.connect()
self.client.send(
fixed_operation
)
response = self.client.getText(512)
self.client.close()
result = Result(response)
# if result.has_error():
# raise Exception(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:
ok = 'OK'
error = 'ERROR'
separator = ':'
def __init__(self, response_text):
self.response = response_text
self.splited_response = self.response.split(self.separator)
def has_error(self):
if not self.splited_response[0] == self.ok:
return True
return False
def get_content(self):
return self.separator.join(
self.splited_response[1:]
)

22
client_jorobany.py Normal file
View File

@ -0,0 +1,22 @@
import socket
class ClientJorobany:
def __init__(self, host, port):
self.host = host
self.port = port
def connect(self):
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 send(self, data):
self.socket.sendall(data.encode())
def close(self):
self.socket.close()

13
exercise.py Normal file
View File

@ -0,0 +1,13 @@
from calculator import Calculator
from client_jorobany import ClientJorobany
host = 'localhost'
port = 65517
operation = '2+3'
expected_result = '5\n'
client = ClientJorobany(host, port)
calculator = Calculator(client)
result = calculator.calc(operation)
assert result == expected_result

46
server.py Normal file
View File

@ -0,0 +1,46 @@
import socket
import threading
import re
# protocolo
# operacion: valor + operando + valor\n
# OK:<resultado>\n
# ERROR:<mensaje>\n
def validate_pdu(pdu):
return re.match(r'\d+\s*[-+*/]\s*\d+', pdu) != None
def handle_connection(conn):
try:
r = conn.makefile('rw')
while True:
frame = r.readline()
pdu = frame.split()
command = ''.join(pdu)
print('got %s' % (frame))
if validate_pdu(command):
try:
r.writelines(["OK:%s\n" % (eval(command))])
except:
r.writelines(["ERR:exception\n"])
else:
r.writelines(["ERROR:😒yntax error\n"])
r.flush()
except:
pass
conn.close()
# MAIN
PORT=65517
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(('', PORT))
s.listen(12)
while True:
conn, addr = s.accept()
print('Connected by', addr)
t = threading.Thread(target=handle_connection, args=(conn,))
t.start()

27
socket_excercise.py Normal file
View File

@ -0,0 +1,27 @@
import socket
class ClientJorobany:
def __init__(self, host, port):
self.host = host
self.port = port
def connect(self):
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 send(self, data):
self.socket.sendall(data)
def close(self):
self.socket.close()
# c = ClientJorobany('mob.bit4bit.in', 65303)
# c.connect()
# print(c.getText(512))
# c.close()

22
user_calculator.py Normal file
View File

@ -0,0 +1,22 @@
from calculator import Calculator
from client_jorobany import ClientJorobany
host = 'localhost'
port = 65517
client = ClientJorobany(host, port)
calculator = Calculator(client)
def sanitize_result(result):
target = '\n'
if result and result[-1] == target:
return result[:-1]
return result
input_message = "Ingrese operacion (q para salir): "
user_input = input(input_message);
while not user_input == 'q':
result = calculator.calc(user_input)
print(sanitize_result(result))
user_input = input(input_message)