From 06dea192b5c13a3154d6cd4c6103145ab7c449ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ra=C3=BAl=20Benencia?= Date: Fri, 3 Aug 2012 10:22:48 -0300 Subject: Semi-functional version --- core.py | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) (limited to 'core.py') diff --git a/core.py b/core.py index cdb5343..430bdf2 100644 --- a/core.py +++ b/core.py @@ -7,16 +7,26 @@ class Daemon(): def query(self, q): if self.is_ip(q): - self.search_ip(q) + result = self.search_ip(q) elif self.is_domain(q): - self.search_domain(q) + result = self.search_domain(q) else: # Try to find a person with the given query person = search_person(q) if person: - return person + result = person else: - return self.get_help() + result = self.get_help() + + return self.format_result(result) + + def format_result(self, values): + """Receive a dictionary and return a string""" + result = "" + for k, v in values.items(): + result += '{0}: {1}\n'.format(k, v) + + return result def search_ip(self, ip): result = {} @@ -70,13 +80,14 @@ class WhoisHandler(socketserver.BaseRequestHandler): self.daemon = self.server.daemon def handle(self): - data = str(self.request.recv(100), 'utf-8')[:-2] + data = str(self.request.recv(100).strip(), 'utf-8') + print('Received: {}'.format(data)) - response = self.daemon.get_header() + "\n" - response += self.daemon.query(data) + "\n" - response += self.daemon.get_footer() + response = self.daemon.get_header() + " \n" + response += self.daemon.query(data) + response += self.daemon.get_footer() + "\n" - self.request.sendall(response) + self.request.sendall(bytes(response, 'utf-8')) class ClassicServer(socketserver.ThreadingTCPServer): def __init__(self, config, daemon): -- cgit v1.2.3