查找IP來源
阿新 • • 發佈:2018-03-16
python
通過調用淘寶IP庫獲取IP歸屬地,腳本如下:
12345678910111213141516171819 | #!/usr/bin/env python # -*- coding: utf-8 -*- import urllib import json import sys #淘寶ip庫接口 url = "http://ip.taobao.com/service/getIpInfo.php?ip=" def ip_find(ip): data = urllib.urlopen(url + ip).read() datadict = json.loads(data) for oneinfo in datadict: if "code" = = oneinfo: if datadict[oneinfo] = = 0 : return datadict[ "data" ][ "country" ] + datadict[ "data" ][ "region" ] + datadict[ "data" ][ "city" ] + "\t" + datadict[ "data" ][ "isp" ] if __name__ = = "__main__" : ip = sys.argv[ 1 ] name = ip_find(ip) print sys.argv[ 1 ],name |
使用方法:
12 | $ python ip.py 202.106 . 0.20 202.106 . 0.20 中國北京市北京市 聯通 |
附:淘寶IP庫接口說明
分類: Python
查找IP來源