用python进行IP地址查询
最近在学python,利用淘宝的RestApi:http://ip.taobao.com/instructions.php;写了个ip地址查询工具
# -*-coding: utf-8-*-
# ip addrss
# author: [email protected]
# date: 2013-2-20
# blog: tinybird.info
import requests
import json
import re
def real_address(ip):
'''
Return your real address in json
Using ip.taobao.com api
'''
url = 'http://ip.taobao.com/service/getIpInfo.php?ip=%s' % ip
response = requests.get(url)
return json.loads(response.content)
# 查询本机的外网ip
def my_ip():
'''Return your own ip address'''
data = requests.get('http://www.whereismyip.com')
return re.search('d+.d+.d+.d+',data.content).group(0)
def print_json(data):
keys = ['ip', 'country', 'region', 'city', 'isp']
for key in keys:
print data['data'][key],
if __name__ == '__main__':
print_json(real_address(my_ip()))