此文中的API将台湾列为国家,非本人立场,台湾属于中国,台湾岛生活的人不一定!
上码:
#!/usr/bin/python
#coding=utf-8
'''
http://ip-api.com/json/ip
'''
import plotly
import plotly.plotly
import plotly.graph_objs as abcc
import plotly.plotly
class Piecharts:
def __init__(self):
print "饼图生成中"
def makePiecharts(self,labels,values,filename):
trace = abcc.Pie(labels = labels,values= values)
plotly.offline.plot([trace],filename=filename)
import requests
import sys
try:
iplist = sys.argv[1]
except:
print "IP list not given or some other error!"
countrylist = {}
regionlist = {}
citylist = {}
with open(iplist) as f:
for ip in f.readlines():
if ip.strip() != '':
url = 'http://ip-api.com/json/' + ip.strip()
try:
result = requests.get(url)
jsontext = result.json()
except:
print "Error: Data not retrieved!"
continue
status = jsontext['status']
if status == 'fail':
print "%s failed!" % ip.strip()
continue
mline = jsontext['as']
city = jsontext['city']
country = jsontext['country']
countryCode = jsontext['countryCode']
isp = jsontext['isp']
lat = jsontext['lat']
lon = jsontext['lon']
org = jsontext['org']
query = jsontext['query']
region = jsontext['region']
regionName = jsontext['regionName']
timezone = jsontext['timezone']
zipcode = jsontext['zip']
if not country in countrylist:
countrylist[country] = 0
else:
countrylist[country] += 1
if not regionName in regionlist:
regionlist[regionName] = 0
else:
regionlist[regionName] += 1
if not city in citylist:
citylist[city] = 0
else:
citylist[city] += 1
try:
print ip.strip() + '--' + country + '--' + regionName
except:
print "Special character!"
print countrylist
#country
labels = [i for i in countrylist]
value = [countrylist[i] for i in countrylist]
drive = Piecharts()
drive.makePiecharts(labels,value,"country.html")
#region
labels = [i for i in regionlist]
value = [regionlist[i] for i in regionlist]
drive = Piecharts()
drive.makePiecharts(labels,value,"region.html")
#city
labels = [i for i in citylist]
value = [citylist[i] for i in citylist]
drive = Piecharts()
drive.makePiecharts(labels,value,"city.html")
gevent协程并发版
#!/usr/bin/python
# coding=utf-8
'''
http://ip-api.com/json/ip
'''
import plotly
import plotly.graph_objs as abcc
import plotly.plotly
class Piecharts:
def __init__(self):
print u'饼图生成中'
def makePiecharts(self, labels, values, filename):
trace = abcc.Pie(labels=labels, values=values)
plotly.offline.plot([trace], filename=filename)
import requests
import sys
try:
iplist = sys.argv[1]
except:
print "IP list not given or some other error!"
countrylist = {}
regionlist = {}
citylist = {}
def locater(url):
try:
result = requests.get(url)
jsontext = result.json()
except:
print "Error: Data not retrieved!"
return
status = jsontext['status']
if status == 'fail':
print "%s failed!" % ip.strip()
return
mline = jsontext['as']
city = jsontext['city']
country = jsontext['country']
countryCode = jsontext['countryCode']
isp = jsontext['isp']
lat = jsontext['lat']
lon = jsontext['lon']
org = jsontext['org']
query = jsontext['query']
region = jsontext['region']
regionName = jsontext['regionName']
timezone = jsontext['timezone']
zipcode = jsontext['zip']
if not country in countrylist:
countrylist[country] = 0
else:
countrylist[country] += 1
if not regionName in regionlist:
regionlist[regionName] = 0
else:
regionlist[regionName] += 1
if not city in citylist:
citylist[city] = 0
else:
citylist[city] += 1
try:
print ip.strip() + '--' + country + '--' + regionName
except:
print "Special character!"
from gevent import monkey
monkey.patch_socket()
from gevent import pool
import gevent
pool = pool.Pool(40)
glist = []
with open(iplist) as f:
for ip in f.readlines():
if ip.strip() != '':
url = 'http://ip-api.com/json/' + ip.strip()
glist.append(pool.spawn(locater, url))
gevent.joinall(glist)
# country
labels = [i for i in countrylist]
value = [countrylist[i] for i in countrylist]
drive = Piecharts()
drive.makePiecharts(labels, value, "country.html")
# region
labels = [i for i in regionlist]
value = [regionlist[i] for i in regionlist]
drive = Piecharts()
drive.makePiecharts(labels, value, "region.html")
# city
labels = [i for i in citylist]
value = [citylist[i] for i in citylist]
drive = Piecharts()
drive.makePiecharts(labels, value, "city.html")
饼图效果: