publicsuffix2: Public Suffix List Parser
publicsuffix2 is a Python library designed to parse and identify the public suffix of a domain name using the official Public Suffix List (PSL). It is a fork of the original 'publicsuffix' package, aiming for API compatibility. The current version is 2.20191221, released in late 2019, meaning its bundled PSL data is not recent.
Warnings
- breaking The `publicsuffix2` package is a fork of the `publicsuffix` library. While they shared an initial API, they are distinct projects and may diverge in bundled Public Suffix List (PSL) data, behavior, or features. Switching between them or assuming future compatibility can lead to unexpected results.
- gotcha The Public Suffix List (PSL) data is bundled directly within the `publicsuffix2` package. The last release (`2.20191221`) is from late 2019, meaning the bundled PSL is significantly outdated. This can lead to incorrect public suffix identification for newer TLDs or entries, potentially causing security vulnerabilities (e.g., incorrect cookie domain handling) and functional issues. There is no mechanism to update the PSL independently of a new package release.
Install
-
pip install publicsuffix2
Imports
- PublicSuffixList
from publicsuffix2 import PublicSuffixList
Quickstart
from publicsuffix2 import PublicSuffixList
# Initialize the PublicSuffixList parser
psl = PublicSuffixList()
# Get the public suffix for various domains
print(f"google.com -> {psl.get_public_suffix('www.google.com')}")
print(f"co.uk -> {psl.get_public_suffix('test.co.uk')}")
print(f"github.io -> {psl.get_public_suffix('myblog.github.io')}")
print(f"local domain (should be None) -> {psl.get_public_suffix('localhost')}")
print(f"invalid domain (should be None) -> {psl.get_public_suffix('not-a-domain')}")