Created
August 13, 2012 09:45
-
-
Save chuangbo/3338813 to your computer and use it in GitHub Desktop.
Check if ip in cidr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#-*- coding:utf-8 -*- | |
# Author: @chuangbo | |
# For: @likexian | |
import socket | |
import struct | |
class CIDR: | |
'''Check if ip in cidr | |
>>> '8.8.8.8' in CIDR('8.8.8.8', 8) | |
True | |
>>> '8.8.7.8' in CIDR('8.8.8.8', 8) | |
False | |
''' | |
# bin(_MASK) = 0b11111111111111111111111111111111,32位1 | |
_MASK = (1 << 32) - 1 | |
def __init__(self, ip, mask): | |
ip = self.ip2int(ip) | |
mask = self._MASK << mask | |
self.cidr = ip & mask | |
@staticmethod | |
def ip2int(str): | |
return struct.unpack("!I",socket.inet_aton(str))[0] | |
def __contains__(self, ip): | |
return self.ip2int(ip) & self.cidr == self.cidr | |
if __name__ == '__main__': | |
import doctest | |
doctest.testmod() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment