From b75f6f26b66daed708a2b71c29191849239bf115 Mon Sep 17 00:00:00 2001 From: Neil Williams Date: Mon, 6 Aug 2012 17:35:07 -0700 Subject: [PATCH] Refactor IP-range searching code for better reusability. Useful for util scripts etc. --- r2/r2/lib/utils/utils.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/r2/r2/lib/utils/utils.py b/r2/r2/lib/utils/utils.py index fae0c3f69..f47f5b57f 100644 --- a/r2/r2/lib/utils/utils.py +++ b/r2/r2/lib/utils/utils.py @@ -1386,10 +1386,15 @@ def summarize_markdown(md): return first_graf[:500] +def find_containing_network(ip_ranges, address): + """Find an IP network that contains the given address.""" + addr = ipaddress.ip_address(address) + for network in ip_ranges: + if addr in network: + return network + return None + + def is_throttled(address): """Determine if an IP address is in a throttled range.""" - addr = ipaddress.ip_address(address) - for network in g.throttles: - if addr in network: - return True - return False + return bool(find_containing_network(g.throttles, address))