def parse_autoblock(data):
# type: (str) -> set
def _parse(item):
# Try to parse item as a single IP
try:
ipaddress.IPv4Address(item)
return {item}
except ipaddress.AddressValueError:
pass
# Try parse item as ip range: ip1-ip2
try:
first_ip, last_ip = [utils.ip2int(i) for i in item.split('-')]
return {utils.int2ip(n) for n in range(first_ip, last_ip + 1)}
except ValueError:
raise APIError(
'Exclude IP\'s are expected to be in the form of '
'10.0.0.1,10.0.0.4 or 10.1.0.10-10.1.1.54 or both '
'comma-separated')
ip_sets = (_parse(unicode(d)) for d in data.split(','))
return reduce(operator.or_, ip_sets)
评论列表
文章目录