Primer Ejercicio y Echo

master
vicsash 8 months ago
parent 632df89519
commit 72349b5c88

@ -0,0 +1,32 @@
import socket
HOST = '192.168.50.106'
PORT = 2000
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
s_addr = (HOST, PORT)
server_address = (s_addr)
# 1 para español, 2 para ingles, 3 para aleman
while True:
idioma = input("Ingrese el idioma: ")
if idioma == "español":
message = b"1"
break
elif idioma == "ingles":
message = b"2"
break
elif idioma == "aleman":
message = b"3"
break
else:
print("Ingrese un idioma valido")
# enviar
print('Enviando {}'.format(message))
sent = s.sendto(message, server_address)
# recibir
print('Esperando por la respuesta')
data, server = s.recvfrom(1024) # línea bloqueante
print('Recibido: {}'.format(data))

@ -0,0 +1,24 @@
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:
if datos == b"1":
s.sendto(b"hola", addr)
elif datos == b"2":
s.sendto(b"hello", addr)
elif datos == b"3":
s.sendto(b"hallo", addr)
else:
s.sendto(b"Lenguaje no reconocido", addr)

@ -1,6 +1,6 @@
import socket
HOST = '127.0.0.1'
HOST = '192.168.50.94'
PORT = 2000
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:

Loading…
Cancel
Save

Powered by INFORMATICA.FP.EDU.ES.