zoukankan      html  css  js  c++  java
  • 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)
  • 相关阅读:
    iOS布局进化史
    thinkphp5.0 文章详情页 上一篇 下一篇
    Thinkphp 3.2中文章详情页的上一篇 下一篇文章功能
    WebSocket协议:5分钟从入门到精通
    Nginx实战之反向代理WebSocket的配置实例
    nginx服务器配置/websocket nginx 配置笔记
    使用nginx作为websocket的proxy server
    看完让你彻底搞懂Websocket原理
    本地phpstudy时常停机连接失败,php.ini文件中9000端口问题
    python 发邮件
  • 原文地址:https://www.cnblogs.com/dreamer-fish/p/6032772.html
Copyright © 2011-2022 走看看