python查询IP地址所属地

1.linux命令行版

#!/usr/bin/python
#-*- coding: utf-8 -*-

import json
import urllib
import sys


def get_data(ip):
    API = "http://ip.taobao.com/service/getIpInfo.php?ip="
    url = API + sys.argv[1]
    jsondata = json.loads(urllib.urlopen(url).read())

    if jsondata['code'] == 1:
        print("No %s info." % ip)
        exit(1)
    else:
        return jsondata

def process_data(jsondata):    
    if jsondata['data']['country']:
        country = jsondata['data']['country']
    else:
        country = "NULL"

    if jsondata['data']['area']:
        area = jsondata['data']['area']
    else:
        area = "NULL"

    if jsondata['data']['region']:
        region = jsondata['data']['region']
    else:
        region = "NULL"

    if jsondata['data']['city']:
        city = jsondata['data']['city']
    else:
        city = "NULL"
    if jsondata['data']['county']:
        county = jsondata['data']['county']
    else:
        county = "NULL"

    if jsondata['data']['isp']:
        isp= jsondata['data']['isp']
    else:
        isp = "NULL"
    return (country, area, region, city, county, isp)

def output_data(data):
    ret = "%-4s\t%-4s\t%-4s\t%-4s\t%-4s\t%-4s\n" % ("country", "area", "region", "city", "county", "isp")
    ret += "%-4s\t%-4s\t%-4s\t%-4s\t%-4s\t%-4s" % data
    print(ret)

ret = get_data(sys.argv[1])
result = process_data(ret)
output_data(result)

原文地址:http://blog.csdn.net/onwer3/article/details/9737919

posted @ 2017-05-17 17:50  道霖  阅读(4518)  评论(0编辑  收藏  举报