You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

49 lines
1.6 KiB

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}"
else:
print("Comando no reconocido")
message = command
s.sendall(message.encode('utf-8'))
break

Powered by INFORMATICA.FP.EDU.ES.