根据淘宝ip数据库查询ip归属地信息

#!/usr/bin/env python
import urllib2
import json
import os

class Ipsearch:
    def __init__(self,ip):
        print "init"
        self.ip = ip
   

    def check_ip(self):
        print "checking"
        tmp = self.ip.split('.')
        print tmp
        for i in tmp:
            if int(i) > 255:
                print "number roor"
                os.exit(1)
        print "trying"
        if not tmp:
            raise "error input"
        print "login..."
        url = "http://ip.taobao.com/service/getIpInfo.php?ip=" + self.ip
        fp = urllib2.urlopen(url)
        f = fp.read()
        s = json.loads(f)
        if s['code'] == 0:
            print "country = ",s['data']['country'] 
            print "aera =",s['data'].get("aera",'not exsit')
            print "city =",s['data'].get("city","not exsit")
            print "isp =",s['data'].get('isp',"not exsit")
        else:
            print "not exsit"
         
         
if __name__ == "__main__":
    ip = raw_input("input your ip:\n") 
    i = Ipsearch(ip)
    try:
        i.check_ip()
    except:
        print "failed..."
        
           

 

posted on 2013-12-05 21:37  lgy111  阅读(427)  评论(0编辑  收藏  举报

导航