-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse-root-mirror.py
More file actions
149 lines (138 loc) · 4.2 KB
/
parse-root-mirror.py
File metadata and controls
149 lines (138 loc) · 4.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/usr/bin/env
#coding: utf-8
import string
import yaml
import sys
import urllib
import os
from collections import Counter
rootdict={}
for i in range(97,110):
a=('http://www.root-servers.org/download/%s-root.yml' %chr(i))
akey=a.split('/')[-1]
rootdict[akey]=a
def DownYaml(rootdict,curdir):
for rootyaml,rooturl in rootdict.items():
local = os.path.join(curdir,rootyaml)
urllib.urlretrieve(rooturl,local)
#r = requests.get(rooturl)
#with open(rootyaml,"wb") as f:
# f.write(r.content)
print 'download finished %s' %rootyaml
def ParYaml(localdir,filelist):
'''
将下载出来所有的根镜像文件进行解析成一个完整的列表数据结构
[{'filename':'a-root.yml','info':[{},{}],'filename':'b-root.yml','info':[{},{}]}]
'''
allX =[]
for file in filelist:
filepath=os.path.join(localdir,file)
if filepath.endswith('.yml'):
with open(filepath,'r') as fy:
x = yaml.load(fy)
filedict = {}
for k,v in x.items():
filedict['filename']= file
filedict['info']=x['Instances']
#x['Instances'] is list
allX.append(filedict)
#print allX
return allX
def parX(allinfolist,X):
'''
想做到如果想看某一个根的情况,如果不传rootlist,就代表是全部根的
X代表的是某个字段,比如根据国家Country,Type(Global 还是Local),
[
{
'filename':'a-root.yml',
'info': [{Country:CN,TOWN:CN},{Country:CN,TOWN:CN}]
},
{
'filename':'b-root.yml',
'info': [{Country:CN,TOWN:CN},{Country:CN,TOWN:CN}]
}
]
'''
alltotal = []
for root in allinfolist:
total = {}
Xlist =[]
for infolist in root['info']:
if X in infolist.keys():
Xlist.append(infolist[X])
count_frq ={}
for item in Xlist:
if item in count_frq:
count_frq[item] += 1
else:
count_frq[item] =1
cc = []
for contrydata in count_frq.values():
cc.append(contrydata)
total['filename']=root['filename']
if X=='Sites':
total['nodesitecount']=sum(Xlist)
else:
#elif: X =='Country':
total['Xlistcount']=count_frq
#total['locnt']
total['loccnt'] = sum(cc)
print total
alltotal.append(total)
return alltotal
def overall(allX,filelist):
'''
所有yml文件整体结果进行分析
①:根镜像节点总数(会出现US USA,需要有一个国家代码表来转换)
②:每个国家town数,而不是节点数,有可能一个town有多个site
③:
'''
d3 =[]
for filename in filelist:
for allinfo in allX:
if filename == allinfo['filename']:
d2 = allinfo['Xlistcount']
d3.append(d2)
d4 = {}
for i in d3:
for k,v in i.items():
if k in d4.keys():
d4[k] += v
else:
d4[k] = v
print d4
def contryX():
'''
入口是一个国家简写,输出这个国家的节点数,包括Global和Local
'''
if __name__ == '__main__':
curdir = os.path.split(os.path.realpath(__file__))[0]
localdir = os.path.join(curdir,'rootfile2')
#DownYaml(rootdict,localdir)
filelist = os.listdir(localdir)
#print filelist #['a-root.yml','b-root.yml']
#filepath='E:\\personal-study\\python\\pythonscripts\\rootfile\\m-root.yml'
#filedict = ParYaml(filepath)
#m=parCountry(filedict)
#正式执行
allinfolist=ParYaml(localdir,filelist)
#print allinfolist
#parX(allinfolist,'Town')
print '#####'
alltotal=parX(allinfolist,'Country')
#print '#######'
#print alltotal
hx={}
for l in alltotal:
lx=l['Xlistcount']
hx.update(lx)
X,Y = Counter(lx),Counter(hx)
hx = dict(X+Y)
print hx
#parX(allinfolist,'Sites')
#print '######'
#parX(allinfolist,'State')
#parX(allinfolist,'Type')
#overall(allinfolist,filelist)
#parX(allinfolist,'IPv4')
#parX(allinfolist,'IPv6')