Package: publicsuffixlist
publicsuffixlistPublic Suffix Listparser implementation for Python 2.6+/3.x.Compliant withTEST DATASupport IDN (unicode or punycoded).Support Python2.6+ and Python 3.xShipped with built-in PSL and the updater script.Written in Pure Python. No library dependencies.Installpublicsuffixlistcan be installed viapiporpip3.$ sudo pip install publicsuffixlistIf you are in a bit old distributions (RHEL/CentOS6.x), you may need to updatepipitself before install.$ sudo pip install -U pipUsagefrompublicsuffixlistimportPublicSuffixListpsl=PublicSuffixList()# uses built-in PSL filepsl.publicsuffix("www.example.com")# "com"# longest public suffix partpsl.privatesuffix("www.example.com")# "example.com"# shortest domain assigned for a registrantpsl.privatesuffix("com")# None# None if no private (non-public) part foundpsl.publicsuffix("www.example.unknownnewtld")# "unknownnewtld"# new TLDs are valid public suffix by defaultpsl.publicsuffix(u"www.example.香港")# u"香港"# accept unicodepsl.publicsuffix("www.example.xn--j6w193g")# "xn--j6w193g"# accept punycoded IDNs by defaultLatest PSL can be passed as a file like line-iterable object.withopen("latest_psl.dat","rb")asf:psl=PublicSuffixList(f)Works with both Python 2.x and 3.x.$ python2 setup.py test
$ python3 setup.py testDrop-in compatibility code to replacepublicsuffix# from publicsuffix import PublicSuffixListfrompublicsuffixlist.compatimportPublicSuffixListpsl=PublicSuffixList()psl.suffix("www.example.com")# return "example.com"psl.suffix("com")# return "" (as str, not None)Some convenient methods available.psl.is_private("example.com")# Truepsl.privateparts("aaa.www.example.com")# ("aaa", "www", "example.com")psl.subdomain("aaa.www.example.com",depth=1)# "www.example.com"Limitationpublicsuffixlistdo NOT provide domain name validation.
In DNS protocol, most of 8-bit characters are acceptable label of domain name. ICANN compliant registries do not accept domain names that have_(underscore) but hostname may have. DMARC records, for example.Users need to confirm the input is valid based on the users' context.Partially encoded (Unicode-mixed) Punycode is not supported because of very slow Punycode en/decoding and unpredictable encoding of results.
If you are not sure the input is valid Punycode or not, you should dounknowndomain.encode("idna")which is idempotence.ICANN and private suffixesThe public suffix list contains both suffixes for ICANN domains and private suffixes. Using the flagonly_icannthe private suffixes can be deactivated:>>> psl = PublicSuffixList()
>>> psl.publicsuffix("example.priv.at")
'priv.at'
>>> psl = PublicSuffixList(only_icann=True)
>>> psl.publicsuffix("example.priv.at")
'at'LicenseThis module is licensed under Mozilla Public License 2.0.Public Suffix List maintained by Mozilla Foundation is licensed under Mozilla Public License 2.0.PSL testcase dataset is public domain (CC0).Development / PackagingThis module and its packaging workflow are maintained in the author's repository located athttps://github.com/ko-zu/psl.
A new package, which includes the latest PSL file, is automatically generated and uploaded to PyPI.
The last part of the version number represents the release date, for instance,0.10.1.20230331.Source / LinkGit repository on GitHub (https://github.com/ko-zu/psl)PyPI (https://pypi.org/project/publicsuffixlist/)