Skip to content

Instantly share code, notes, and snippets.

@tammoippen
Created November 13, 2017 15:51
Show Gist options
  • Save tammoippen/385abad0c3fd31d9b912a75302405dc5 to your computer and use it in GitHub Desktop.
Save tammoippen/385abad0c3fd31d9b912a75302405dc5 to your computer and use it in GitHub Desktop.
_roots = None
def roots_level_domains():
global _roots
if _roots is not None:
return _roots
# get https://publicsuffix.org/list/effective_tld_names.dat
rld = []
with open('effective_tld_names.dat', 'rb') as f:
for l in f:
ul = l.strip().decode('UTF-8')
if ul and not ul.startswith(u'//'):
rld += [list(reversed(ul.split('.')))]
_roots = dict()
for parts in rld:
curr = _roots
for p in parts:
if p == '*':
curr['valid'] = True
if p not in curr:
curr[p] = dict()
curr = curr[p]
curr['valid'] = True
return _roots
def rld(host):
parts = list(reversed(host.split('.')))
roots = roots_level_domains()
res = []
curr = roots
for p in parts:
subs = curr.get(p, {})
if subs == {}:
subs = curr.get('*', {})
if subs.get('valid', False):
res += [p]
else:
return '.'.join(reversed(res))
curr = subs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment