Skip to content

Commit

Permalink
fix: Properly catch invalid IP passed to IPASN
Browse files Browse the repository at this point in the history
Fix #5
  • Loading branch information
Rafiot committed May 29, 2019
1 parent 47cd0f2 commit 800b964
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions client/pyipasnhistory/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,16 +99,21 @@ def query(self, ip: str, source: str='caida', address_family: str=None,
:param aggregate: (only if more than one response) Aggregare the responses if the prefix and the ASN are the same
'''

if '/' in ip:
# The user passed a prefix... getting the 1st IP in it.
network = ipaddress.ip_network(ip)
first_ip = network[0]
address_family = f'v{first_ip.version}'
ip = str(first_ip)

if not address_family:
ip_parsed = ipaddress.ip_address(ip)
address_family = f'v{ip_parsed.version}'
try:
if '/' in ip:
# The user passed a prefix... getting the 1st IP in it.
network = ipaddress.ip_network(ip)
first_ip = network[0]
address_family = f'v{first_ip.version}'
ip = str(first_ip)

if not address_family:
ip_parsed = ipaddress.ip_address(ip)
address_family = f'v{ip_parsed.version}'
except ValueError:
return {'meta': {'source': source},
'error': f'The IP address is invalid: "{ip}"',
'reponse': {}}

to_query = {'ip': ip, 'source': source, 'address_family': address_family}
if date:
Expand Down

0 comments on commit 800b964

Please sign in to comment.