Please find a running example. Important element is to create byte object of the payload instead of string before you send as reply. socket object produces and ingests only bytes
import socket
import threading
import sys
def actual_work(data):
print(data)
return b'ACK'
def daemon():
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind(('127.0.0.1', 6666))
print("Listening on udp %s:%i" % ('127.0.0.1', 6666))
try:
while True:
data, addr = sock.recvfrom(4096)
ack = actual_work(data)
sock.sendto(ack, addr)
except Exception as e:
print(e)
finally:
sock.close()
def client():
sock = socket.socket( socket.AF_INET, socket.SOCK_DGRAM )
sock.settimeout(1)
try:
sock.sendto(b'payload', ('127.0.0.1', 6666))
reply, _ = sock.recvfrom(4096)
print(reply)
except socket.timeout as e:
print(e)
sys.exit(1)
finally:
sock.close()
if __name__ == '__main__':
thread = threading.Thread(target=daemon)
thread.start()
client()
client()
client()
client()
#Issue kill thread here
thread.join()