Skip to content

Instantly share code, notes, and snippets.

@dadevel
Last active January 10, 2023 10:23
Show Gist options
  • Select an option

  • Save dadevel/89ec05cd52676e249e1ad13eff951785 to your computer and use it in GitHub Desktop.

Select an option

Save dadevel/89ec05cd52676e249e1ad13eff951785 to your computer and use it in GitHub Desktop.
Second Level Domain Extractor
#!/usr/bin/env python3
import sys
import urllib.parse
import tldextract
# pip3 install --user tldextract
def main() -> None:
for line in sys.stdin:
url = urllib.parse.urlparse(line)
fqdn = url.netloc if url.scheme and url.netloc else line
result = tldextract.extract(fqdn)
print(f'{result.domain}.{result.suffix}')
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment