I'm building a UDP communication class, and I'm trying to add incoming messages to a queue. The problem is that I'm unable to access the parent class' queue member. Following is the applicable code, and notice the last line, which doesn't work, but is showing what I'm trying to do. I'm trying to add the message to the parent class' queue.
import socketserver
import threading, queue
class RsaServer:
messages = None # set to a queue class in __init__
def __init__(self, host="localhost", port=9999):
self.host = host
self.port = port
self.messages = queue.SimpleQueue()
self.main_thread = threading.Thread(target=self._server_thread())
def _server_thread(self):
with socketserver.UDPServer((self.host, self.port), self.UDPHandler) as server:
server.serve_forever()
class UDPHandler(socketserver.BaseRequestHandler):
"""Used to handle incoming communications to the server"""
def handle(self):
# print(self.request)
data = self.request[0].strip()
socket = self.request[1]
# from_addr = self.client_address[0]
try:
message = data.decode()
except UnicodeDecodeError:
# print("Rcvd bad data from: {}".format(from_addr))
pass
else:
# add msg to the gueue
messages.put(message)