Registry / http-networking / publicsuffix2

publicsuffix2

JSON →
library2.20191221pypypi✓ verified 24d ago

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.

pip install publicsuffix2
INSTALL
IMPORT
SIG · PUBLICSUFFIX2
P
publicsuffix2
http-networkingpythonv2.20191221
Install
1.5s avg
Import
88ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v2.20191221 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.096s · 18.1MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.5s · import 0.080s · 19MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

PublicSuffixList
from publicsuffix2 import PublicSuffixList
from publicsuffix import PublicSuffixList
This is a fork; ensure you import from 'publicsuffix2' not the original 'publicsuffix' package.

Initialize PublicSuffixList and use `get_public_suffix()` to extract the registrable domain from a given hostname. It returns None for hosts that are not valid domains or do not have a recognizable public suffix.

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')}")
Debug
Known issues
breakingThe `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.
fix
Explicitly choose and consistently use either `publicsuffix` or `publicsuffix2` throughout your project. Do not intermix or rely on their interchangeability.
affects: All versions
gotchaThe 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.
fix
For applications requiring a current Public Suffix List, consider using a more actively maintained library that provides up-to-date PSL data, or be aware of the inherent risks and limitations of using outdated data with `publicsuffix2`.
affects: 2.20191221 and older
Errors
Common errors & fixes
publicsuffix2 outdated data
The `publicsuffix2` library, version 2.20191221, bundles the Public Suffix List (PSL) from late 2019, which is significantly outdated and does not recognize newer TLDs or recent changes, leading to incorrect public suffix identification.
fix
For applications requiring a current Public Suffix List, consider migrating to a more actively maintained library that provides up-to-date PSL data, or be aware of the inherent risks and limitations of using outdated data with `publicsuffix2`.
ModuleNotFoundError: No module named 'publicsuffix.publicsuffix'
Developers might mistakenly attempt to import from the original `publicsuffix` package or an incorrect submodule path, instead of the `publicsuffix2` fork.
fix
Ensure you are importing directly from the `publicsuffix2` package, which is the correct module name for this library. Example: `from publicsuffix2 import get_public_suffix` or `from publicsuffix2 import PublicSuffixList`.
publicsuffix2.get_public_suffix('new-tld.example') returns None
The `get_public_suffix()` method returns `None` for hostnames that are not valid domains or for valid domains whose public suffix is not recognized by the library's bundled, outdated Public Suffix List.
fix
This often indicates that the domain's TLD is too new to be present in `publicsuffix2`'s static PSL. The primary fix is to use a more actively maintained library with an updated PSL. If you must use `publicsuffix2`, you'll need to manually manage an external, updated PSL file and pass it to the function if the API supports it, or handle the `None` return gracefully. The current version provides a `fetch()` method to get the latest list, which should be used if dynamic updates are feasible.
Incorrect results for Unicode domains with publicsuffix2
Failure to correctly handle IDNA (Punycode) encoding for internationalized domain names can lead to incorrect public suffix identification. By default, the module converts the PSL, but for UTF-8 domains, the `IDNA-encoding` flag might need to be explicitly set.
fix
When working with Unicode domains, ensure that the domain is properly Punycode-encoded before passing it to `publicsuffix2` functions, or, if using `PublicSuffixList` directly with UTF-8 domains, you might need to set `IDNA-encoding=False` during instantiation to prevent double-conversion or incorrect processing. Example: `psl = PublicSuffixList(IDNA_encoding=False)` (refer to the library's specific IDNA handling documentation).
Upgrade
Version history
2.20191221latest on PyPI · released Dec 21, 2019
Audit
Dependencies

No dependency data recorded yet.

Agent activity
10 hits · last 30 days
node
8
Resources
publicsuffix2 — pip install publicsuffix2 · libregistry