生产实践:
Python实现利用淘宝IP库批量查询IP信息
学习技巧:
python多线程
脚本内容:
#!/usr/bin/env python
# coding=utf8
############################################################
# $Name: mtbpy.py
# $Version: v1.0
# $Function: use Taobao IP address library(http://ip.taobao.com) to query the IP info
# $Author: Zhihe An
# $organization: chegva.com
# $Create Date: 2017-09-23
############################################################
import urllib2
import json
import time
import sys
import threading
def getip(ip):
ip_tb_api_url = "http://ip.taobao.com/service/getIpInfo.php?ip="
f = urllib2.urlopen(ip_tb_api_url + ip)
result = json.loads(f.read())
area = result["data"]["area"]
city = result["data"]["city"]
country = result["data"]["country"]
isp = result["data"]["isp"]
region = result["data"]["region"]
time.sleep(1)
print "%-23s %-8s %-8s %-8s %-8s %-8s" %(ip,country, isp, area, region, city)
if __name__ == '__main__':
print "IP地址\t\t\t国家\t 运营商 区域 省份 城市"
print "===="*20
try:
with open('list','r') as f:
for ipaddr in f.readlines():
ipaddr=ipaddr.strip('\n')
t = threading.Thread(target = getip,args = [ipaddr])
t.start()
#t.join()
except Exception as e:
raise
Python
◎查看效果
参考:
https://github.com/hellosa/taobaoip-alfredworkflow/blob/master/README.md
http://www.ttlsa.com/python/python-multi-threaded-tutorial