From 632df89519cb8c2602ecbef7baffd3f3e09d2f50 Mon Sep 17 00:00:00 2001 From: vicsash Date: Fri, 19 Jan 2024 17:47:01 +0100 Subject: [PATCH] Primer Ejercicio y Echo --- .idea/.gitignore | 8 +++++++ .idea/UDP.iml | 10 ++++++++ .../inspectionProfiles/profiles_settings.xml | 6 +++++ .idea/misc.xml | 4 ++++ .idea/modules.xml | 8 +++++++ .idea/vcs.xml | 6 +++++ ejer_bucle/cliente.py | 23 +++++++++++++++++++ ejer_bucle/servidor.py | 15 ++++++++++++ ejer_echo/cliente_echo.py | 15 ++++++++++++ ejer_echo/server_echo.py | 15 ++++++++++++ 10 files changed, 110 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/UDP.iml create mode 100644 .idea/inspectionProfiles/profiles_settings.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 ejer_bucle/cliente.py create mode 100644 ejer_bucle/servidor.py create mode 100644 ejer_echo/cliente_echo.py create mode 100644 ejer_echo/server_echo.py diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/UDP.iml b/.idea/UDP.iml new file mode 100644 index 0000000..7307346 --- /dev/null +++ b/.idea/UDP.iml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..bf12176 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..6bde5d8 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/ejer_bucle/cliente.py b/ejer_bucle/cliente.py new file mode 100644 index 0000000..b9873f1 --- /dev/null +++ b/ejer_bucle/cliente.py @@ -0,0 +1,23 @@ +import socket + +HOST = '127.0.0.1' +PORT = 2000 + +with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s: + server_address = (HOST, PORT) + + while True: + message = input("Enter a message (or 'exit' to quit): ") + if message.lower() == 'exit': + break + + # Send the message + print('Sending {!r}'.format(message)) + sent = s.sendto(message.encode('utf-8'), server_address) + + # Receive the response + print('Waiting for the response') + data, server = s.recvfrom(1024) # línea bloqueante + print('Received {!r} from {}'.format(data.decode('utf-8'), server)) + +# Program ends when 'exit' is entered by the user \ No newline at end of file diff --git a/ejer_bucle/servidor.py b/ejer_bucle/servidor.py new file mode 100644 index 0000000..4f493dc --- /dev/null +++ b/ejer_bucle/servidor.py @@ -0,0 +1,15 @@ +import socket +HOST = '' +PORT = 2000 +with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s: + s_addr = (HOST, PORT) + s.bind(s_addr) + while True: + #recibir + datos, addr = s.recvfrom(1024) #línea bloqueante + print('recibidos {} bytes de {}'.format(len(datos), addr)) + print(datos) + #enviar + if datos: + sent = s.sendto(b"Saludos desde el Servidor UDP", addr) + print('enviados {} bytes de vuelta a {}'.format(sent, addr)) \ No newline at end of file diff --git a/ejer_echo/cliente_echo.py b/ejer_echo/cliente_echo.py new file mode 100644 index 0000000..7af8929 --- /dev/null +++ b/ejer_echo/cliente_echo.py @@ -0,0 +1,15 @@ +import socket +HOST = '127.0.0.1' +PORT = 2000 +with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s: + s_addr = (HOST, PORT) + server_address = (s_addr) + cadena = input("Por favor, introduce una cadena: ") + message = bytes(cadena, 'utf-8') + # enviar + print('Enviando {!r}'.format(message)) + sent = s.sendto(message, server_address) + # recibir + print('Esperando por la respuesta') + data, server = s.recvfrom(1024) #línea bloqueante + print('recibidos {!r}'.format(data)) \ No newline at end of file diff --git a/ejer_echo/server_echo.py b/ejer_echo/server_echo.py new file mode 100644 index 0000000..47cda43 --- /dev/null +++ b/ejer_echo/server_echo.py @@ -0,0 +1,15 @@ +import socket +HOST = '' +PORT = 2000 +with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s: + s_addr = (HOST, PORT) + s.bind(s_addr) + while True: + #recibir + datos, addr = s.recvfrom(1024) #línea bloqueante + print('recibidos {} bytes de {}'.format(len(datos), addr)) + print(datos) + #enviar + if datos: + sent = s.sendto(datos.upper(), addr) + print('enviados {} bytes de vuelta a {}'.format(sent, addr)) \ No newline at end of file