首页 > 代码库 > python从淘宝ip库中获取公网ip的信息

python从淘宝ip库中获取公网ip的信息

看到一个题目:  从ip.taobao.com中获取公网ip的信息

分析:

1、生成公网IP地址;

2、根据ip.taobao.com返回的代码,用json解析 下面是淘宝的接口说明

‘‘‘

接口说明

1. 请求接口(GET):

http://ip.taobao.com/service/getIpInfo.php?ip=[ip地址字串]

2. 响应信息:

(json格式的)国家 、省(自治区或直辖市)、市(县)、运营商

3. 返回数据格式:

{"code":0,"data":{"ip":"210.75.225.254","country":"\u4e2d\u56fd","area":"\u534e\u5317",
"region":"\u5317\u4eac\u5e02","city":"\u5317\u4eac\u5e02","county":"","isp":"\u7535\u4fe1",
"country_id":"86","area_id":"100000","region_id":"110000","city_id":"110000",
"county_id":"-1","isp_id":"100017"}}
其中code的值的含义为,0:成功,1:失败。

‘‘‘

======================

下面是我的代码:

#-*- coding:utf-8 -*-
#! /usr/bin/env python
import urllib,json,time

######################
#  create ip address
######################

def create_ip(beginip ,endip ,iptype):
    file = open(iptype+‘_ipinfo.txt‘, ‘w‘)
    begin = beginip.split(‘.‘)
    X1 = int(begin[0])
    X2 = int(begin[1])
    X3 = int(begin[2])
    X4 = int(begin[3])

    end = endip.split(‘.‘)
    Y1 = int(end[0])
    Y2 = int(end[1])
    Y3 = int(end[2])
    Y4 = int(end[3])

    for X1 in range(X1, Y1):
        for X2 in range(X2, Y2):
            for X3 in range(X3, Y3):
                for X4 in range(X4, Y4):
                    ip = "%d.%d.%d.%d" % (X1,X2,X3,X4)        #计算全球的公网IP地址

################################################################################
# get the info of the ip  from ip.taobao.com
# whith url http://ip.taobao.com/service/getIpInfo.php?ip=X.X.X.X
################################################################################

                    url = ‘http://ip.taobao.com/service/getIpInfo.php?ip=‘+ip
                    page = urllib.urlopen(url)
                    time.sleep(0.2)             #等待返回数据
                    alldata = page.read()
                    data = json.loads(alldata)
                    result = data[‘code‘]
                    info = data[‘data‘]
 
                    info_ip = info[‘ip‘]+‘,‘+info[‘area‘]+‘,‘+info[‘area_id‘]+‘,‘+info[‘city‘]
+‘,‘+info[‘city_id‘]+‘,‘+info[‘country‘]+‘,‘+info[‘country_id‘]+‘,‘+info[‘isp‘]+‘,‘+info[‘isp_
id‘]+‘,‘+info[‘region‘]+‘,‘+info[‘region_id‘]

                    info = info_ip.encode("utf-8")    #将字符编码类型转化为utf-8
                    
                    time.sleep(0.2)            #由于taobao有查询限制,要小于10qps,这
里设置为5qps

                    if ‘beginip‘ == ‘endip‘:
                        file.write(info)
                        file.close()
                    elif X4 == 255 :
                        continue
                    elif X4 == 0 :
                        continue
                    elif X1 > Y1:
                        print "please input right ip range"
                        file.close()
                        break
                    elif X1 <= Y1:
                        print info
                        if result == 0 :
                            file.write(info + ‘\n‘)
                        else :        
                            continue
                    else:
                        file.close()
                        return
                X4 = 0
            X3 = 0
        X2 = 0
    X1 =0

#create the A B C net type ip
create_ip(‘1.0.0.1‘,‘9.255.255.254‘,‘A1‘)
create_ip(‘11.0.0.1‘,‘126.255.255.254‘,‘A2‘)
create_ip(‘128.0.0.1‘,‘169.253.255.254‘,‘B1‘)
create_ip(‘169.255.0.1‘,‘172.15.255.254‘,‘B2‘)
create_ip(‘172.32.0.1‘,‘191.255.255.254‘,‘B3‘)
create_ip(‘192.0.0.1‘,‘192.167.255.254‘,‘C1‘)
create_ip(‘192.169.0.0‘,‘223.255.255.254‘,‘C2‘)


但是运行中会报错,

1.0.0.1,,,,,澳大利亚,AU,,,,
1.0.0.2,,,,,澳大利亚,AU,,,,
1.0.0.3,,,,,澳大利亚,AU,,,,
1.0.0.4,,,,,澳大利亚,AU,,,,
1.0.0.5,,,,,澳大利亚,AU,,,,
1.0.0.6,,,,,澳大利亚,AU,,,,
1.0.0.7,,,,,澳大利亚,AU,,,,
1.0.0.8,,,,,澳大利亚,AU,,,,
1.0.0.9,,,,,澳大利亚,AU,,,,
1.0.0.10,,,,,澳大利亚,AU,,,,
1.0.0.11,,,,,澳大利亚,AU,,,,
1.0.0.12,,,,,澳大利亚,AU,,,,
1.0.0.13,,,,,澳大利亚,AU,,,,
1.0.0.14,,,,,澳大利亚,AU,,,,
1.0.0.15,,,,,澳大利亚,AU,,,,
1.0.0.16,,,,,澳大利亚,AU,,,,
1.0.0.17,,,,,澳大利亚,AU,,,,
1.0.0.18,,,,,澳大利亚,AU,,,,
Traceback (most recent call last):
  File "createipandgetinfov5.py", line 77, in <module>
    create_ip(‘1.0.0.1‘,‘9.255.255.254‘,‘A1‘)
  File "createipandgetinfov5.py", line 40, in create_ip
    data = json.loads(alldata)
  File "/usr/lib/python2.7/json/__init__.py", line 338, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python2.7/json/decoder.py", line 366, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python2.7/json/decoder.py", line 384, in raw_decode
    raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded

想了很多方法不知道是什么问题,希望大牛们帮忙分析分析!

本文出自 “Security” 博客,请务必保留此出处http://mrjackzhou.blog.51cto.com/1180783/1561121

python从淘宝ip库中获取公网ip的信息