Skip to content

Commit

Permalink
Replaced urllib with httpx. Supports HTTP/2, more convenient and secure.
Browse files Browse the repository at this point in the history
Fixes exception raising.
  • Loading branch information
KOLANICH committed Feb 16, 2021
1 parent 212d4c2 commit f0a3b4a
Show file tree
Hide file tree
Showing 13 changed files with 102 additions and 186 deletions.
8 changes: 4 additions & 4 deletions ipwhois/asn.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ def parse_fields_dns(self, response):
except Exception as e:

raise ASNParseError('Parsing failed for "{0}" with exception: {1}.'
''.format(response, e)[:100])
''.format(response, e)[:100]) from e

return ret

Expand Down Expand Up @@ -222,7 +222,7 @@ def parse_fields_verbose_dns(self, response):
except Exception as e:

raise ASNParseError('Parsing failed for "{0}" with exception: {1}.'
''.format(response, e)[:100])
''.format(response, e)[:100]) from e

return ret

Expand Down Expand Up @@ -279,7 +279,7 @@ def parse_fields_whois(self, response):
except Exception as e:

raise ASNParseError('Parsing failed for "{0}" with exception: {1}.'
''.format(response, e)[:100])
''.format(response, e)[:100]) from e

return ret

Expand Down Expand Up @@ -379,7 +379,7 @@ def parse_fields_http(self, response, extra_org_map=None):
except Exception as e: # pragma: no cover

raise ASNParseError('Parsing failed for "{0}" with exception: {1}.'
''.format(response, e)[:100])
''.format(response, e)[:100]) from e

return asn_data

Expand Down
28 changes: 14 additions & 14 deletions ipwhois/experimental.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,16 +103,16 @@ def get_bulk_asn_whois(addresses=None, retry_count=3, timeout=120):

else:

raise ASNLookupError('ASN bulk lookup failed.')
raise ASNLookupError('ASN bulk lookup failed.') from e

except: # pragma: no cover
except BaseException as e: # pragma: no cover

raise ASNLookupError('ASN bulk lookup failed.')
raise ASNLookupError('ASN bulk lookup failed.') from e


def bulk_lookup_rdap(addresses=None, inc_raw=False, retry_count=3, depth=0,
excluded_entities=None, rate_limit_timeout=60,
socket_timeout=10, asn_timeout=240, proxy_openers=None):
socket_timeout=10, asn_timeout=240, http_clients=None):
"""
The function for bulk retrieving and parsing whois information for a list
of IP addresses via HTTP (RDAP). This bulk lookup method uses bulk
Expand All @@ -138,8 +138,8 @@ def bulk_lookup_rdap(addresses=None, inc_raw=False, retry_count=3, depth=0,
connections in seconds. Defaults to 10.
asn_timeout (:obj:`int`): The default timeout for bulk ASN lookups in
seconds. Defaults to 240.
proxy_openers (:obj:`list` of :obj:`OpenerDirector`): Proxy openers
for single/rotating proxy support. Defaults to None.
http_clients (:obj:`list` of :obj:`httpx.Client`): httpx clients
for single/rotating proxy and fingerprint support. Defaults to None.
Returns:
namedtuple:
Expand Down Expand Up @@ -209,11 +209,11 @@ def bulk_lookup_rdap(addresses=None, inc_raw=False, retry_count=3, depth=0,
}
asn_parsed_results = {}

if proxy_openers is None:
if http_clients is None:

proxy_openers = [None]
http_clients = [None]

proxy_openers_copy = iter(proxy_openers)
http_clients_copy = iter(http_clients)

# Make sure addresses is unique
unique_ip_list = list(unique_everseen(addresses))
Expand Down Expand Up @@ -347,19 +347,19 @@ def bulk_lookup_rdap(addresses=None, inc_raw=False, retry_count=3, depth=0,

rate_tracker[rir]['count'] += 1

# Get the next proxy opener to use, or None
# Get the next HTTP client object to use, or None
try:

opener = next(proxy_openers_copy)
client = next(http_clients_copy)

# Start at the beginning if all have been used
except StopIteration:

proxy_openers_copy = iter(proxy_openers)
opener = next(proxy_openers_copy)
http_clients_copy = iter(http_clients)
client = next(http_clients_copy)

# Instantiate the objects needed for the RDAP lookup
net = Net(ip, timeout=socket_timeout, proxy_opener=opener)
net = Net(ip, timeout=socket_timeout, http_client=client)
rdap = RDAP(net)

try:
Expand Down
10 changes: 5 additions & 5 deletions ipwhois/ipwhois.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ class IPWhois:
An IPv4 or IPv6 address
timeout (:obj:`int`): The default timeout for socket connections in
seconds. Defaults to 5.
proxy_opener (:obj:`urllib.request.OpenerDirector`): The request for
proxy support. Defaults to None.
http_client (:obj:`httpx.Client`): HTTP client object. Proxies are here.
Defaults to None.
"""

def __init__(self, address, timeout=5, proxy_opener=None):
def __init__(self, address, timeout=5, http_client=None):

self.net = Net(
address=address, timeout=timeout, proxy_opener=proxy_opener
address=address, timeout=timeout, http_client=http_client
)
self.ipasn = IPASN(self.net)

Expand All @@ -61,7 +61,7 @@ def __init__(self, address, timeout=5, proxy_opener=None):
def __repr__(self):

return 'IPWhois({0}, {1}, {2})'.format(
self.address_str, str(self.timeout), repr(self.net.opener)
self.address_str, str(self.timeout), repr(self.net.http_client)
)

def lookup_whois(self, inc_raw=False, retry_count=3, get_referral=False,
Expand Down
Loading

0 comments on commit f0a3b4a

Please sign in to comment.