随笔 - 911  文章 - 5  评论 - 94  阅读 - 243万

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   momingliu11  阅读(1043)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5

点击右上角即可分享
微信分享提示