match IPs more robustly and multiple per line
This commit is contained in:
parent
70308230e6
commit
921c51e118
1 changed files with 15 additions and 8 deletions
|
@ -1,17 +1,24 @@
|
|||
import re
|
||||
|
||||
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):
|
||||
# TODO: make more robust. (regex pls)
|
||||
for ip in f.readlines():
|
||||
if "#" in ip:
|
||||
ip, _, _ = ip.partition("#")
|
||||
ip = ip.strip()
|
||||
if ip.count(".") != 3:
|
||||
continue
|
||||
yield ip
|
||||
for line in f.readlines():
|
||||
line, _, _ = line.partition("#") # ignore comments
|
||||
for match in ipv4_pattern_strict.finditer(line):
|
||||
yield match.group() # yield the entire string
|
||||
|
||||
|
||||
def addr_to_int(ip):
|
||||
|
|
Loading…
Reference in a new issue