IP地址查询

 

# -*- coding: UTF-8 -*-

ip = '61.158.147.194'

import urllib,urllib2,cookielib,sys,cjson
reload(sys)
sys.setdefaultencoding('utf-8')

url = 'http://ip.taobao.com/service/getIpInfo.php?' #定义接口地址,限制10qps
headers = {
   'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko'
}
headers = {'User-agent':'Mozilla/5.0'} #---OK
url_args = urllib.urlencode({"ip":ip}) 
urls = '%s%s' %(url,url_args)
req = urllib2.Request(url=urls,headers=headers) #需要添加一个header,否则会提示403forbidden
res = urllib2.urlopen(req).read() #返回:aabb00


res_d = cjson.decode(res)['data']
# res_d = json.loads(res,strict=False,encoding='utf-8')
print res_d[u'country'],res_d[u'region'],res_d[u'city'],res_d[u'isp']
#返回值:中国 河南省 开封市 联通

 

设置URL超时:

def IP2Addr(ip):
    url = 'http://ip.taobao.com/service/getIpInfo.php?' #定义接口地址,限制10qps
    headers = {
       'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko'
    }
    headers = {'User-agent':'Mozilla/5.0'} #---OK
    url_args = urllib.urlencode({"ip":ip}) 
    urls = '%s%s' %(url,url_args)
    req = urllib2.Request(url=urls,headers=headers) #需要添加一个header,否则会提示403forbidden
    try:
        res = urllib2.urlopen(req,timeout = 0.5)  #增加超时时间
        res_d = cjson.decode(res.read())['data']
        #ipaddr = '%s %s %s %s' %(res_d[u'country'],res_d[u'region'],res_d[u'city'],res_d[u'isp'])
        ipaddr = '%s %s' %(res_d[u'country'],res_d[u'region'])
        return ipaddr
    except urllib2.URLError, e:
        return 'None'
 

 

调用新浪IP查询接口:

ip = '61.158.147.194'

import urllib,urllib2,cookielib,sys,cjson
reload(sys)
sys.setdefaultencoding('utf-8')

def IP2Addr(ip):
    #url = 'http://ip.taobao.com/service/getIpInfo.php?' #定义taobao接口地址,限制10qps
    url = 'http://int.dpool.sina.com.cn/iplookup/iplookup.php?' #定义Sina接口地址
    headers = {
       'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko'
    }
    headers = {'User-agent':'Mozilla/5.0'} #---OK
    url_args = urllib.urlencode({"format":"json","ip":ip}) 
    urls = '%s%s' %(url,url_args)
    req = urllib2.Request(url=urls,headers=headers) #需要添加一个header,否则会提示403forbidden
    
    try:
        res = urllib2.urlopen(req,timeout = 0.5) 
        res_d = cjson.decode(res.read())
        if res_d['ret'] == 1:

            ipaddr = '%s %s' %(res_d[u'country'],res_d[u'province'])
            # print ipaddr
        else:
            ipaddr = 'None'
        return ipaddr
    except urllib2.URLError, e:
        return 'None'
    
print IP2Addr(ip)

 

Python3:

import urllib
from urllib import request
from urllib import parse
from urllib.request import urlopen
def IP2Addr(ip):
    url = 'http://ip.taobao.com/service/getIpInfo.php?' #定义接口地址,限制10qps
    headers = {
       'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko'
    }
    headers = {'User-agent':'Mozilla/5.0'} #---OK
    url_args = parse.urlencode({"ip":ip}) 
    urls = '%s%s' %(url,url_args)
    req = request.Request(url=urls,headers=headers) #需要添加一个header,否则会提示403forbidden
    try:
        res = urlopen(req,timeout = 0.5)  #增加超时时间
        res_d = ujson.decode(res.read())['data']
        #ipaddr = '%s %s %s %s' %(res_d[u'country'],res_d[u'region'],res_d[u'city'],res_d[u'isp'])
        if res_d[u'country'] == '中国':
            ipaddr = '%s省%s' %(res_d[u'region'],res_d[u'city'])
        else:
            ipaddr = '%s%s%s' %(res_d[u'country'],res_d[u'region'],res_d[u'city'])


    except Exception as ee:
        ipaddr = None
    return ipaddr
m=IP2Addr('23.106.157.126')
print(m)

 

posted on 2016-11-05 11:53  momingliu11  阅读(1037)  评论(0编辑  收藏  举报