You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
30 lines
884 B
30 lines
884 B
import socket
|
|
|
|
HOST = '192.168.50.102'
|
|
PORT = 3333
|
|
|
|
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)
|
|
|
|
# convertir datos a cadena de texto
|
|
datos_str = datos.decode('utf-8')
|
|
|
|
# enviar (en formato eco y en mayúsculas)
|
|
if datos_str == '1':
|
|
s.sendto(b'hola', addr)
|
|
elif datos_str == '2':
|
|
s.sendto(b'hello', addr)
|
|
elif datos_str == '3':
|
|
s.sendto(b'hallo', addr)
|
|
elif datos_str:
|
|
datos_upper = datos_str.upper()
|
|
sent = s.sendto(datos_upper.encode('utf-8'), addr)
|
|
print('Enviados {} bytes de vuelta a {}'.format(sent, addr))
|