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.
36 lines
1.4 KiB
36 lines
1.4 KiB
7 months ago
|
import socket
|
||
|
|
||
|
HOST = ''
|
||
|
PORT = 2005
|
||
|
MAX_CONNECTIONS = 2
|
||
|
MAX_DINERO = 120000
|
||
|
connections = 0
|
||
|
#pip install six
|
||
|
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
||
|
s_addr = (HOST, PORT)
|
||
|
s.bind(s_addr)
|
||
|
s.listen(MAX_CONNECTIONS)
|
||
|
while True:
|
||
|
if connections >= MAX_CONNECTIONS:
|
||
|
print('Maximo de conexiones alcanzado, esperando a que se libere una conexion.')
|
||
|
continue
|
||
|
client_socket, addr = s.accept()
|
||
|
connections += 1 # Increment connections after the check
|
||
|
with client_socket:
|
||
|
while True:
|
||
|
datos = client_socket.recv(1024)
|
||
|
if not datos:
|
||
|
connections -= 1
|
||
|
break
|
||
|
print('Cliente connectetado y datos enviados : {} bytes de {}'.format(len(datos), addr))
|
||
|
command, amount = datos.decode('utf-8').split()
|
||
|
amount = int(amount)
|
||
|
if command.lower() == 'retirar':
|
||
|
print('retiro exitoso')
|
||
|
if amount <= MAX_DINERO:
|
||
|
MAX_DINERO -= amount
|
||
|
response = f"Retiro Existoso."
|
||
|
else:
|
||
|
response = "Saldo insuficiente por favor intente mas tarde."
|
||
|
client_socket.sendall(response.encode('utf-8'))
|
||
|
print('enviados {} bytes de vuelta a {}'.format(len(response), addr))
|