#!/usr/bin/env python3.8
import socket, time

SERVER_ADDRESS = '192.168.0.5'
SERVER_PORT = 22222

s = socket.socket()
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((SERVER_ADDRESS, SERVER_PORT))
s.listen(5)

print("Listening on address %s. Kill server with Ctrl-C" %
      str((SERVER_ADDRESS, SERVER_PORT)))

while True:
    c, addr = s.accept()
    print("\nConnection received from %s" % str(addr))

    while True:

        data = c.recv(2048)
        if not data:
            print("End of file from client. Resetting")
            break

        print(data.decode())

        analisis = data.decode()
        separar = analisis.split(',')

        operacion = separar[0]
        factor1 = separar[1]
        factor2 = separar[2]

        if operacion == 'SUMA':
            data3 = int(factor1) + int(factor2)
            data3 = 'SUMANDO:'+','+str(data3)+','+'FIN'
        elif operacion == 'MULTIPLICA':
            data3 = int(factor1) * int(factor2)
            data3 = 'MULTIPLICANDO:'+','+str(data3)+','+'SIGUE'
        elif operacion == 'DIVIDE':
            data3 = int(factor1) / int(factor2)
            data3 = 'DIVIDIENDO:'+','+str(data3)+','+'FIN'
        elif operacion == 'RESTA':
            data3 = int(factor1) - int(factor2)
            data3 = 'RESTANDO:'+','+str(data3)+','+'FIN'


        data3 = data3.encode()

        c.send(data3)
        print('ENVIANDO INFORMACION')
        #break
        time.sleep(1)

    c.close()