6262
6363class PublicSuffixList (object ):
6464
65- def __init__ (self , psl_file = None , idna = True ):
65+ def __init__ (self , psl_file = None , idna = True , private = True ):
6666 """
6767 Read and parse a public suffix list. `psl_file` is either a file
6868 location string, or a file-like object, or an iterable of lines from a
@@ -80,6 +80,7 @@ def __init__(self, psl_file=None, idna=True):
8080
8181 :param psl_file: string or None
8282 :param idna: boolean, whether to convert file to IDNA-encoded strings
83+ :param private: boolean, include non-ICANN private names, default=True
8384 """
8485 # Note: we test for None as we accept empty lists as inputs
8586 if psl_file is None or isinstance (psl_file , str ):
@@ -91,7 +92,7 @@ def __init__(self, psl_file=None, idna=True):
9192
9293 # a list of eTLDs with their modifiers, e.g., *
9394 self .tlds = []
94- root = self ._build_structure (psl , idna )
95+ root = self ._build_structure (psl , idna , private )
9596 self .root = self ._simplify (root )
9697
9798 def _find_node (self , parent , parts ):
@@ -161,7 +162,7 @@ def _simplify(self, node):
161162
162163 return (node [0 ], dict ((k , self ._simplify (v )) for (k , v ) in node [1 ].items ()))
163164
164- def _build_structure (self , fp , idna ):
165+ def _build_structure (self , fp , idna , private ):
165166 """
166167 Build a Trie from the public suffix list. If idna==True, idna-encode
167168 each line before building.
@@ -180,6 +181,7 @@ def _build_structure(self, fp, idna):
180181
181182 :param fp: pointer for the public suffix list
182183 :param idna: boolean, convert lines to idna-encoded strings
184+ :param private: boolean, include non-ICANN private names, default=True
183185 :return: Trie
184186 """
185187 root = [0 ]
@@ -188,6 +190,8 @@ def _build_structure(self, fp, idna):
188190
189191 for line in fp :
190192 line = line .strip ()
193+ if not private and line .startswith ('// ===BEGIN PRIVATE' ):
194+ break
191195 if not line or line .startswith ('//' ):
192196 continue
193197 if idna :
0 commit comments