1. 生成一个大文件ips.txt,要求1200行,每行随机为172.25.254.0/24段的ip;
2. 读取ips.txt文件统计这个文件中ip出现频率排前10的ip;
import random
def file(filename):
ips = ['172.25.254.' + str(i) for i in range(1, 255)]
print(ips)
with open(filename, 'a+') as f:
for j in range(1200):
print(random.sample(ips, 1))
f.write(random.sample(ips, 1)[0] + '\n')
def sort_file(filename,count=10):
dict_ip = dict()
with open(filename) as f:
for ip in f:
if ip in dict_ip:
dict_ip[ip] += 1
else:
dict_ip[ip] = 1
sorted_ip = sorted(dict_ip.items(), key=lambda x: x[1],reverse=True)[:count]
return sorted_ip
print(sort_file('jjj.txt'))
sunshinew427
发布了64 篇原创文章 · 获赞 0 · 访问量 421
私信
关注