代码优化&新增ip
This commit is contained in:
parent
1ad185c72f
commit
8bb3edc89d
1
ipdb.txt
1
ipdb.txt
|
|
@ -47,5 +47,6 @@
|
|||
221.6.171.0/24
|
||||
221.7.251.0/24
|
||||
221.205.168.0/23
|
||||
222.94.227.0/24
|
||||
222.125.57.0/24
|
||||
222.189.163.0/24
|
||||
57
main.py
57
main.py
|
|
@ -3,29 +3,36 @@
|
|||
# 将ipdb.txt备份为ipdb_日期_时间.txt 格式
|
||||
import datetime
|
||||
|
||||
date = datetime.datetime.now().strftime('%Y-%m-%d-%H-%M-%S')
|
||||
with open('ipdb.txt', 'r') as f:
|
||||
lines = f.readlines()
|
||||
with open(f'ipdb_{date}.txt', 'w') as f:
|
||||
f.write(''.join(lines))
|
||||
print('备份完成!')
|
||||
def main(filename='ipdb.txt'):
|
||||
date = datetime.datetime.now().strftime('%Y-%m-%d-%H-%M-%S')
|
||||
with open(filename, 'r') as f:
|
||||
lines = f.readlines()
|
||||
print(f"{filename.split('.')[0]}_{date}.txt")
|
||||
with open(f"{filename.split('.')[0]}_{date}.txt", 'w') as f:
|
||||
f.write(''.join(lines))
|
||||
print('备份完成!')
|
||||
|
||||
with open('ipdb.txt', 'r') as f:
|
||||
# 读取内容,并将每行的数据进行排序
|
||||
lines = f.readlines()
|
||||
lines_without_newlines = [line.strip() for line in lines]
|
||||
# 对lines_without_newlines进行去重
|
||||
lines_without_newlines = list(set(lines_without_newlines))
|
||||
# 剔除lines_without_newlines中空的元素和#开头的元素
|
||||
lines_without_newlines = [line for line in lines_without_newlines if line and not line.startswith('#')]
|
||||
# 将ip清单中的每个ip地址按照点分十进制进行排序
|
||||
lines_without_newlines.sort(key=lambda x: tuple(map(int, x.split('/')[0].split('.'))))
|
||||
# 在lines_without_newlines开头插入新元素
|
||||
lines_without_newlines.insert(0, '# IP段清单')
|
||||
print(lines_without_newlines)
|
||||
# 打印lines_without_newlines的长度
|
||||
print(len(lines_without_newlines))
|
||||
# 将排序后的内容写入文件,要求每个元素一行
|
||||
with open('ipdb.txt', 'w') as f:
|
||||
f.write('\n'.join(lines_without_newlines))
|
||||
print('排序完成,更新完成!')
|
||||
with open(filename, 'r') as f:
|
||||
# 读取内容,并将每行的数据进行排序
|
||||
lines = f.readlines()
|
||||
lines_without_newlines = [line.strip() for line in lines]
|
||||
# 对lines_without_newlines进行去重
|
||||
lines_without_newlines = list(set(lines_without_newlines))
|
||||
# 剔除lines_without_newlines中空的元素和#开头的元素
|
||||
lines_without_newlines = [line for line in lines_without_newlines if line and not line.startswith('#')]
|
||||
# 将ip清单中的每个ip地址按照点分十进制进行排序
|
||||
lines_without_newlines.sort(key=lambda x: tuple(map(int, x.split('/')[0].split('.'))))
|
||||
# 在lines_without_newlines开头插入新元素
|
||||
lines_without_newlines.insert(0, '# IP段清单')
|
||||
print(lines_without_newlines)
|
||||
# 打印lines_without_newlines的长度
|
||||
print(len(lines_without_newlines))
|
||||
# 将排序后的内容写入文件,要求每个元素一行
|
||||
with open(filename, 'w') as f:
|
||||
f.write('\n'.join(lines_without_newlines))
|
||||
print('排序完成,更新完成!')
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Loading…
Reference in New Issue