Skip to content

Commit

Permalink
Merge pull request #229 from BillWeissABK/add-timeout-flag
Browse files Browse the repository at this point in the history
Adding a quick timeout option to whois.whois()
  • Loading branch information
richardpenman authored Jun 17, 2024
2 parents 3e105cb + d042777 commit 6e57d17
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions whois/whois.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,13 +159,14 @@ def findwhois_iana(tld):
s.close()
return re.search(r"whois:\s+(.*?)\n", response.decode("utf-8")).group(1)

def whois(self, query, hostname, flags, many_results=False, quiet=False):
def whois(self, query, hostname, flags, many_results=False, quiet=False, timeout=10):
"""Perform initial lookup with TLD whois server
then, if the quick flag is false, search that result
for the region-specific whois server and do a lookup
there for contact details. If `quiet` is `True`, will
not send a message to logger when a socket error
is encountered.
is encountered. Uses `timeout` as a number of seconds
to set as a timeout on the socket
"""
response = b""
if "SOCKS" in os.environ:
Expand Down Expand Up @@ -195,7 +196,7 @@ def whois(self, query, hostname, flags, many_results=False, quiet=False):
)
else:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(10)
s.settimeout(timeout)
try: # socket.connect in a try, in order to allow things like looping whois on different domains without
# stopping on timeouts: https://stackoverflow.com/questions/25447803/python-socket-connection-exception
s.connect((hostname, 43))
Expand Down

0 comments on commit 6e57d17

Please sign in to comment.