-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconv.py
executable file
·58 lines (43 loc) · 1.13 KB
/
conv.py
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
#!/usr/bin/env python3
import sqlite3
from cz_ip import ip_to_int
def extract(s):
"""
8.8.8.8 8.8.8.8 美国 加利福尼亚州圣克拉拉县山景市谷歌公司DNS服务器
8.8.8.9 8.8.8.255 美国 加利福尼亚州圣克拉拉县山景市谷歌公司
...
"""
b, e, desc = s.split(None, 2)
return (ip_to_int(b), ip_to_int(e), desc.replace("CZ88.NET", "").strip())
def check(ips):
n = -1
cnt = 0
for ip in ips:
if not ip.strip():
break
cnt += 1
b, e, _ = extract(ip)
assert b == n + 1
n = e
return cnt, ips[-1]
def main():
with open('ip.txt', encoding="gbk") as f:
ips = list(f)
check(ips)
con = sqlite3.connect("cz_ip/ip.db")
c = con.cursor()
c.executescript("""
drop table if exists cz;
create table cz (
i int primary key,
s text
);
""")
for ip in ips:
if not ip.strip():
break
i, _, s = extract(ip)
c.execute("insert into cz (i, s) values (?, ?)", (i, s))
con.commit()
if __name__ == '__main__':
main()