A Data Structure for efficiently storing, removing and checking all Ipv4 addresses in O(1) time.
- pytest
from ip_model.Ipv4 import Ipv4
blacklist = Ipv4()
# arg: String
# returns: True
blacklist.add("192.0.0.18")
# arg: String
# returns: removed IP
blacklist.remove("192.0.0.18")
# arg: String
# returns: bool
blacklist.is_present("192.0.0.18")
from ip_model.Ipv4 import Ipv6
blacklist = Ipv6()
# arg: String
# returns: True
blacklist.add("192::18")
# arg: String
# returns: removed IP
blacklist.remove("192:ffff:e034::23")
# arg: String
# returns: bool
blacklist.is_present("::5:4fed")
# arg: String
# returns: True
blacklist.add_cidr("192.92.53.0/24")
# arg: String
# returns: True
blacklist.add_cidr("192.92.53.0/255.255.255.252")
# arg: String
# returns: removed CIDR
blacklist.remove_cidr("192.92.53.0/24")
# arg: String
# returns: True
blacklist.add_cidr("8653:53fe::/122/24")
# arg: String
# returns: removed CIDR
blacklist.remove_cidr("8653:53fe::/122")
Note:
The tradeoff is, the call for how to handle with overlapping CIDR's must be taken by the service using the DS. The DataStructure only performs the requested operation with the given data
Handles both Ipv4 and Ipv6
from ip_model.Ip import Ip
blacklist = Ip()
# arg: String
# returns: True
blacklist.add("192.0.0.18")
blacklist.add("192::18")
# arg: String
# returns: removed IP
blacklist.remove("192.0.0.18")
blacklist.remove("1924::18")
- Throws
TypeError
: passing Invalid Datatype, incorrect number of arguments - Throws
InvalidIpException
: When an invalid Ip is passed
from ip_model.Exceptions import InvalidIpException
try:
blacklist.add("192.455.554.343")
except InvalidIpException:
print("Incorrect Ipv4 Address")
# For CIDR
try:
blacklist.add("192.12.65.0/16")
except InvalidIpException:
print("Incorrect CIDR")
mkdir ip-model && cd ip-model
git clone git@github.com:rakesht2499/ip_model.git
# -or -
git clone https://github.com/rakesht2499/ip_model.git
cd tests/
pytest
cd tests/
pytest --html=report.html
Click here for ip-model: Under the Hood