Refactor IP-range searching code for better reusability.

Useful for util scripts etc.
This commit is contained in:
Neil Williams
2012-08-06 17:35:07 -07:00
parent 768663b032
commit b75f6f26b6

View File

@@ -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))