Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for passing a timeout through to the whois query #117

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pythonwhois/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from . import net, parse

def get_whois(domain, normalized=[]):
raw_data, server_list = net.get_whois_raw(domain, with_server_list=True)
def get_whois(domain, normalized=[], timeout=None):
raw_data, server_list = net.get_whois_raw(domain, with_server_list=True, timeout=None)
# Unlisted handles will be looked up on the last WHOIS server that was queried. This may be changed to also query
# other servers in the future, if it turns out that there are cases where the last WHOIS server in the chain doesn't
# actually hold the handle contact details, but another WHOIS server in the chain does.
Expand Down
15 changes: 8 additions & 7 deletions pythonwhois/net.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from codecs import encode, decode
from . import shared

def get_whois_raw(domain, server="", previous=None, rfc3490=True, never_cut=False, with_server_list=False, server_list=None):
def get_whois_raw(domain, server="", previous=None, rfc3490=True, never_cut=False, with_server_list=False, server_list=None, timeout=None):
previous = previous or []
server_list = server_list or []
# Sometimes IANA simply won't give us the right root WHOIS server
Expand Down Expand Up @@ -30,7 +30,7 @@ def get_whois_raw(domain, server="", previous=None, rfc3490=True, never_cut=Fals
target_server = exc_serv
break
if is_exception == False:
target_server = get_root_server(domain)
target_server = get_root_server(domain, timeout=timeout)
else:
target_server = server
if target_server == "whois.jprs.jp":
Expand All @@ -41,7 +41,7 @@ def get_whois_raw(domain, server="", previous=None, rfc3490=True, never_cut=Fals
request_domain = "=%s" % domain # Avoid partial matches
else:
request_domain = domain
response = whois_request(request_domain, target_server)
response = whois_request(request_domain, target_server, timeout=timeout)
if never_cut:
# If the caller has requested to 'never cut' responses, he will get the original response from the server (this is
# useful for callers that are only interested in the raw data). Otherwise, if the target is verisign-grs, we will
Expand All @@ -66,24 +66,25 @@ def get_whois_raw(domain, server="", previous=None, rfc3490=True, never_cut=Fals
referal_server = match.group(2)
if referal_server != server and "://" not in referal_server: # We want to ignore anything non-WHOIS (eg. HTTP) for now.
# Referal to another WHOIS server...
return get_whois_raw(domain, referal_server, new_list, server_list=server_list, with_server_list=with_server_list)
return get_whois_raw(domain, referal_server, new_list, server_list=server_list, with_server_list=with_server_list, timeout=timeout)
if with_server_list:
return (new_list, server_list)
else:
return new_list

def get_root_server(domain):
data = whois_request(domain, "whois.iana.org")
def get_root_server(domain, timeout=None):
data = whois_request(domain, "whois.iana.org", timeout=timeout)
for line in [x.strip() for x in data.splitlines()]:
match = re.match("refer:\s*([^\s]+)", line)
if match is None:
continue
return match.group(1)
raise shared.WhoisException("No root WHOIS server found for domain.")

def whois_request(domain, server, port=43):
def whois_request(domain, server, port=43, timeout=None):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((server, port))
sock.settimeout(timeout)
sock.send(("%s\r\n" % domain).encode("utf-8"))
buff = b""
while True:
Expand Down