一个Python包,用于有效地存储、删除和检查IP,用例:黑名单、白名单等
ip-model的Python项目详细描述
Ip模型
一种在O(1)时间内有效地存储、删除和检查所有Ipv4地址的数据结构。在
先决条件
- pytest公司
使用
单个Ip
Ipv4
fromip_model.Ipv4importIpv4blacklist=Ipv4()
添加IP:
^{pr2}$删除IP:
# arg: String# returns: removed IPblacklist.remove("192.0.0.18")
检查IP是否存在:
# arg: String# returns: boolblacklist.is_present("192.0.0.18")
Ipv6
fromip_model.Ipv4importIpv6blacklist=Ipv6()
添加IP:
# arg: String# returns: Trueblacklist.add("192::18")
删除IP:
# arg: String# returns: removed IPblacklist.remove("192:ffff:e034::23")
检查IP是否存在:
# arg: String# returns: boolblacklist.is_present("::5:4fed")
CIDR公司
Ipv4:
添加
# arg: String# returns: Trueblacklist.add_cidr("192.92.53.0/24")
# arg: String# returns: Trueblacklist.add_cidr("192.92.53.0/255.255.255.252")
删除:
# arg: String# returns: removed CIDRblacklist.remove_cidr("192.92.53.0/24")
Ipv6:
添加
# arg: String# returns: Trueblacklist.add_cidr("8653:53fe::/122/24")
删除:
# arg: String# returns: removed CIDRblacklist.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
Ip等级(来自v3.0.0)
同时处理Ipv4和Ipv6
fromip_model.IpimportIpblacklist=Ip()
添加IP:
# arg: String# returns: Trueblacklist.add("192.0.0.18")blacklist.add("192::18")
删除IP:
# arg: String# returns: removed IPblacklist.remove("192.0.0.18")blacklist.remove("1924::18")
异常处理
- 抛出
TypeError
:传递的数据类型无效,参数数目不正确 - 抛出
InvalidIpException
:传递无效Ip时
fromip_model.ExceptionsimportInvalidIpExceptiontry:blacklist.add("192.455.554.343")exceptInvalidIpException:print("Incorrect Ipv4 Address")# For CIDRtry:blacklist.add("192.12.65.0/16")exceptInvalidIpException:print("Incorrect CIDR")
变更日志
- 3.0.0版
引入IP类 add、remove、add_cidr、remove\u cidr的返回类型已更改
- v2.0.1版
修复Ipv6中的错误
- 2.0.0版
添加对Ipv6地址的支持&CIDR
- 1.1.0版
添加对Ipv4 CIDR范围的支持
- 1.0.0版
引入支持单个Ipv4地址的Ip模型
- 项目
标签: