Skip to content
/ psl Public
forked from ko-zu/psl

publicsuffixlist for python

License

Notifications You must be signed in to change notification settings

mzpqnxow/psl

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

88 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

publicsuffixlist

Public Suffix List parser implementation for Python 2.5+/3.x.

  • Compliant with TEST DATA
  • Support IDN (unicode or punycoded).
  • Support Python2.5+ and Python 3.x
  • Shipped with built-in PSL and the updater script.
  • Written in Pure Python. No library dependencies.

Build Status PyPI version Downloads

Install

publicsuffixlist can be installed via pip or pip3.

$ sudo pip install publicsuffixlist

If you are in a bit old distributions (RHEL/CentOS6.x), you may need to update pip itself before install.

$ sudo pip install -U pip

Usage

from publicsuffixlist import PublicSuffixList

psl = PublicSuffixList()
# uses built-in PSL file

psl.publicsuffix("www.example.com")   # "com"
# longest public suffix part

psl.privatesuffix("www.example.com")  # "example.com"
# shortest domain assigned for a registrant

psl.privatesuffix("com") # None
# None if no private (non-public) part found


psl.publicsuffix("www.example.unknownnewtld") # "unkownnewtld"
# new TLDs are valid public suffix by default

psl.publicsuffix(u"www.example.香港")   # u"香港"
# accept unicode

psl.publicsuffix("www.example.xn--j6w193g") # "xn--j6w193g"
# accept punycoded IDNs by default

Latest PSL can be passed as a file like line-iterable object.

with open("latest_psl.dat", "rb") as f:
    psl = PublicSuffixList(f)

Works with both Python 2.x and 3.x.

$ python2 setup.py test
$ python3 setup.py test

Drop-in compatibility code to replace publicsuffix

# from publicsuffix import PublicSuffixList
from publicsuffixlist.compat import PublicSuffixList

psl = PublicSuffixList()
psl.suffix("www.example.com")   # return "example.com"
psl.suffix("com")               # return "" rather than None

Some convenient methods available.

psl.is_private("example.com")  # True
psl.privateparts("aaa.www.example.com") # ("aaa", "www", "example.com")
psl.subdomain("aaa.www.example.com", depth=1) # "www.example.com"

Limitation

publicsuffixlist do 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 do unknowndomain.encode("idna") which is idempotence.

ICANN and private suffixes

The public suffix list contains both suffixes for ICANN domains and private suffixes. Using the flag only_icann the 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'

License

  • This 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).

Source / Link

About

publicsuffixlist for python

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python 100.0%