分析攻击IP来源地并画出饼图
生活随笔
收集整理的這篇文章主要介紹了
分析攻击IP来源地并画出饼图
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
版權聲明:本文可能為博主原創文章,若標明出處可隨便轉載。 https://blog.csdn.net/Jailman/article/details/80508954
此文中的API將臺灣列為國家,非本人立場,臺灣屬于中國,臺灣島生活的人不一定!
上碼:
gevent協程并發版
#!/usr/bin/python # coding=utf-8''' http://ip-api.com/json/ip '''import plotly import plotly.graph_objs as abcc import plotly.plotlyclass 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 systry: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!"returnstatus = jsontext['status']if status == 'fail':print "%s failed!" % ip.strip()returnmline = 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] = 0else:countrylist[country] += 1if not regionName in regionlist:regionlist[regionName] = 0else:regionlist[regionName] += 1if not city in citylist:citylist[city] = 0else:citylist[city] += 1try:print ip.strip() + '--' + country + '--' + regionNameexcept:print "Special character!"from gevent import monkey monkey.patch_socket() from gevent import pool import geventpool = 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)# countrylabels = [i for i in countrylist]value = [countrylist[i] for i in countrylist]drive = Piecharts()drive.makePiecharts(labels, value, "country.html")# regionlabels = [i for i in regionlist]value = [regionlist[i] for i in regionlist]drive = Piecharts()drive.makePiecharts(labels, value, "region.html")# citylabels = [i for i in citylist]value = [citylist[i] for i in citylist]drive = Piecharts()drive.makePiecharts(labels, value, "city.html")餅圖效果:
總結
以上是生活随笔為你收集整理的分析攻击IP来源地并画出饼图的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CSS3 - 清除浮动
- 下一篇: UniDBGrid增加显示记录数的lab