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 = 2002 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("Conexión rechazada, intente más tarde.") sys.exit() message = "Conectando!" print('{!r}'.format(message)) s.sendall(message.encode('utf-8')) data = s.recv(1024) print("Received data from server: " + data.decode('utf-8')) if data.decode('utf-8') == "Maximo numero de conexiones alcanzado. Por favor intente mas tarde.": print("Conexiones máximas alcanzadas. Por favor intente más tarde.") sys.exit() while True: command = input("Entra comando (retirar o 'exit' para salir): ") if command.lower() == 'exit': s.sendall(command.encode('utf-8')) time.sleep(1) break elif command.lower() == 'retirar': amount = input("Entera la cantidad de dinero: ") message = f"{command} {amount}" s.sendall(message.encode('utf-8')) data = s.recv(1024) if data.decode('utf-8') == "Saldo insuficiente por favor intente mas tarde.": print("No hay suficientes fondos. Por favor intente más tarde.") break else: print("Comando no reconocido") message = command s.sendall(message.encode('utf-8')) break