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.
54 lines
1.9 KiB
54 lines
1.9 KiB
9 months ago
|
import socket
|
||
9 months ago
|
import time
|
||
|
import sys
|
||
9 months ago
|
|
||
9 months ago
|
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()
|
||
9 months ago
|
PORT = 2002
|
||
9 months ago
|
|
||
|
while True:
|
||
|
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
||
|
server_address = (HOST, PORT)
|
||
9 months ago
|
try:
|
||
|
s.connect(server_address)
|
||
|
except ConnectionRefusedError:
|
||
9 months ago
|
print("Conexión rechazada, intente más tarde.")
|
||
|
sys.exit()
|
||
9 months ago
|
message = "Conectando!"
|
||
|
print('{!r}'.format(message))
|
||
9 months ago
|
s.sendall(message.encode('utf-8'))
|
||
9 months ago
|
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:
|
||
9 months ago
|
command = input("Entra comando (retirar o 'exit' para salir): ")
|
||
|
if command.lower() == 'exit':
|
||
9 months ago
|
s.sendall(command.encode('utf-8'))
|
||
|
time.sleep(1)
|
||
9 months ago
|
break
|
||
|
elif command.lower() == 'retirar':
|
||
|
amount = input("Entera la cantidad de dinero: ")
|
||
|
message = f"{command} {amount}"
|
||
9 months ago
|
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
|
||
9 months ago
|
else:
|
||
|
print("Comando no reconocido")
|
||
9 months ago
|
message = command
|
||
9 months ago
|
s.sendall(message.encode('utf-8'))
|
||
9 months ago
|
break
|