diff options
Diffstat (limited to 'core.py')
-rw-r--r-- | core.py | 24 |
1 files changed, 17 insertions, 7 deletions
@@ -1,9 +1,11 @@ import socketserver import ipaddr +import re class Daemon(): def __init__(self, data): - self.data = data + self.data = data + self.domain_regexp = re.compile("(?!-)[A-Z\d-]{1,63}(?<!-)$", re.IGNORECASE) def query(self, q): if self.is_ip(q): @@ -54,13 +56,21 @@ class Daemon(): def search_person(self, query): pass - # TODO def is_ip(self, query): - return True - - # TODO - def is_domain(self, query): - pass + try: + ipaddr.IPAddress(query) + return True + except ValueError: + return False + + def is_domain(self, hostname): + if len(hostname) > 255: + return False + + if hostname[-1:] == ".": + hostname = hostname[:-1] # strip exactly one dot from the right, if present + + return all(self.domain_regexp.match(x) for x in hostname.split(".")) # TODO def get_help(self): |