分析攻击IP来源地并画出饼图
此文中的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")
饼图效果:
分析攻击IP来源地并画出饼图的更多相关文章
- 从BUG工具redmine上获取数据后借助python模块pycha 画出BUG分析类报表
整体代码比较冗长,但是很好读.写的方法全是按照BUG分类去写的.所以写死了,凑合看吧,画出饼图,树状图和生成对应的数据excel,希望大家举一反三能帮助自己分析BUG #__author__ = 'x ...
- 利用ggplot2画出各种漂亮图片详细教程
1.Why use ggplot2 ggplot2是我见过最human friendly的画图软件,这得益于Leland Wilkinson在他的著作<The Grammar of Graphi ...
- H5坦克大战之【画出坦克】
今天是个特殊的日子,圣诞节,也是周末,在这里先祝大家圣诞快乐!喜庆的日子,我们可以稍微放松一下,扯一扯昨天雷霆对战凯尔特人的比赛,这场比赛大威少又双叒叕拿下三双,而且是一个45+11+11的超级三双, ...
- 像画笔一样慢慢画出Path的三种方法(补充第四种)
今天大家在群里大家非常热闹的讨论像画笔一样慢慢画出Path的这种效果该如何实现. 北京-LGL 博客号@ligl007发起了这个话题.然后各路高手踊跃发表意见.最后雷叔 上海-雷蒙 博客号@雷蒙之星 ...
- 用css画出三角形
看到有面试题里会有问到如何用css画出三角形 众所周知好多图形都可以拆分成三角形,所以说会了画三角形就可以画出很多有意思的形状 画出三角形的原理是调整border(边框)的四个方向的宽度,线条样式以及 ...
- MATLAB 画出三个通信小区cell边界示意图
d=1000; %两个小区中心间距离的一半 rcell=2*d/sqrt(3); %小区半径 ncell=3; %小区个数 cellposition=zeros(ncell,2); %初始化小区中心位 ...
- 如何用css画出三角形
看到有面试题里会有问到如何用css画出三角形 众所周知好多图形都可以拆分成三角形,所以说会了画三角形就可以画出很多有意思的形状 画出三角形的原理是调整border(边框)的四个方向的宽度,线条样式以及 ...
- 用css画出三角形【转】
看到有面试题里会有问到如何用css画出三角形 众所周知好多图形都可以拆分成三角形,所以说会了画三角形就可以画出很多有意思的形状 画出三角形的原理是调整border(边框)的四个方向的宽度,线条样式以及 ...
- 由两点坐标如何画出直线 matlab
由两点坐标如何画出直线 方法1:利用直线方程 斜率加截距 方法2:数据拟合 %由两点坐标得数据拟合直线与画线 x = [,]; y = [,]; k = ((-)/(-));% 由两点坐标得到直线斜 ...
随机推荐
- js将url转换二维码
二维码生成库 qrcode.js /*from tccdn minify at 2014-6-4 14:59:43,file:/cn/c/c/qrcode.js*/ /** * @fileovervi ...
- Oracle数据库,数字强制显示2位小数
在银行.财务等对数字要求敏感的系统中,数字的显示一般有着严格的要求.今遇到一个需求,如题,要求将数字以两位小数的格式显示,如果没有小数,则强制显示为0.例如:123.4 显示为 123.4012 ...
- 杂记-格式化Date默认格式,日期加一天,jstl判断字符类型,ajax模拟from表单后台跳转页面,jstl访问数据库并在页面显示
1.格式化Date默认格式 String str="Sun Oct 08 22:36:45 CST 2017"; SimpleDateFormat sdf = new Simple ...
- 数据结构与算法之PHP查找算法(二分查找)
二分查找又称折半查找,只对有序的数组有效. 优点是比较次数少,查找速度快,平均性能好,占用系统内存较少: 缺点是要求待查表为有序表,且插入删除困难. 因此,折半查找方法适用于不经常变动而查找频繁的有序 ...
- python使用SQLAlchemy模块连接MySQL
ORM技术:Object-Relational Mapping,负责把关系数据库的表结构映射到对象上. 1.安装SQLAlchemy,MySQLdb模块 MySQLdb安装教程:http://www. ...
- 【Linux】shell学习之sed
sed替换命令 使用该命令,可以将特定字符串或匹配的规则表达式用另一个字符串替换. sed 's/88/--/' filename 将filename每行第一次出现的88用字符串--替换,然后将该文件 ...
- Space Ant
Space Ant The most exciting space discovery occurred at the end of the 20th century. In 1999, scient ...
- Vmware tools install
Vmware tools 1◆ 下载 2◆ diagram Ifcfg-eth0 =====>关闭防火墙 systemctl stop firewalld.service ===== ...
- matlab global persistent变量
global变量是全局的,在使用global变量的函数里需要用global声明所使用的变量. persistent类似global,不过仅对当前函数有作用,这样避免了外面的影响.当这个函数被clear ...
- python 转换代码格式
import os dirname="C:\\Users\\haier\\Desktop\\new" def walk(path): for item in os.listdir( ...