Extract subdomain using Python
When it comes to DNS configuration, we need to proper extract subdomain, domain of the URL. It is error-prone to implement it by ourself. The proper approach is to leverage the existing solution out there.
pip3 install tldextract
>> import tldextract
>> tldextract.extract("http://im.sub.domain.com/pages/index.html")
ExtractResult(subdomain='im.sub', domain='domain', suffix='com')
>> tldextract.extract("http://blog.domain.com/pages/index?q=2019")
ExtractResult(subdomain='blog', domain='domain', suffix='com')
Source: