Skip to main content

Name parser based on Public Suffix List

Project description

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()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pslextract-2.1.0-py3-none-any.whl (9.8 kB view details)

Uploaded Python 3

File details

Details for the file pslextract-2.1.0-py3-none-any.whl.

File metadata

  • Download URL: pslextract-2.1.0-py3-none-any.whl
  • Upload date:
  • Size: 9.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for pslextract-2.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1b768da8d740e1a36018a5aed491112b4d40eb33308b794fe3fb438204254ea2
MD5 137fccb9f6af5d40ed126fd3a1151c23
BLAKE2b-256 7915a8b231e04a92878ef0e2ea343d547ae50b499a916bbe5e8591a58dcfe590

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page