def ip_in_net(ip, network): ipaddr = int(''.join(['%02x' % int(x) for x in ip.split('.')]), 16) netaddr, bits = network.split('/') netmask = int(''.join(['%02x' % int(x) for x in netaddr.split('.')]), 16) mask = (0xffffffff << (32 - int(bits))) & 0xffffffff return (ipaddr & mask) == (netmask & mask) def human_size(num, suffix=''): for unit in ['', 'K', 'M', 'G', 'T', 'P', 'E', 'Z']: if abs(num) < 1024.0: return "%3.1f %s%s" % (num, unit, suffix) num /= 1024.0 return "%.1f%s%s" % (num, 'Yi', suffix) class FactoryMixin: _instance = None @classmethod def factory(cls, *args, **kwargs): if not cls._instance: cls._instance = cls(*args, **kwargs) return cls._instance