diff options
author | Raúl Benencia <rul@kalgan.cc> | 2012-08-03 13:36:15 -0300 |
---|---|---|
committer | Raúl Benencia <rul@kalgan.cc> | 2012-08-03 13:36:15 -0300 |
commit | 21ac58f5dabc78a8c7bb56249f5fa0151202e3d0 (patch) | |
tree | 69d755421920855d38819811d6da5dfdbc9ae0ed | |
parent | 06dea192b5c13a3154d6cd4c6103145ab7c449ba (diff) |
Added functions is_ip() and is_domain()
-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): |