Skip to main content

PSLExtract

Name parser based on Public Suffix List (a.k.a. PSL).

This package is an alternative to publicsuffixlist.

Setup

Use Python virtual environements for the best experience.

# in a venv
pip install pslextract

CLI Usage

# Fetch latest Public Suffix List version, use -h for help
pslfetch
# Parse latest Public Suffix List (after pslfetch), use -h for help
pslparse
# Process FQDNs
names=(
    raw.githubusercontent.com
    koromodako.github.io
    github.com
)
pslextract ${names[@]} | jq
{
  "name": "raw.githubusercontent.com",
  "prefix": "",
  "public_suffix": "githubusercontent.com",
  "private_suffix": "raw.githubusercontent.com"
}
{
  "name": "koromodako.github.io",
  "prefix": "",
  "public_suffix": "github.io",
  "private_suffix": "koromodako.github.io"
}
{
  "name": "github.com",
  "prefix": "",
  "public_suffix": "com",
  "private_suffix": "github.com"
}

API Usage

from dataclasses import asdict
from json import dumps

from pslextract import (
    DEFAULT_JSON_FILE,
    DEFAULT_RAW_FILE,
    PSLIndex,
    json_dumps,
    psl_create_index,
    psl_extract,
    psl_fetch,
)

NAMES = (
    'example.com',                # a valid Name
    'sub.example.com',            # a valid Name
    'sub.api.gov.uk',             # a valid Name
    ''                            # an invalid Name (empty string)
    '127.0.0.1',                  # an invalid Name (IPv4 address)
    'fe80::1010:1e8f:3f57:fe54',  # an invalid Name (IPv6 address)
    'subexamplecom',              # an invalid Name (missing dots)
    'sub....example.com',         # an invalid Name (empty components)
    'co.uk',                      # an invalid Name (suffix only)
    'sub.sld.notavalidtld',       # an invalid Name (unknown tld)
)


def main():
    if not DEFAULT_JSON_FILE.is_file():
        if not DEFAULT_RAW_FILE.is_file():
            psl_fetch()
        index = psl_create_index()
        index.to_json_file()
    index = PSLIndex.from_json_file()
    for name in NAMES:
        name = psl_extract(index, name)
        if not name:
            continue
        dct = {'name': name.value}
        dct.update(asdict(name))
        print(json_dumps(dct))


if __name__ == '__main__':
    main()

Metadata

Release files for pslextract 2.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distribution (wheel)

Table of built distributions (wheels) for pslextract 2.1.0
File Interpreter ABI Platform
pslextract-2.1.0-py3-none-any.whl Python 3 none any Details

Release files / pslextract-2.1.0-py3-none-any.whl

Download URL pslextract-2.1.0-py3-none-any.whl
Size 9.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1b768da8d740e1a36018a5aed491112b4d40eb33308b794fe3fb438204254ea2
BLAKE2b-256 checksum
How to use checksums
7915a8b231e04a92878ef0e2ea343d547ae50b499a916bbe5e8591a58dcfe590
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.3

Release history Release notifications | RSS feed

This release

2.1.0 This release

1 release file

2.0.0

1 release file

1.0.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page