今天同事给了6W多个IP叫我查出ISP和归属地,果断用python urllib2搞之,数据库用是淘宝的API接口
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
|
#!/usr/bin/python
#coding:utf-8
import
urllib2
import
json
import
time
url
=
'http://ip.taobao.com/service/getIpInfo.php?ip='
def
checkTaobaoIP(ip):
try
:
response
=
urllib2.urlopen(url
+
ip, timeout
=
5
)
result
=
response.readlines()
data
=
json.loads(result[
0
])
return
"%15s: %s-%s-%s"
%
(ip,data[
'data'
][
'isp'
],data[
'data'
][
'region'
],data[
'data'
][
'city'
])
except
:
return
"%15s: timeout"
%
ip
if
__name__
=
=
"__main__"
:
f
=
open
(
'ip.txt'
)
ips
=
f.readlines()
f.close()
f
=
open
(
'ip-check.txt'
,
'w'
)
for
ip
in
ips:
line
=
checkTaobaoIP(ip.strip())
if
line:
print
line.encode(
'utf-8'
)
f.write(line.encode(
'utf-8'
)
+
'\n'
)
else
:
print
line
f.write(line
+
'\n'
)
f.close()
print
"Done!"
本文转自运维笔记博客51CTO博客,原文链接http://blog.51cto.com/lihuipeng/1533936如需转载请自行联系原作者 lihuipeng
|