import socket import time import sys def get_ip_address(): s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) try: s.connect(('10.255.255.255', 1)) IP = s.getsockname()[0] except Exception: IP = '127.0.0.1' finally: s.close() return IP HOST = get_ip_address() PORT = 2005 while True: with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: server_address = (HOST, PORT) try: s.connect(server_address) except ConnectionRefusedError: print("Connection refused, try again later.") sys.exit() # Terminate the program # Send a message immediately after connecting message = "Conectando!" print('{!r}'.format(message)) s.sendall(message.encode('utf-8')) print('Esperando la respuesta') while True: # Keep the connection open until the user enters a command command = input("Entra comando (retirar o 'exit' para salir): ") if command.lower() == 'exit': break elif command.lower() == 'retirar': amount = input("Entera la cantidad de dinero: ") message = f"{command} {amount}" else: print("Comando no reconocido") message = command # Send the unrecognized command to the server s.sendall(message.encode('utf-8'))