I know there are some similar questions up here, but they mostly either want to find the range itself (which uses some libraries, like the example that stackoverflow says is a dupe of my question) and is in another language.
I have a way to convert the subnet into the beginning and the end of the range of ip's in a subnet (okay, bad wording, it's simply like
220.127.116.11/16 -> (18.104.22.168 , 22.214.171.124)
ip_range = ('126.96.36.199', '188.8.131.52')
if '184.108.40.206' >= ip_range and '220.127.116.11' <= ip_range:
You can't really do string comparisons on a dot separated list of numbers because your test will simply fail on input say
'9' is simply greater than
>>> '18.104.22.168' < '22.214.171.124' False
So instead you can convert the input into tuples of integers through comprehension expression
def convert_ipv4(ip): return tuple(int(n) for n in ip.split('.'))
Note the lack of type checking, but if your input is a proper IP address it will be fine. Since you have a 2-tuple of IP addresses, you can create a function that takes both start and end as argument, pass that tuple in through argument list, and return that with just one statement (as Python allows chaining of comparisons). Perhaps like:
def check_ipv4_in(addr, start, end): return convert_ipv4(start) < convert_ipv4(addr) < convert_ipv4(end)
Test it out.
>>> ip_range = ('126.96.36.199', '188.8.131.52') >>> check_ipv4_in('184.108.40.206', *ip_range) True
With this method you can lazily expand it to IPv6, though the conversion to and from hex (instead of int) will be needed instead.