diff options
| author | Raúl Benencia <rbenencia@linti.unlp.edu.ar> | 2012-08-02 13:58:58 -0300 | 
|---|---|---|
| committer | Raúl Benencia <rbenencia@linti.unlp.edu.ar> | 2012-08-02 13:58:58 -0300 | 
| commit | 10e9272d170f5c634e8ca8f7415fb4ad1454bfe6 (patch) | |
| tree | 8a41526b84c0586677bdc57fb62ee797efb14552 | |
| parent | 9afa33502b67151f36561119b70c765af675c1ca (diff) | |
Finished port to python3. Untested.
| -rw-r--r-- | core.py | 50 | ||||
| -rw-r--r-- | doc/pywhoisd.dia | bin | 3761 -> 3770 bytes | |||
| -rw-r--r-- | model.py | 47 | ||||
| -rwxr-xr-x | pywhoisd.py | 10 | ||||
| -rw-r--r-- | xml.py | 23 | 
5 files changed, 88 insertions, 42 deletions
| @@ -1,18 +1,24 @@ -from ipcalc import IP, Network  import socketserver +import ipcalc -class WhoisDaemon(): +class Daemon():      def __init__(data):          self.data = data          def query(q): -        if is_ip(q): +        if self.is_ip(q):              self.search_ip(q) +             +        elif self.is_domain(q): +            self.search_domain(q) +                      else: -            if is_domain(q): -                self.search_domain(q) +            # Try to find a person with the given query +            person = search_person(q) +            if person: +                return person              else: -                return self.print_help() +                return self.show_help()      def search_ip(self, ip):          result = {} @@ -20,7 +26,7 @@ class WhoisDaemon():          # Iterate over all IP block elements          for network in self.data.get_networks():             for block in network.ip_blocks: -               if ip in Network(block): +               if ip in ipcalc.Network(block):                     result['name'] = network.name                     for key in network.data:                         result[key] = network.data[key] @@ -30,9 +36,25 @@ class WhoisDaemon():          result['error'] = "Red no encontrada"          return result -    def search_domain(self): +    # 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): @@ -41,17 +63,17 @@ class ThreadedTCPRequestHandler(socketserver.BaseRequestHandler):          response = self.get_header() + "\n" + self.get_answer(data) + "\n" + self.get_footer()                  self.request.sendall(response) -class WhoisServer(socketserver.TCPServer): +class ClassicServer(socketserver.TCPServer):      def __init__(self, config): -        host = config.get('Servers', 'classic_host') -        port = config.get('Servers', 'classic_port') +        host = config['Servers']['classic_host'] +        port = config['Servers']['classic_port']          TCPServer.__init__(self, (host, port), ThreadedTCPRequestHandler)      def start(self):          pass         -class WhoisWebServer(socketserver.TCPServer): +class WebServer(socketserver.TCPServer):      def __init__(self, config): -        self.host = config.get('Servers', 'web_host') -        self.port = config.get('Servers', 'web_host') +        self.host = config['Servers']['web_host'] +        self.port = config['Servers']['web_port'] diff --git a/doc/pywhoisd.dia b/doc/pywhoisd.diaBinary files differ index b10ed92..df695a8 100644 --- a/doc/pywhoisd.dia +++ b/doc/pywhoisd.dia @@ -1,5 +1,3 @@ -from xml.etree.ElementTree import ElementTree -  class Network():      """A simple network definition""" @@ -31,19 +29,25 @@ class Person():          self.email = email  class Data(): -    """Abstract class for storing anf getting information""" +    """Abstract class for storing and getting information"""      def __init__(self, config): -        self.networks = []          self.config = config +        self.networks = [] +        self.domains = [] +        self.persons = []      def parse_config(self): -        """Abstract method""" +        """Parse neccesary config params depending on the method used + +        Abstract method"""          pass      def load_data(self): -        """Abstract method""" +        """Load data from defined source. + +        Abstract method"""          pass @@ -55,23 +59,18 @@ class Data():          return self.networks -class DataXML(Data): -    """Reads network information from a XML file""" -     -    def parse_config(): -        """Reads and sets up XML config file fields""" +    def get_domains(self): +        """Return all domains. Common method for all kind of storages.""" -        self.data_file = self.config['Storage']['xml_file'] +        if self.networks == None: +            self.load_data() + +        return self.domains + +    def get_persons(self): +        """Return all persons. Common method for all kind of storages.""" -    def load_data(self): -        """Parse XML for getting network information""" # Ugly implementation. Beautify. -        root = ElementTree(file=self.data_file).getroot() -        for elem in root: -            network = WhoisNetwork(elem.attrib['name']) -            for e in elem: -                if e.tag == 'ip_block': -                    network.ip_blocks.append(e.text) -                else: -                    network.data[e.tag] = e.text -                     -            self.networks.append(network) +        if self.persons == None: +            self.load_data() + +        return self.persons diff --git a/pywhoisd.py b/pywhoisd.py index 6b8a36d..4e78564 100755 --- a/pywhoisd.py +++ b/pywhoisd.py @@ -1,6 +1,8 @@  #!/usr/bin/python3  import configparser +  import core +import xml  class PyWhoisD():      """Main class. It reads the configuration options and starts the server""" @@ -22,13 +24,13 @@ class PyWhoisD():          mode = self.config['Storage']['mode']          if mode == 'xml': -            self.data = WhoisData.WhoisDataXML(self.config) +            self.data = xml.DataXML(self.config)      def config_daemon(self):          """Config common information source for all configured servers"""          self.config_data() -        self.daemon = WhoisDaemon(self.data) +        self.daemon = core.Daemon(self.data)      def web(self): @@ -46,10 +48,10 @@ class PyWhoisD():          """Sets up server configuration from config files"""          if self.classic(): -            self.classic_server = core.WhoisClassicServer(self.config, self.daemon) +            self.classic_server = core.ClassicServer(self.config, self.daemon)          if self.web(): -            self.web_server = core.WhoisWebServer(self.config, self.daemon) +            self.web_server = core.WebServer(self.config, self.daemon)      def start_servers(self):          """Start configured servers""" @@ -0,0 +1,23 @@ +from xml.etree.ElementTree import ElementTree +import model + +class DataXML(model.Data): +    """Reads network information from a XML file""" +     +    def parse_config(): +        """Reads and sets up XML config file fields""" +         +        self.data_file = self.config['Storage']['xml_file'] +         +    def load_data(self): +        """Parse XML for getting network information""" # Ugly implementation. Beautify. +        root = ElementTree(file=self.data_file).getroot() +        for elem in root: +            network = model.Network(elem.attrib['name']) +            for e in elem: +                if e.tag == 'ip_block': +                    network.ip_blocks.append(e.text) +                else: +                    network.data[e.tag] = e.text +                     +            self.networks.append(network) | 
