diff options
author | Raúl Benencia <rbenencia@linti.unlp.edu.ar> | 2012-08-03 10:22:48 -0300 |
---|---|---|
committer | Raúl Benencia <rbenencia@linti.unlp.edu.ar> | 2012-08-03 10:22:48 -0300 |
commit | 06dea192b5c13a3154d6cd4c6103145ab7c449ba (patch) | |
tree | b31a936a3a881397e59d756b96b4e36d3990d617 /core.py | |
parent | 9d13223e971c1bb37b141cb0b0a3a7dd4077f4d8 (diff) |
Semi-functional version
Diffstat (limited to 'core.py')
-rw-r--r-- | core.py | 29 |
1 files changed, 20 insertions, 9 deletions
@@ -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): |