Permalink
Branch: master
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
44 lines (29 sloc) 1.04 KB
import threading
import socketserver
class ThreadedTCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
pass
class ThreadedTCPRequestHandler(socketserver.BaseRequestHandler):
def handle(self):
data = self.request.recv(1024)
self.server.queue.add(data)
class Queue:
def __init__(self, ip, port):
self.server = ThreadedTCPServer((ip, port), ThreadedTCPRequestHandler)
self.server.queue = self
self.server_thread = threading.Thread(target=self.server.serve_forever)
self.server_thread.daemon = True
self.messages = []
def start_server(self):
self.server_thread.start()
print("Server loop running in thread:", self.server_thread.name)
def stop_server(self):
self.server.shutdown()
self.server.server_close()
def add(self, message):
self.messages.append(message)
def view(self):
return self.messages
def get(self):
return self.messages.pop()
def exists(self):
return len(self.messages)