3 ms·
I have done this in code that I have written (yes, IPV6 - haven't seen it yet and it has been talked about for years) that handles IP address. One thing that i
by brianobush 17y ago
I have done this in code that I have written (yes, IPV6 - haven't seen it yet and it has been talked about for years) that handles IP address.
One thing that is really easy once the ip address (x) is in the integer space: private address determination becomes a simple integer comparison. e.g.,
0.0.0.0 is simply x > 0,
in the range 192.168.0.0 to 192.168.255.255 is written as: x >= 3232235520 && x <= 3232301055, etc.
- tghw 17y agoOr, much more easier for anyone else reading your code: ip.startswith('192.168.') or ip.startswith('10.') No, it's not as fast, but are you really doing that many is_ip_private() calls?
- tptacek 17y agoThat works for /16's and /8's. Now do it for /27.
- ars 17y agoGrab the last octet, and see if it's <= 30 Personally I store a string until 128bit math becomes easier (so I can handle IPv6). But usually I just want to log it, not check netmasks.
- tptacek 17y agoThere are, what, 8 /27s in a /24? How does seeing if the last octet is less than 30 help you?