Skip to content

Commit e85331e

Browse files
Merge pull request #295 from goodlandsecurity/master
Allow setting timeout for WHOIS request, regex parse additional WHOIS fields
2 parents 53adfe4 + fcd7baf commit e85331e

3 files changed

Lines changed: 22 additions & 7 deletions

File tree

whois/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ def whois(
3030
quiet: bool = False,
3131
ignore_socket_errors: bool = True,
3232
convert_punycode: bool = True,
33+
timeout: int = 10,
3334
) -> dict[str, Any]:
3435
"""
3536
url: the URL to search whois
@@ -40,6 +41,7 @@ def whois(
4041
quiet: whether to avoid printing output (default False)
4142
ignore_socket_errors: whether to ignore socket errors (default True)
4243
convert_punycode: whether to convert the given URL punycode (default True)
44+
timeout: timeout for WHOIS request (default 10 seconds)
4345
"""
4446
# clean domain to expose netloc
4547
ip_match = IPV4_OR_V6.match(url)
@@ -71,7 +73,7 @@ def whois(
7173
nic_client = NICClient()
7274
if convert_punycode:
7375
domain = domain.encode("idna").decode("utf-8")
74-
text = nic_client.whois_lookup(None, domain, flags, quiet=quiet, ignore_socket_errors=ignore_socket_errors)
76+
text = nic_client.whois_lookup(None, domain, flags, quiet=quiet, ignore_socket_errors=ignore_socket_errors, timeout=timeout)
7577
entry = WhoisEntry.load(domain, text)
7678
if inc_raw:
7779
entry["raw"] = text

whois/parser.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,10 @@ class WhoisEntry(dict):
121121
"state": r"Registrant State/Province: *(.+)",
122122
"registrant_postal_code": r"Registrant Postal Code: *(.+)",
123123
"country": r"Registrant Country: *(.+)",
124+
"tech_name": r"Tech Name: *(.+)",
125+
"tech_org": r"Tech Organization: *(.+)",
126+
"admin_name": r"Admin Name: *(.+)",
127+
"admin_org": r"Admin Organization: *(.+)"
124128
}
125129

126130
# allows for data string manipulation before casting to date

whois/whois.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -241,11 +241,11 @@ def whois(
241241
nhost = None
242242
response_str = response.decode("utf-8", "replace")
243243
if 'with "=xxx"' in response_str:
244-
return self.whois(query, hostname, flags, True, quiet=quiet, ignore_socket_errors=ignore_socket_errors)
244+
return self.whois(query, hostname, flags, True, quiet=quiet, ignore_socket_errors=ignore_socket_errors, timeout=timeout)
245245
if flags & NICClient.WHOIS_RECURSE and nhost is None:
246246
nhost = self.findwhois_server(response_str, hostname, query)
247247
if nhost is not None and nhost != "":
248-
response_str += self.whois(query, nhost, 0, quiet=quiet, ignore_socket_errors=ignore_socket_errors)
248+
response_str += self.whois(query, nhost, 0, quiet=quiet, ignore_socket_errors=ignore_socket_errors, timeout=timeout)
249249
except socket.error as e:
250250
if not quiet:
251251
logger.error(
@@ -416,7 +416,7 @@ def choose_server(self, domain: str) -> Optional[str]:
416416
# return server
417417

418418
def whois_lookup(
419-
self, options: Optional[dict], query_arg: str, flags: int, quiet: bool = False, ignore_socket_errors: bool = True
419+
self, options: Optional[dict], query_arg: str, flags: int, quiet: bool = False, ignore_socket_errors: bool = True, timeout: int = 10
420420
) -> str:
421421
"""Main entry point: Perform initial lookup on TLD whois server,
422422
or other server to get region-specific whois server, then if quick
@@ -444,16 +444,17 @@ def whois_lookup(
444444
options["country"] + NICClient.QNICHOST_TAIL,
445445
flags,
446446
quiet=quiet,
447-
ignore_socket_errors=ignore_socket_errors
447+
ignore_socket_errors=ignore_socket_errors,
448+
timeout=timeout
448449
)
449450
elif self.use_qnichost:
450451
nichost = self.choose_server(query_arg)
451452
if nichost is not None:
452-
result = self.whois(query_arg, nichost, flags, quiet=quiet, ignore_socket_errors=ignore_socket_errors)
453+
result = self.whois(query_arg, nichost, flags, quiet=quiet, ignore_socket_errors=ignore_socket_errors, timeout=timeout)
453454
else:
454455
result = ""
455456
else:
456-
result = self.whois(query_arg, options["whoishost"], flags, quiet=quiet, ignore_socket_errors=ignore_socket_errors)
457+
result = self.whois(query_arg, options["whoishost"], flags, quiet=quiet, ignore_socket_errors=ignore_socket_errors, timeout=timeout)
457458
return result
458459

459460

@@ -599,6 +600,14 @@ def parse_command_line(argv: list[str]) -> tuple[optparse.Values, list[str]]:
599600
dest="whoishost",
600601
help="Lookup using host " + NICClient.PANDIHOST,
601602
)
603+
parser.add_option(
604+
"-t",
605+
"--timeout",
606+
action="store",
607+
type="int",
608+
dest="timeout",
609+
help="Set timeout for WHOIS request",
610+
)
602611
parser.add_option("-?", "--help", action="help")
603612

604613
return parser.parse_args(argv)

0 commit comments

Comments
 (0)