43 lines
1.2 KiB
Python
43 lines
1.2 KiB
Python
# servidor_universal.py
|
|
import socket
|
|
import os
|
|
|
|
if not os.path.exists("recibidos"):
|
|
os.makedirs("recibidos")
|
|
|
|
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
server.bind(('0.0.0.0', 5000))
|
|
server.listen(1)
|
|
|
|
print("🖥️ Servidor Universal Escuchando...")
|
|
print("📍 IPs disponibles:")
|
|
os.system("hostname -I 2>/dev/null || ipconfig 2>/dev/null")
|
|
print("🔌 Puerto: 5000")
|
|
print("=" * 40)
|
|
|
|
while True:
|
|
try:
|
|
client, addr = server.accept()
|
|
print(f"📨 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)
|
|
f.write(datos)
|
|
recibido += len(datos)
|
|
|
|
print(f"✅ {nombre} recibido correctamente!")
|
|
client.send("Archivo recibido ✅".encode())
|
|
client.close()
|
|
|
|
except KeyboardInterrupt:
|
|
print("\n👋 Servidor cerrado")
|
|
break
|
|
except Exception as e:
|
|
print(f"❌ Error: {e}")
|