Skip to content

Commit

Permalink
Refactor whois function and add custom exceptions for better error ha…
Browse files Browse the repository at this point in the history
…ndling
  • Loading branch information
Misiu committed Jan 14, 2025
1 parent 30b544f commit e1e90df
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
16 changes: 13 additions & 3 deletions whois/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import os
import subprocess
import socket
from typing import Any, List, Optional, Pattern
from .parser import WhoisEntry
from .whois import NICClient
import logging
Expand All @@ -18,12 +19,21 @@
logger.addHandler(handler)

# thanks to https://www.regextester.com/104038
IPV4_OR_V6 = re.compile(
IPV4_OR_V6: Pattern[str] = re.compile(
r"((^\s*((([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))\s*$)|(^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$))" # noqa: E501
)


def whois(url, command=False, flags=0, executable="whois", executable_opts=None, inc_raw=False, quiet=False, convert_punycode=True):
def whois(
url:str,
command:bool=False,
flags: int=0,
executable: str="whois",
executable_opts: Optional[List[str]]=None,
inc_raw: bool=False,
quiet: bool=False,
convert_punycode: bool=True
):
# clean domain to expose netloc
ip_match = IPV4_OR_V6.match(url)
if ip_match:
Expand Down Expand Up @@ -62,7 +72,7 @@ def whois(url, command=False, flags=0, executable="whois", executable_opts=None,
suffixes = None


def extract_domain(url):
def extract_domain(url: str) -> str:
"""Extract the domain from the given URL
>>> logger.info(extract_domain('http://www.google.com.au/tos.html'))
Expand Down
26 changes: 26 additions & 0 deletions whois/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class PywhoisError(Exception):
pass


class UnknownTldError(PywhoisError):
pass


class WhoisDomainNotFoundError(PywhoisError):
pass


class FailedParsingWhoisOutputError(PywhoisError):
pass


class WhoisQuotaExceededError(PywhoisError):
pass


class UnknownDateFormatError(PywhoisError):
pass


class WhoisCommandFailedError(PywhoisError):
pass

0 comments on commit e1e90df

Please sign in to comment.