from collections import Iterable from ipaddress import IPv4Address, IPv4Network class IpList(object): def __init__(self, addresses): self.addresses = [IPv4Network(address) if isinstance(address, Iterable) else IPv4Address(address) for address in addresses] def __contains__(self, ip): ip = IPv4Address(ip) for address in self.addresses: if isinstance(address, IPv4Address): if ip == address: return True if isinstance(address, IPv4Network): if ip in address: return True return False INTERNAL_IPS = IpList([ '127.0.0.1', '192.168.0.0/24' ])