Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update env.sh #13

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions env.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
# 3. SMTP
# 4. IRC

PROTOCOL=0
PROTOCOL=2

# Don't modify the next line
echo "PROTOCOL=${PROTOCOL}" >> "$GITHUB_ENV"
echo "PROTOCOL=${PROTOCOL}" >> "$GITHUB_ENV"
69 changes: 69 additions & 0 deletions main/client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import socket
import sys


class FTPClient:
def __init__(self, host, port):
self.host = host
self.port = port
self.control_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.control_socket.connect((self.host, self.port))
self.receive_response() # Recibir mensaje de bienvenida

def receive_response(self):
response = self.control_socket.recv(4096).decode('utf-8')
print(response, end='')
return response

def send_command(self, command):
self.control_socket.send((command + '\r\n').encode('utf-8'))
return self.receive_response()

def login(self, username, password):
self.send_command(f'USER {username}')
self.send_command(f'PASS {password}')

def list_files(self):
response = self.send_command('LIST')
if "150" in response:
return self.receive_response()

def quit(self):
self.send_command('QUIT')
self.control_socket.close()
print("Conexión cerrada.")


def main():
if len(sys.argv) != 3:
print("Uso: python client.py <servidor> <puerto>")
return

host = sys.argv[1]
port = int(sys.argv[2])

client = FTPClient(host, port)

while True:
try:
command = input("ftp> ").strip()
if not command:
continue

if command.startswith("login"):
_, username, password = command.split()
client.login(username, password)
elif command == "list":
client.list_files()

elif command == "quit":
client.quit()
break
else:
print("Comando no reconocido. Comandos disponibles: login, list, download, upload, quit")
except Exception as e:
print(f"Error: {e}")


if __name__ == "__main__":
main()
50 changes: 50 additions & 0 deletions main/server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import socket
import os
import threading


class FTPServer:
def __init__(self, host, port):
self.host = host
self.port = port
self.server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.server_socket.bind((self.host, self.port))
self.server_socket.listen(5)
print(f"Servidor FTP escuchando en {self.host}:{self.port}")

def handle_client(self, client_socket):
client_socket.send("220 Bienvenido al Servidor FTP\r\n".encode('utf-8'))
while True:
data = client_socket.recv(1024).decode('utf-8').strip()
if not data:
break

command = data.split(' ')[0].upper()
if command == 'USER':
client_socket.send("331 Nombre de usuario correcto, se requiere contraseña\r\n".encode('utf-8'))
elif command == 'PASS':
client_socket.send("230 Usuario autenticado, proceda\r\n".encode('utf-8'))
elif command == 'LIST':
files = os.listdir('.')
client_socket.send("150 Listado de directorio en proceso\r\n".encode('utf-8'))
client_socket.send(('\r\n'.join(files) + '\r\n').encode('utf-8'))
client_socket.send("226 Listado de directorio completado\r\n".encode('utf-8'))
elif command == 'QUIT':
client_socket.send("221 Adios\r\n".encode('utf-8'))
break
else:
client_socket.send("500 Comando desconocido\r\n".encode('utf-8'))

client_socket.close()

def run(self):
while True:
client_socket, addr = self.server_socket.accept()
print(f"Conexión aceptada de {addr}")
client_thread = threading.Thread(target=self.handle_client, args=(client_socket,))
client_thread.start()


if __name__ == "__main__":
server = FTPServer('0.0.0.0', 21)
server.run()
Loading