158 lines
4.1 KiB
Python
158 lines
4.1 KiB
Python
# cliente_servidor.py
|
|
import socket
|
|
import os
|
|
import subprocess
|
|
import threading
|
|
import platform
|
|
import tkinter as tk
|
|
from tkinter import filedialog
|
|
|
|
# ==========================
|
|
# SERVIDOR
|
|
# ==========================
|
|
|
|
def obtener_puerto_disponible():
|
|
for puerto in range(5000, 5101):
|
|
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
try:
|
|
s.bind(('0.0.0.0', puerto))
|
|
s.close()
|
|
return puerto
|
|
except:
|
|
s.close()
|
|
continue
|
|
raise Exception("❌ No hay puertos disponibles entre 5000 y 5100.")
|
|
|
|
|
|
puerto_servidor = obtener_puerto_disponible()
|
|
|
|
def iniciar_servidor():
|
|
if not os.path.exists("recibidos"):
|
|
os.makedirs("recibidos")
|
|
|
|
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
server.bind(('0.0.0.0', puerto_servidor))
|
|
server.listen(1)
|
|
|
|
print(f"\n🖥️ Servidor escuchando en puerto {puerto_servidor}...")
|
|
print("=" * 40)
|
|
|
|
while True:
|
|
try:
|
|
client, addr = server.accept()
|
|
print(f"\n📨 Conexión entrante de: {addr}")
|
|
|
|
info = client.recv(1024).decode()
|
|
nombre, tamaño = info.split("|")
|
|
tamaño = int(tamaño)
|
|
|
|
with open(f"recibidos/{nombre}", "wb") as f:
|
|
recibido = 0
|
|
while recibido < tamaño:
|
|
datos = client.recv(4096)
|
|
if not datos:
|
|
break
|
|
f.write(datos)
|
|
recibido += len(datos)
|
|
|
|
print(f"✅ Archivo recibido: {nombre}")
|
|
client.send("Archivo recibido correctamente".encode())
|
|
client.close()
|
|
|
|
except Exception as e:
|
|
print(f"❌ Error del servidor: {e}")
|
|
continue
|
|
|
|
# ==========================
|
|
# FUNCIONES CLIENTE
|
|
# ==========================
|
|
|
|
def limpiar_pantalla():
|
|
subprocess.run('clear', shell=True)
|
|
|
|
def mostrar_menu():
|
|
print("🖥️ ENVIADOR DE ARCHIVOS")
|
|
print("=" * 30)
|
|
print("1. 📁 Seleccionar archivo y enviar")
|
|
print("2. 🚪 Salir")
|
|
print("=" * 30)
|
|
|
|
def seleccionar_archivo():
|
|
root = tk.Tk()
|
|
root.withdraw() # Oculta la ventana principal
|
|
|
|
# Abre el selector de archivos nativo del SO
|
|
archivo = filedialog.askopenfilename(
|
|
title="Selecciona un archivo",
|
|
initialdir="."
|
|
)
|
|
|
|
root.destroy()
|
|
return archivo
|
|
|
|
def enviar_archivo(ruta, ip_servidor):
|
|
if not ruta or not os.path.exists(ruta):
|
|
print("❌ No se seleccionó archivo o no existe.")
|
|
input("Enter para continuar...")
|
|
return
|
|
|
|
try:
|
|
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
client.connect((ip_servidor, 5000))
|
|
|
|
nombre = os.path.basename(ruta)
|
|
tamaño = os.path.getsize(ruta)
|
|
|
|
client.send(f"{nombre}|{tamaño}".encode())
|
|
|
|
print(f"📤 Enviando archivo: {nombre}...")
|
|
with open(ruta, "rb") as f:
|
|
while True:
|
|
datos = f.read(4096)
|
|
if not datos:
|
|
break
|
|
client.send(datos)
|
|
|
|
print("⏳ Esperando confirmación...")
|
|
respuesta = client.recv(1024).decode()
|
|
print(f"✅ {respuesta}")
|
|
client.close()
|
|
|
|
input("Enter para continuar...")
|
|
|
|
except Exception:
|
|
print("❌ Error al conectar con el dispositivo")
|
|
input("Enter para continuar...")
|
|
|
|
# ==========================
|
|
# PROGRAMA
|
|
# ==========================
|
|
|
|
def main():
|
|
# Lanzar servidor en hilo
|
|
hilo_servidor = threading.Thread(target=iniciar_servidor, daemon=True)
|
|
hilo_servidor.start()
|
|
|
|
while True:
|
|
limpiar_pantalla()
|
|
mostrar_menu()
|
|
|
|
opcion = input("\nSelecciona una opción (1-2): ")
|
|
|
|
if opcion == "1":
|
|
ip = input("📡 IP destino: ")
|
|
print("📁 Selecciona archivo...")
|
|
archivo = seleccionar_archivo()
|
|
enviar_archivo(archivo, ip)
|
|
|
|
elif opcion == "2":
|
|
print("👋 Saliendo...")
|
|
break
|
|
|
|
else:
|
|
print("❌ Opción inválida")
|
|
input("Enter para continuar...")
|
|
|
|
if __name__ == "__main__":
|
|
main()
|