1
1
import Utils
2
2
from socket import *
3
3
4
- server_name = 'localhost'
5
- server_port = 12000
6
- timeout = 35
7
- buffer_size = 1024
4
+ class Client :
8
5
9
- def connect_server ():
10
- client_socket = socket (AF_INET , SOCK_STREAM )
11
- client_socket .settimeout (timeout )
12
- client_socket .connect ((server_name ,server_port ))
13
-
14
-
15
- def send_command (client_socket , command ):
16
- client_socket .send ((command + "\r \n " ).encode ())
17
- try :
18
- response = client_socket .recv (buffer_size ).decode ()
19
- print (response )
20
- return response
21
- except socket .timeout :
22
- print ("Tiempo de espera agotado para la respuesta del servidor." )
23
- return None
24
-
25
- def list_files (tls_socket ):
26
- response = send_command (tls_socket , 'PASV' )
27
- start = response .find ('(' ) + 1
28
- end = response .find (')' )
29
- pasv_info = response [start :end ].split ()
30
- ip = '.' .join (pasv_info [:4 ])
31
- port = (int (pasv_info [4 ]) << 8 ) + int (pasv_info [5 ])
32
- data_socket = socket .socket (socket .AF_INET , socket .SOCK_STREAM )
33
- data_socket .connect ((ip , port ))
34
- send_command (data_socket , 'LIST' )
35
-
36
- files = ''
37
- while True :
38
- chunk = data_socket .recv (buffer_size ).decode ()
39
- if not chunk :
40
- break
41
- files += chunk
6
+ #Constructor de la clase
7
+ def __init__ (self , ip_server , port ):
8
+ self .ip_server = ip_server
9
+ self .port = port
10
+ self .client_socket = socket (AF_INET , SOCK_STREAM )
11
+ self .data_socket = None
42
12
43
- data_socket .close ()
44
- print (tls_socket .recv (buffer_size ).decode ())
45
-
46
- def stor_file (tls_socket , file_name , file_path ):
47
- response = send_command (tls_socket , 'PASV' )
48
- start = response .find ('(' ) + 1
49
- end = response .find (')' )
50
- pasv_info = response [start :end ].split ()
51
- ip = '.' .join (pasv_info [:4 ])
52
- port = (int (pasv_info [4 ]) << 8 ) + int (pasv_info [5 ])
53
- data_socket = socket .socket (socket .AF_INET , socket .SOCK_STREAM )
54
- data_socket .connect ((ip , port ))
55
- send_command (data_socket , 'STOR ' + file_name )
56
-
57
- with open (file_path , 'rb' ) as file :
58
- file_content = file .read ()
59
- data_socket .sendall (file_content )
13
+ #Conectar con el servidor
14
+ def connect (self ):
15
+ self .client_socket .connect ((self .ip_server , self .port ))
16
+ response = self .receive_response ()
17
+ print (f"{ response } " )
60
18
61
- data_socket .close ()
62
- print (tls_socket .recv (buffer_size ).decode ())
63
-
64
- def retr_file (tls_socket , file_name , destiny_path ):
65
- response = send_command (tls_socket , 'PASV' )
66
- start = response .find ('(' ) + 1
67
- end = response .find (')' )
68
- pasv_info = response [start :end ].split ()
69
- ip = '.' .join (pasv_info [:4 ])
70
- port = (int (pasv_info [4 ]) << 8 ) + int (pasv_info [5 ])
71
- data_socket = socket .socket (socket .AF_INET , socket .SOCK_STREAM )
72
- data_socket .connect ((ip , port ))
73
- send_command (data_socket , 'RETR ' + file_name )
74
-
75
- with open (destiny_path , 'wb' ) as destiny :
76
- while True :
77
- data = socket .recv (4096 )
78
- if not data :
79
- break
80
- destiny .write (data )
19
+ #Recibir una respuesta del servidor
20
+ def receive_response (self ):
21
+ response = self .client_socket .recv (1024 )
22
+ return response .decode ()
23
+
24
+ #Enviar comando al servidor
25
+ def send_command (self , command ):
26
+ self .client_socket .sendall (f"{ command } \r \n " .encode ())
27
+ return self .receive_response ()
28
+
29
+ #Entrar en modo pasivo
30
+ def enter_passive_mode (self ):
31
+ response = self .send_command ("PASV" )
32
+ start = response .find ('(' ) + 1
33
+ end = response .find (')' )
34
+ data = response [start :end ].split (',' )
35
+ ip = '.' .join (data [:4 ])
36
+ port = int (data [4 ]) * 256 + int (data [5 ])
37
+ return ip , port
81
38
82
- data_socket .close ()
83
- print (tls_socket .recv (buffer_size ).decode ())
39
+ #Salir y cerrar la concexion
40
+ def quit (self , command ):
41
+ self .send_command ("QUIT" )
42
+ self .client_socket .close ()
43
+ print ("Close Conection..." )
84
44
85
- def ftp_client ():
86
- client_socket = connect_server ()
87
- if not client_socket :
88
- print ("No se pudo establecer una conexión segura." )
89
- return
45
+ #Comando para listar archivos
46
+ def list_file (self , command ):
47
+ ip , port = self .enter_passive_mode ()
48
+ self .data_socket = socket (AF_INET , SOCK_STREAM )
49
+ self .data_socket .connect ((ip , port ))
50
+ self .send_command ("LIST" )
51
+ data = self .data_socket .recv (1024 )
52
+ self .data_socket .close ()
53
+ return data .decode ()
90
54
91
- while True :
92
- command = input (">>" )
93
- if not command :
94
- continue
55
+ #Comando para recibir archivos
56
+ def retrieve_file (self , command ):
57
+ filename = command .split ()[1 ]
58
+ ip , port = self .enter_passive_mode ()
59
+ self .data_socket = socket (AF_INET , SOCK_STREAM )
60
+ self .data_socket .connect ((ip , port ))
61
+ self .send_command (f"RETR { filename } " )
95
62
96
- command_name = command . split ()[ 0 ]. upper ()
97
- if command_name in Utils . commands :
98
- if command_name == 'QUIT' :
99
- send_command ( client_socket , command )
63
+ with open ( filename , 'wb' ) as f :
64
+ while True :
65
+ data = self . data_socket . recv ( 1024 )
66
+ if not data :
100
67
break
101
-
102
- elif command_name == 'STOR' :
103
- local_file = command .split ()[1 ]
104
- remote_file = command .split ()[2 ]
105
- stor_file (client_socket , local_file , remote_file )
68
+ f .write (data )
69
+ self .data_socket .close ()
70
+ print (f"The file { filename } downloaded succesfully" )
106
71
107
- elif command_name == 'RETR' :
108
- remote_file = command .split ()[1 ]
109
- local_file = command .split ()[2 ]
110
- retr_file (client_socket , remote_file , local_file )
72
+ #Comando para Updatear un archivo
73
+ def store_file (self , command ):
74
+ filename = command .split ()[1 ]
75
+ ip , port = self .enter_passive_mode ()
76
+ self .data_socket = socket (AF_INET , SOCK_STREAM )
77
+ self .data_socket .connect ((ip , port ))
78
+ self .send_command (f"STOR { filename } " )
111
79
112
- elif command_name == 'LIST' :
113
- list_files (client_socket )
80
+ with open (filename , 'rb' ) as f :
81
+ while True :
82
+ data = f .read (4096 )
83
+ if not data :
84
+ break
85
+ self .data_socket .sendall (data )
86
+ self .data_socket .close ()
87
+ print (f"The file { filename } uploaded succesfully" )
88
+
89
+ Commands_Methods = {
90
+ "LIST" : list_file ,
91
+ "STOR" : store_file ,
92
+ "RETR" : retrieve_file ,
93
+ "QUIT" : quit ,
94
+ }
114
95
115
- else : send_command (client_socket , command_name )
116
-
117
- else :
118
- lev = 1000000000000
119
- sug = ""
120
- for command in Utils .commands :
121
- newlev = Utils .levenshtein_distance (command , command_name )
122
- if newlev < sug :
123
- sug = command
124
- print (f"Command { command_name } not found, prove with { sug } " )
96
+ #Metodo que llamara a todas las funcionalidades del cliente
97
+ def ftp_client (self ):
98
+ while True :
99
+ _input = input ("ftp>> " ).strip ()
100
+ command = _input .split ()[0 ].upper ()
101
+ if command in self .Commands_Methods .keys ():
102
+ self .Commands_Methods [command ](_input )
103
+ self .send_command (command )
104
+ else :
105
+ sug = Utils .Calculate_Lev (command )
106
+ print (f"Command { command } not found, prove with { sug } " )
0 commit comments