diff --git a/whois/whois.py b/whois/whois.py index deef59e..64a3691 100644 --- a/whois/whois.py +++ b/whois/whois.py @@ -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 @@ -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