Server IP : 85.214.239.14 / Your IP : 18.117.170.80 Web Server : Apache/2.4.62 (Debian) System : Linux h2886529.stratoserver.net 4.9.0 #1 SMP Tue Jan 9 19:45:01 MSK 2024 x86_64 User : www-data ( 33) PHP Version : 7.4.18 Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare, MySQL : OFF | cURL : OFF | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : OFF Directory : /proc/3/cwd/proc/3/root/usr/lib/python3/dist-packages/netaddr/contrib/ |
Upload File : |
#----------------------------------------------------------------------------- # Copyright (c) 2008 by David P. D. Moss. All rights reserved. # # Released under the BSD license. See the LICENSE file for details. #----------------------------------------------------------------------------- from netaddr.ip import IPNetwork, cidr_exclude, cidr_merge class SubnetSplitter(object): """ A handy utility class that takes a single (large) subnet and allows smaller subnet within its range to be extracted by CIDR prefix. Any leaving address space is available for subsequent extractions until all space is exhausted. """ def __init__(self, base_cidr): """ Constructor. :param base_cidr: an IPv4 or IPv6 address with a CIDR prefix. (see IPNetwork.__init__ for full details). """ self._subnets = set([IPNetwork(base_cidr)]) def extract_subnet(self, prefix, count=None): """Extract 1 or more subnets of size specified by CIDR prefix.""" for cidr in self.available_subnets(): subnets = list(cidr.subnet(prefix, count=count)) if not subnets: continue self.remove_subnet(cidr) self._subnets = self._subnets.union( set( cidr_exclude(cidr, cidr_merge(subnets)[0]) ) ) return subnets return [] def available_subnets(self): """Returns a list of the currently available subnets.""" return sorted(self._subnets, key=lambda x: x.prefixlen, reverse=True) def remove_subnet(self, ip_network): """Remove a specified IPNetwork from available address space.""" self._subnets.remove(ip_network)