标签:IP地址查询
思路是,使用requests模块调用阿里的ip接口查询ip归属地关于requests模块的使用,可以查询相关文档,很强大,本次不做描述
#!/usr/bin/python #coding=utf-8 import requests def checkip(ip): URL = 'http://ip.taobao.com/service/getIpInfo.php' try: r = requests.get(URL, params=ip, timeout=3) except requests.RequestException as e: print(e) else: json_data = r.json() if json_data[u'code'] == 0: print '所在国家: ' + json_data[u'data'][u'country'].encode('utf-8') print '所在地区: ' + json_data[u'data'][u'area'].encode('utf-8') print '所在省份: ' + json_data[u'data'][u'region'].encode('utf-8') print '所在城市: ' + json_data[u'data'][u'city'].encode('utf-8') print '所属运营商:' + json_data[u'data'][u'isp'].encode('utf-8') else: print '查询失败,请稍后再试!' ip={'ip':(raw_input('please input ip address:'))} checkip(ip)
查询结果如下
标签:IP地址查询
原文地址:http://blog.51cto.com/11555417/2104159