此文中的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来源地并画出饼图的更多相关文章

  1. 从BUG工具redmine上获取数据后借助python模块pycha 画出BUG分析类报表

    整体代码比较冗长,但是很好读.写的方法全是按照BUG分类去写的.所以写死了,凑合看吧,画出饼图,树状图和生成对应的数据excel,希望大家举一反三能帮助自己分析BUG #__author__ = 'x ...

  2. 利用ggplot2画出各种漂亮图片详细教程

    1.Why use ggplot2 ggplot2是我见过最human friendly的画图软件,这得益于Leland Wilkinson在他的著作<The Grammar of Graphi ...

  3. H5坦克大战之【画出坦克】

    今天是个特殊的日子,圣诞节,也是周末,在这里先祝大家圣诞快乐!喜庆的日子,我们可以稍微放松一下,扯一扯昨天雷霆对战凯尔特人的比赛,这场比赛大威少又双叒叕拿下三双,而且是一个45+11+11的超级三双, ...

  4. 像画笔一样慢慢画出Path的三种方法(补充第四种)

    今天大家在群里大家非常热闹的讨论像画笔一样慢慢画出Path的这种效果该如何实现. 北京-LGL 博客号@ligl007发起了这个话题.然后各路高手踊跃发表意见.最后雷叔 上海-雷蒙 博客号@雷蒙之星 ...

  5. 用css画出三角形

    看到有面试题里会有问到如何用css画出三角形 众所周知好多图形都可以拆分成三角形,所以说会了画三角形就可以画出很多有意思的形状 画出三角形的原理是调整border(边框)的四个方向的宽度,线条样式以及 ...

  6. MATLAB 画出三个通信小区cell边界示意图

    d=1000; %两个小区中心间距离的一半 rcell=2*d/sqrt(3); %小区半径 ncell=3; %小区个数 cellposition=zeros(ncell,2); %初始化小区中心位 ...

  7. 如何用css画出三角形

    看到有面试题里会有问到如何用css画出三角形 众所周知好多图形都可以拆分成三角形,所以说会了画三角形就可以画出很多有意思的形状 画出三角形的原理是调整border(边框)的四个方向的宽度,线条样式以及 ...

  8. 用css画出三角形【转】

    看到有面试题里会有问到如何用css画出三角形 众所周知好多图形都可以拆分成三角形,所以说会了画三角形就可以画出很多有意思的形状 画出三角形的原理是调整border(边框)的四个方向的宽度,线条样式以及 ...

  9. 由两点坐标如何画出直线 matlab

    由两点坐标如何画出直线  方法1:利用直线方程 斜率加截距 方法2:数据拟合 %由两点坐标得数据拟合直线与画线 x = [,]; y = [,]; k = ((-)/(-));% 由两点坐标得到直线斜 ...

随机推荐

  1. HTML(简介及常用标签)

    一.HTML简介 1.1 html是什么? 超文本标记语言(Hypertext Markup Language,HTML)通过标签语言来标记要显示的网页中的各个部分.一套规则,浏览器认识的规则. 浏览 ...

  2. python装饰器概念与应用

    格式一:装饰器外层不传参,内层传参 user_status = False # 用户登录了就把这个改成True def login(func): # 把要执行的henan模块从这里传进来 def in ...

  3. leetcode-algorithms-19 Remove Nth Node From End of List

    leetcode-algorithms-19 Remove Nth Node From End of List Given a linked list, remove the n-th node fr ...

  4. Thymeleaf使用bootstrap及其bootstrap相关插件(二)

    接上文http://www.cnblogs.com/conswin/p/7929772.html 接下来bootstrap-datepicker的简单使用. 1.引入添加js 和 css 2.然后是h ...

  5. python 线程 进程

    1.进程与线程优.缺点的比较总言:使用进程和线程的目的,提高执行效率. 进程: 优点:能利用机器的多核性能,同时进行多个操作. 缺点:需要耗费资源,重新开辟内存空间,耗内存. 线程: 优点:共享内存( ...

  6. 基于Vue + webpack + Vue-cli 实现分环境打包项目

    需求由来:我公司项目上线发布至服务器分为三个环境分别为测试环境.预发布环境.生产环境:前期做法是项目通过脚步打包时由脚步把域名和后缀名之类的全部替换成要发布的环境所需要的,因为我公司的项目比较大由许许 ...

  7. mybatis.xml和mapper.xml的配置

    mybatis.xml和mapper.xml的配置 1.创建一个Source Folder 2.完成分包mapper和mybatis 3.创建mybatis.xml文档 4xml文档名 5.名字规范 ...

  8. Web应用的统一异常处理(二十四)

    我们在做Web应用的时候,请求处理过程中发生错误是非常常见的情况.Spring Boot提供了一个默认的映射:/error,当处理中抛出异常之后,会转到该请求中处理,并且该请求有一个全局的错误页面用来 ...

  9. WinForm之窗体应用程序

    WinForm之窗体应用程序 基本简单数据库操作(增删改查) using System; using System.Collections.Generic; using System.Windows. ...

  10. laravel中文件上传:

    laravel5.5版本: congfig下的filesystems.php中配置:uploads信息: 'uploads' => [ 'driver' => 'local', 'root ...