Skip to content
This repository was archived by the owner on Apr 1, 2025. It is now read-only.

Commit ba4c581

Browse files
committed
protocol fix for correct testing
1 parent 15f1227 commit ba4c581

File tree

1 file changed

+121
-121
lines changed

1 file changed

+121
-121
lines changed

client/client.py

Lines changed: 121 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
11
import socket
22
import os
33
import re
4-
5-
# Configuración inicial
6-
FTP_SERVER_ADDR = '127.0.0.1' # Dirección del servidor
7-
FTP_CONTROL_PORT = 21 # Puerto del servidor FTP para manejar sesiones. Este valor no cambia
8-
PASV_MODE = 0 # Indicador de modo pasivo (0=inactivo 1=activo)
9-
DATA_SOCKET = None # Socket de transferencia utilizado para transferencia de datos
10-
BUFFER_SIZE = 1024
11-
TYPE = I
4+
import sys
125

136
# Funciones -------------------------------------------------------------------------------------------------------------------
147

@@ -21,9 +14,9 @@ def response(socket):
2114
break
2215
return response
2316

24-
def client_connects_to_server(socket):
25-
socket.connect((FTP_SERVER_ADDR, FTP_CONTROL_PORT))
26-
return response(socket)
17+
def client_connects_to_server(sock, server_addr, port):
18+
sock.connect((server_addr, port))
19+
return sock.recv(1024).decode()
2720

2821
def send(socket, message):
2922
socket.sendall(f"{message}\r\n".encode())
@@ -39,25 +32,15 @@ def default_login(socket):
3932
else:
4033
return (f"Error de autenticación: {response}")
4134

42-
def client_login(socket):
43-
username = input("Ingrese el nombre de usuario: ")
44-
if username == "":
45-
return default_login()
46-
47-
send(socket, f"USER {username}")
48-
password = getpass.getpass("Ingrese la contraseña: ")
49-
response = send(socket, f"PASS {password}")
50-
51-
req = input("Requiere una cuenta específica? s -> SI [otra tecla] -> NO: ")
52-
if req == "s" or req == "S":
53-
account = input("Ingrese su cuenta: ")
54-
acct_response = send(socket, f"ACCT {account}")
55-
return acct_response
35+
def client_login(sock, username, password):
36+
sock.sendall(f"USER {username}\r\n".encode())
37+
response = sock.recv(1024).decode()
5638

57-
elif "230" in response:
58-
return response
59-
else:
60-
return (f"Error de autenticación: {response}")
39+
if "331" in response:
40+
sock.sendall(f"PASS {password}\r\n".encode())
41+
response = sock.recv(1024).decode()
42+
43+
return response
6144

6245
def argument_handler(min_required_args, max_required_args, given_args):
6346
if min_required_args>given_args:
@@ -625,103 +608,120 @@ def cmd_SITE(socket, *args):
625608
else:
626609
return response
627610

611+
# Obtener argumentos manualmente
612+
def get_arg(flag):
613+
try:
614+
index = sys.argv.index(flag)
615+
return sys.argv[index + 1]
616+
except (ValueError, IndexError):
617+
return None
618+
628619
# Ejecución principal del cliente ---------------------------------------------------------------------------------------------
629620

630-
# Conexión inicial
631-
FTP_SERVER_ADDR = input("Ingrese la dirección IP del servidor FTP: ")
632-
socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
621+
host = get_arg("-h")
622+
port = int(get_arg("-p"))
623+
user = get_arg("-u")
624+
password = get_arg("-w")
625+
command = get_arg("-c").lower()
626+
a_arg = get_arg("-a")
627+
b_arg = get_arg("-b")
628+
629+
if not all([host, port, user, password, command]):
630+
print("Error: Host, puerto, usuario, contraseña y comando son obligatorios.")
631+
exit()
632+
633+
# Conexión al servidor
634+
ftp_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
633635
try:
634-
print(client_connects_to_server(socket))
636+
print(client_connects_to_server(ftp_socket, host, port))
635637
except Exception as e:
636638
print(f"Error al conectar con el servidor: {e}")
637639
exit()
638640

