import socketserver import ipcalc class Daemon(): def __init__(data): self.data = data def query(q): if self.is_ip(q): self.search_ip(q) elif self.is_domain(q): self.search_domain(q) else: # Try to find a person with the given query person = search_person(q) if person: return person else: return self.show_help() def search_ip(self, ip): result = {} # Iterate over all IP block elements for network in self.data.get_networks(): for block in network.ip_blocks: if ip in ipcalc.Network(block): result['name'] = network.name for key in network.data: result[key] = network.data[key] return result result['error'] = "Red no encontrada" return result # TODO def search_domain(self, domain): pass def search_person(self, query): pass # TODO def is_ip(self, query): pass # TODO def is_domain(self, query): pass # TODO def show_help(): pass class ThreadedTCPRequestHandler(socketserver.BaseRequestHandler): def handle(self): data = self.request.recv(100) cur_thread = threading.current_thread() response = self.get_header() + "\n" + self.get_answer(data) + "\n" + self.get_footer() self.request.sendall(response) class ClassicServer(socketserver.TCPServer): def __init__(self, config): host = config['Servers']['classic_host'] port = config['Servers']['classic_port'] TCPServer.__init__(self, (host, port), ThreadedTCPRequestHandler) def start(self): pass class WebServer(socketserver.TCPServer): def __init__(self, config): self.host = config['Servers']['web_host'] self.port = config['Servers']['web_port']