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

Allow querying RDAP IP Networks #252

Closed
wants to merge 4 commits into from
Closed
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
36 changes: 30 additions & 6 deletions ipwhois/net.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@
if sys.version_info >= (3, 3): # pragma: no cover
from ipaddress import (ip_address,
IPv4Address,
IPv6Address)
IPv6Address,
ip_network,
IPv4Network,
IPv6Network)
else: # pragma: no cover
from ipaddr import (IPAddress as ip_address,
IPv4Address,
Expand Down Expand Up @@ -117,10 +120,15 @@ def __init__(self, address, timeout=5, proxy_opener=None,

# IPv4Address or IPv6Address
if isinstance(address, IPv4Address) or isinstance(
address, IPv6Address):
address, IPv6Address) or isinstance(
address, IPv4Network) or isinstance(address, IPv6Network):

self.address = address

elif isinstance(address, str) and '/' in address:

self.address = ip_network(address)

else:

# Use ipaddress package exception handling.
Expand Down Expand Up @@ -161,8 +169,16 @@ def __init__(self, address, timeout=5, proxy_opener=None,

if self.version == 4:

# Check if no ASN/whois resolution needs to occur.
is_defined = ipv4_is_defined(self.address_str)
if isinstance(self.address, IPv4Network):

# Check if no ASN/whois resolution needs to occur with the
# first IP in the subnet.
is_defined = ipv4_is_defined(next(self.address.hosts()))

else:

# Check if no ASN/whois resolution needs to occur.
is_defined = ipv4_is_defined(self.address_str)

if is_defined[0]:

Expand All @@ -182,8 +198,16 @@ def __init__(self, address, timeout=5, proxy_opener=None,

else:

# Check if no ASN/whois resolution needs to occur.
is_defined = ipv6_is_defined(self.address_str)
if isinstance(self.address, IPv6Network):

# Check if no ASN/whois resolution needs to occur with the
# first IP in the subnet.
is_defined = ipv6_is_defined(next(self.address.hosts()))

else:

# Check if no ASN/whois resolution needs to occur.
is_defined = ipv6_is_defined(self.address_str)

if is_defined[0]:

Expand Down
16 changes: 14 additions & 2 deletions ipwhois/tests/test_net.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,35 @@ class TestNet(TestCommon):
def test_ip_invalid(self):
self.assertRaises(ValueError, Net, '192.168.0.256')
self.assertRaises(ValueError, Net, 'fe::80::')
self.assertRaises(ValueError, Net, '192.256.0.0/16')
self.assertRaises(ValueError, Net, 'fe::80::/10')

def test_ip_defined(self):
if sys.version_info >= (3, 3):
from ipaddress import (IPv4Address, IPv6Address)
from ipaddress import (IPv4Address, IPv6Address,
IPv4Network, IPv6Network)
else:
from ipaddr import (IPv4Address, IPv6Address)
from ipaddr import (IPv4Address, IPv6Address,
IPv4Network, IPv6Network)

self.assertRaises(IPDefinedError, Net, '192.168.0.1')
self.assertRaises(IPDefinedError, Net, 'fe80::')
self.assertRaises(IPDefinedError, Net, IPv4Address('192.168.0.1'))
self.assertRaises(IPDefinedError, Net, IPv6Address('fe80::'))
self.assertRaises(IPDefinedError, Net, '192.168.0.0/16')
self.assertRaises(IPDefinedError, Net, 'fe80::/10')
self.assertRaises(IPDefinedError, Net, IPv4Network('192.168.0.0/16'))
self.assertRaises(IPDefinedError, Net, IPv6Network('fe80::/10'))

def test_ip_version(self):
result = Net('74.125.225.229')
self.assertEqual(result.version, 4)
result = Net('2001:4860:4860::8888')
self.assertEqual(result.version, 6)
result = Net('8.8.8.0/24')
self.assertEqual(result.version, 4)
result = Net('2001:4860::/32')
self.assertEqual(result.version, 6)

def test_timeout(self):
result = Net('74.125.225.229')
Expand Down