From e124417a7f36caf2f061764cbce082bdb3959b79 Mon Sep 17 00:00:00 2001 From: Minoplhy Date: Thu, 12 Aug 2021 16:50:43 +0700 Subject: [PATCH] Turn Startswith into Regex cause startswith replace cause problem with lot domains --- to-rpz/host_rpz_argv.py | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/to-rpz/host_rpz_argv.py b/to-rpz/host_rpz_argv.py index 7779155..86cf7e1 100644 --- a/to-rpz/host_rpz_argv.py +++ b/to-rpz/host_rpz_argv.py @@ -6,22 +6,17 @@ from shutil import copyfile infile = sys.argv[1] outfile = sys.argv[2] -a = ['0.0.0.0 ','0.0.0.0','::1 ','127.0.0.1 ','0','::','::1','127.0.0.1','0','::'] -lst = [] - -with open(infile, 'r') as f: - for line in f: - for word in a: - if word in line and not line.startswith('#'): - line = line.replace(word,'') - else: - line = line.replace(line, line) - lst.append(line) -f.close() -with open(infile, 'w') as f: - for line in lst: - f.write(line) -f.close() +with open(infile) as f: + file = f.read().split('\n') + for i in range(len(file)): + file[i] = re.sub('^127.0.0.1 ', ' ', file[i]) + file[i] = re.sub('^0.0.0.0 ', '', file[i]) + file[i] = re.sub('^0 ', '', file[i]) + file[i] = re.sub('^:: ', '', file[i]) + file[i] = re.sub('^::1 ', '' ,file[i]) +with open(infile, 'w') as f1: + f1.writelines(["%s\n" % item for item in file]) +f.close() with open(infile) as f: file = f.read().split('\n')