1
- import ipaddress
2
1
import json
3
2
from ipaddress import IPv4Address , IPv4Interface , IPv4Network
4
3
from typing import Dict , List
@@ -12,11 +11,15 @@ class IPAddressPool:
12
11
def __init__ (self , ipaddr_db_json_path : str ):
13
12
self .json_path = ipaddr_db_json_path
14
13
self ._ip_version = 4
15
- self ._prefix_len = 0
14
+ self ._network = None
16
15
self ._ipaddr_pool = []
17
16
self ._hostnames = {}
18
17
self ._load_from_json (ipaddr_db_json_path )
19
18
19
+ @property
20
+ def prefixlen (self ):
21
+ return self ._network .prefixlen
22
+
20
23
@property
21
24
def addresses (self ) -> List [IPv4Address ]:
22
25
addresses = list (self ._ipaddr_pool )
@@ -33,10 +36,11 @@ def initialize(self, ipv4_cidr_address: str):
33
36
if self .is_initialized ():
34
37
raise IPAddressPoolException ("Address pool already initialized" )
35
38
36
- prefix_len , address_list = self ._generate_addresses (ipv4_cidr_address )
37
-
39
+ iface = IPv4Interface (ipv4_cidr_address )
40
+ network : IPv4Network = iface .network
41
+ address_list = list (network .hosts ())
42
+ self ._network = network
38
43
self ._ipaddr_pool = address_list
39
- self ._prefix_len = prefix_len
40
44
self ._save ()
41
45
42
46
def new_address (self , hostname : str ) -> IPv4Address :
@@ -112,10 +116,3 @@ def _load_from_json(self, json_path):
112
116
self ._initialize_from_dict (loaded )
113
117
except FileNotFoundError :
114
118
pass
115
-
116
- def _generate_addresses (self , cidr_address ):
117
- iface : IPv4Interface = ipaddress .ip_interface (cidr_address )
118
- network : IPv4Network = iface .network
119
- prefix_len = network .prefixlen
120
- hosts = list (network .hosts ())
121
- return prefix_len , hosts
0 commit comments