From bbe6c04e2dc35a7b9567f41e84b425b5ae3c3b26 Mon Sep 17 00:00:00 2001 From: natalia Date: Fri, 16 Feb 2024 18:47:08 +0100 Subject: [PATCH] =?UTF-8?q?Proyecto=20Diccionario=20Multiling=C3=BCe?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CLIENTETRAD.py | 31 +++++++++++++++++++++++++++++++ SERVIDORTRAD.py | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 CLIENTETRAD.py create mode 100644 SERVIDORTRAD.py diff --git a/CLIENTETRAD.py b/CLIENTETRAD.py new file mode 100644 index 0000000..9cb9b85 --- /dev/null +++ b/CLIENTETRAD.py @@ -0,0 +1,31 @@ +import socket + +def cliente_udp(): + cliente = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + direccion_servidor = ('localhost', 2000) + + idioma_elegido = input("Seleccione el idioma (ingles, frances, italiano, portugues, japones o coreano): ") + + while True: + opcion = input("Ingrese 'c' para cambiar de idioma, una palabra o '0' para salir: ") + + if opcion == '0': + break + elif opcion == 'c': + idioma_elegido = input("Seleccione el nuevo idioma (ingles, frances, italiano, portugues, japones o coreano): ") + continue + + + mensaje = f"{opcion},{idioma_elegido}" + cliente.sendto(mensaje.encode('utf-8'), direccion_servidor) + + datos, _ = cliente.recvfrom(1024) + respuesta = datos.decode('utf-8') + + print(f"Respuesta del servidor: {respuesta}") + + print("Conexión terminada.") + cliente.close() + +if __name__ == "__main__": + cliente_udp() diff --git a/SERVIDORTRAD.py b/SERVIDORTRAD.py new file mode 100644 index 0000000..ea0fc1b --- /dev/null +++ b/SERVIDORTRAD.py @@ -0,0 +1,40 @@ +import socket + +idiomas = { + "ingles": {"hola": "hello", "adios": "bye", "buenos dias": "good morning"}, + "frances": {"hola": "bonjour", "adios": "au revoir", "buenos dias": "bonne matinée"}, + "italiano": {"hola": "ciao", "adios": "arrivederci", "buenos dias": "buongiorno"}, + "portugues": {"hola": "olá", "adios": "adeus", "buenos dias": "bom dia"}, + "japones": {"hola": "こんにちは (konnichiwa)", "adios": "さようなら (sayounara)", "buenos dias": "おはようございます (ohayou gozaimasu)"}, + "coreano": {"hola": "안녕하세요 (anneonghaseyo)", "adios": "안녕히 가세요 (annyeonghi kaseyo)", "buenos dias": "좋은 아침이에요 (jonun achimieyo)"} +} + +def servidor_udp(): + servidor = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + direccion_servidor = ('', 2000) + servidor.bind(direccion_servidor) + + print('El servidor está a la escucha...') + + cliente_conectado = False + + while True: + datos, direccion_cliente = servidor.recvfrom(1024) + palabra, idioma = datos.decode('utf-8').split(',') + + if not cliente_conectado: + print(f"¡Cliente conectado desde {direccion_cliente}!") + cliente_conectado = True + + if idioma in idiomas: + traduccion = idiomas[idioma].get(palabra.lower(), "Error en la respuesta, palabra no encontrada :)") + respuesta = f"{traduccion}" + else: + respuesta = "Error en la respuesta, inserta un idioma válido :)" + + servidor.sendto(respuesta.encode('utf-8'), direccion_cliente) + + +if __name__ == "__main__": + servidor_udp() +