Skip to content

Commit

Permalink
Merge pull request #238 from RideTheSkyP/findwhois_iana_proxy
Browse files Browse the repository at this point in the history
findwhois_iana with proxy
  • Loading branch information
richardpenman authored Sep 4, 2024
2 parents 34b04e0 + 033a1d3 commit 4efeff8
Showing 1 changed file with 29 additions and 24 deletions.
53 changes: 29 additions & 24 deletions whois/whois.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,30 +146,7 @@ def findwhois_server(buf, hostname, query):
return nhost

@staticmethod
def findwhois_iana(tld):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(10)
s.connect(("whois.iana.org", 43))
s.send(bytes(tld, "utf-8") + b"\r\n")
response = b""
while True:
d = s.recv(4096)
response += d
if not d:
break
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, 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. Uses `timeout` as a number of seconds
to set as a timeout on the socket
"""
response = b""
def get_socket():
if "SOCKS" in os.environ:
try:
import socks
Expand Down Expand Up @@ -197,6 +174,34 @@ def whois(self, query, hostname, flags, many_results=False, quiet=False, timeout
)
else:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
return s


def findwhois_iana(self, tld):
s = self.get_socket()
s.settimeout(10)
s.connect(("whois.iana.org", 43))
s.send(bytes(tld, "utf-8") + b"\r\n")
response = b""
while True:
d = s.recv(4096)
response += d
if not d:
break
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, 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. Uses `timeout` as a number of seconds
to set as a timeout on the socket
"""
response = b""
s = self.get_socket()
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
Expand Down

0 comments on commit 4efeff8

Please sign in to comment.