2022-12-21 13:20:13 -05:00
|
|
|
import socket
|
2022-12-20 12:36:54 -05:00
|
|
|
|
2022-12-21 13:20:13 -05:00
|
|
|
LOCAL_IP = '127.0.0.1'
|
|
|
|
SERVER_PORT = 29987
|
|
|
|
SERVER_BUFFER = 1024
|
2022-12-20 12:36:54 -05:00
|
|
|
|
2022-12-21 13:20:13 -05:00
|
|
|
udp_server_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
|
|
|
udp_server_socket.bind((LOCAL_IP, SERVER_PORT))
|
2022-12-20 12:36:54 -05:00
|
|
|
|
2022-12-21 13:20:13 -05:00
|
|
|
waiting_for_connection = True
|
2022-12-20 12:36:54 -05:00
|
|
|
|
2022-12-21 13:20:13 -05:00
|
|
|
while waiting_for_connection:
|
|
|
|
received = udp_server_socket.recvfrom(SERVER_BUFFER)
|
|
|
|
message = received[0]
|
|
|
|
address = received[1]
|
2022-12-20 12:36:54 -05:00
|
|
|
|
2022-12-21 13:20:13 -05:00
|
|
|
decoded_message = message.decode('UTF-8')
|
|
|
|
if decoded_message.split(';')[0] != 'PYPONGREQ':
|
|
|
|
message = 'PYPONGRST;ERROR:INVALID HEADER'
|
|
|
|
udp_server_socket.sendto(message.encode('UTF-8'), address)
|
|
|
|
else:
|
|
|
|
message = 'PYPONGRST;ECHO:{0}'.format(decoded_message)
|
|
|
|
udp_server_socket.sendto(message.encode('UTF-8'), address)
|
2022-12-20 12:36:54 -05:00
|
|
|
|
2022-12-21 13:20:13 -05:00
|
|
|
udp_server_socket.close()
|