match IPs more robustly and multiple per line
This commit is contained in:
parent
70308230e6
commit
921c51e118
|
@ -1,17 +1,24 @@
|
||||||
import re
|
import re
|
||||||
|
|
||||||
ipv4_pattern = re.compile(r"(\d+)\.(\d+)\.(\d+)\.(\d+)", re.ASCII)
|
ipv4_pattern = re.compile(r"(\d+)\.(\d+)\.(\d+)\.(\d+)", re.ASCII)
|
||||||
|
ipv4_pattern_strict = re.compile(r"""
|
||||||
|
(?:^|(?<=[^\d.]))
|
||||||
|
(0|1\d\d|2[0-4]\d|25[0-5]|[1-9]\d?)
|
||||||
|
\.
|
||||||
|
(0|1\d\d|2[0-4]\d|25[0-5]|[1-9]\d?)
|
||||||
|
\.
|
||||||
|
(0|1\d\d|2[0-4]\d|25[0-5]|[1-9]\d?)
|
||||||
|
\.
|
||||||
|
(0|1\d\d|2[0-4]\d|25[0-5]|[1-9]\d?)
|
||||||
|
(?:$|(?=[^\d.]))
|
||||||
|
""", re.ASCII | re.VERBOSE)
|
||||||
|
|
||||||
|
|
||||||
def read_ips(f):
|
def read_ips(f):
|
||||||
# TODO: make more robust. (regex pls)
|
for line in f.readlines():
|
||||||
for ip in f.readlines():
|
line, _, _ = line.partition("#") # ignore comments
|
||||||
if "#" in ip:
|
for match in ipv4_pattern_strict.finditer(line):
|
||||||
ip, _, _ = ip.partition("#")
|
yield match.group() # yield the entire string
|
||||||
ip = ip.strip()
|
|
||||||
if ip.count(".") != 3:
|
|
||||||
continue
|
|
||||||
yield ip
|
|
||||||
|
|
||||||
|
|
||||||
def addr_to_int(ip):
|
def addr_to_int(ip):
|
||||||
|
|
Loading…
Reference in New Issue
Block a user