639-
# Autenticación de usuario
640-
while True:
641-
response = client_login(socket)
642-
print(response)
643-
if "230" in response:
644-
break
645-
else:
646-
print("Inténtelo de nuevo.")
641+
# Autenticación
642+
response = client_login(ftp_socket, user, password)
643+
print(response)
644+
if "230" not in response:
645+
print("Error de autenticación")
646+
exit()
647647

648-
# Ejecución de comandos del cliente
649-
while True:
650-
try:
651-
user_input = input("=> ")
652-
command_parts = user_input.strip().split(" ")
653-
command = command_parts[0].lower()
654-
args = command_parts[1:]
655-
656-
if command == 'user':
657-
print(cmd_USER(socket, *args))
658-
elif command == 'pass':
659-
print(cmd_PASS(socket, *args))
660-
elif command == 'acct':
661-
print(cmd_ACCT(socket, *args))
662-
elif command == 'smnt':
663-
print(cmd_SMNT(socket, *args))
664-
elif command == 'rein':
665-
print(cmd_REIN(socket, *args))
666-
elif command == 'quit':
667-
print(cmd_QUIT(socket, *args))
668-
break #Revisar si es necesario el break
669-
elif command == 'pwd':
670-
print(cmd_PWD(socket, *args))
671-
elif command == 'cwd':
672-
print(cmd_CWD(socket, *args))
673-
elif command == 'cdup':
674-
print(cmd_CDUP(socket, *args))
675-
elif command == 'mkd':
676-
print(cmd_MKD(socket, *args))
677-
elif command == 'rmd':
678-
print(cmd_RMD(socket, *args))
679-
elif command == 'retr':
680-
print(cmd_RETR(socket, *args))
681-
elif command == 'stor':
682-
print(cmd_STOR(socket, *args))
683-
elif command == 'appe':
684-
print(cmd_APPE(socket, *args))
685-
elif command == 'dele':
686-
print(cmd_DELE(socket, *args))
687-
elif command == 'list':
688-
print(cmd_LIST(socket, *args))
689-
elif command == 'nlst':
690-
print(cmd_NLST(socket, *args))
691-
elif command == 'abor':
692-
print(cmd_ABOR(socket, *args))
693-
elif command == 'type':
694-
print(cmd_TYPE(socket, *args))
695-
elif command == 'mode':
696-
print(cmd_MODE(socket, *args))
697-
elif command == 'stru':
698-
print(cmd_STRU(socket, *args))
699-
elif command == 'port':
700-
print(cmd_PORT(socket, *args))
701-
elif command == 'pasv':
702-
PASV_SOCKET = cmd_PASV(socket, *args)
703-
elif command == 'syst':
704-
print(cmd_SYST(socket, *args))
705-
elif command == 'stat':
706-
print(cmd_STAT(socket, *args))
707-
elif command == 'help':
708-
print(cmd_HELP(socket, *args))
709-
elif command == 'rnfr':
710-
print(cmd_RNFR(socket, *args))
711-
elif command == 'rnto':
712-
print(cmd_RNTO(socket, *args))
713-
elif command == 'noop':
714-
print(cmd_NOOP(socket, *args))
715-
elif command == 'stou':
716-
print(cmd_STOU(socket, *args))
717-
elif command == 'allo':
718-
print(cmd_ALLO(socket, *args))
719-
elif command == 'rest':
720-
print(cmd_REST(socket, *args))
721-
elif command == 'site':
722-
print(cmd_SITE(socket, *args))
723-
else:
724-
print("502: Comando no reconocido, por favor intente de nuevo.")
725-
except Exception as e:
726-
print(f"Error: {e}")
727-
648+
# Ejecutar comando
649+
cmd_args = [arg for arg in [a_arg, b_arg] if arg is not None]
650+
651+
try:
652+
user_input = input("=> ")
653+
command_parts = user_input.strip().split(" ")
654+
command = command_parts[0].lower()
655+
args = command_parts[1:]
656+
657+
if command == 'user':
658+
print(cmd_USER(ftp_socket, *cmd_args))
659+
elif command == 'pass':
660+
print(cmd_PASS(ftp_socket, *cmd_args))
661+
elif command == 'acct':
662+
print(cmd_ACCT(ftp_socket, *cmd_args))
663+
elif command == 'smnt':
664+
print(cmd_SMNT(ftp_socket, *cmd_args))
665+
elif command == 'rein':
666+
print(cmd_REIN(ftp_socket, *cmd_args))
667+
elif command == 'quit':
668+
print(cmd_QUIT(ftp_socket, *cmd_args))
669+
ftp_socket.close()
670+
elif command == 'pwd':
671+
print(cmd_PWD(ftp_socket, *cmd_args))
672+
elif command == 'cwd':
673+
print(cmd_CWD(ftp_socket, *cmd_args))
674+
elif command == 'cdup':
675+
print(cmd_CDUP(ftp_socket, *cmd_args))
676+
elif command == 'mkd':
677+
print(cmd_MKD(ftp_socket, *cmd_args))
678+
elif command == 'rmd':
679+
print(cmd_RMD(ftp_socket, *cmd_args))
680+
elif command == 'retr':
681+
print(cmd_RETR(ftp_socket, *cmd_args))
682+
elif command == 'stor':
683+
print(cmd_STOR(ftp_socket, *cmd_args))
684+
elif command == 'appe':
685+
print(cmd_APPE(ftp_socket, *cmd_args))
686+
elif command == 'dele':
687+
print(cmd_DELE(ftp_socket, *cmd_args))
688+
elif command == 'list':
689+
print(cmd_LIST(ftp_socket, *cmd_args))
690+
elif command == 'nlst':
691+
print(cmd_NLST(ftp_socket, *cmd_args))
692+
elif command == 'abor':
693+
print(cmd_ABOR(ftp_socket, *cmd_args))
694+
elif command == 'type':
695+
print(cmd_TYPE(ftp_socket, *cmd_args))
696+
elif command == 'mode':
697+
print(cmd_MODE(ftp_socket, *cmd_args))
698+
elif command == 'stru':
699+
print(cmd_STRU(ftp_socket, *cmd_args))
700+
elif command == 'port':
701+
print(cmd_PORT(ftp_socket, *cmd_args))
702+
elif command == 'pasv':
703+
PASV_SOCKET = cmd_PASV(ftp_socket, *cmd_args)
704+
elif command == 'syst':
705+
print(cmd_SYST(ftp_socket, *cmd_args))
706+
elif command == 'stat':
707+
print(cmd_STAT(ftp_socket, *cmd_args))
708+
elif command == 'help':
709+
print(cmd_HELP(ftp_socket, *cmd_args))
710+
elif command == 'rnfr':
711+
print(cmd_RNFR(ftp_socket, *cmd_args))
712+
elif command == 'rnto':
713+
print(cmd_RNTO(ftp_socket, *cmd_args))
714+
elif command == 'noop':
715+
print(cmd_NOOP(ftp_socket, *cmd_args))
716+
elif command == 'stou':
717+
print(cmd_STOU(ftp_socket, *cmd_args))
718+
elif command == 'allo':
719+
print(cmd_ALLO(ftp_socket, *cmd_args))
720+
elif command == 'rest':
721+
print(cmd_REST(ftp_socket, *cmd_args))
722+
elif command == 'site':
723+
print(cmd_SITE(ftp_socket, *cmd_args))
724+
else:
725+
print("502: Comando no reconocido, por favor intente de nuevo.")
726+
except Exception as e:
727+
print(f"Error al ejecutar el comando: {e}")

0 commit comments

Comments
 (0)