分析攻击IP来源地并画出饼图

简介: 版权声明:本文可能为博主原创文章,若标明出处可随便转载。 https://blog.
版权声明:本文可能为博主原创文章,若标明出处可随便转载。 https://blog.csdn.net/Jailman/article/details/80508954

此文中的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")

饼图效果:
这里写图片描述

目录
相关文章
|
5月前
GEE图表——GEE绘制图表,设定双纵坐标图形以NDVI和RVI为例
GEE图表——GEE绘制图表,设定双纵坐标图形以NDVI和RVI为例
67 0
|
Python
matplotlib绘制箱形图之基本配置——万能模板案例(一)
matplotlib绘制箱形图之基本配置——万能模板案例
1133 0
matplotlib绘制箱形图之基本配置——万能模板案例(一)
|
5月前
|
自然语言处理 数据可视化
【词云图绘制实战】——数据准备、清洗、多形式展示
【词云图绘制实战】——数据准备、清洗、多形式展示
|
5月前
|
数据可视化
绘制GGPLOT2双色XY区间面积图组合交叉折线图数据可视化
绘制GGPLOT2双色XY区间面积图组合交叉折线图数据可视化
|
5月前
|
存储 数据可视化 关系型数据库
绘制圆环图/雷达图/星形图/极坐标图/径向图POLAR CHART可视化分析汽车性能数据
绘制圆环图/雷达图/星形图/极坐标图/径向图POLAR CHART可视化分析汽车性能数据
|
5月前
|
Serverless
统计问题|绘制任意分布的 QQ 图
统计问题|绘制任意分布的 QQ 图
118 1
|
12月前
|
数据挖掘
这图怎么画| 批量小提琴图+箱线图+散点+差异分析
这图怎么画| 批量小提琴图+箱线图+散点+差异分析
272 0
跟着Nat Commun学作图 | 4.配对箱线图+差异分析
跟着Nat Commun学作图 | 4.配对箱线图+差异分析
514 0
跟着Nat Commun学作图 | 4.配对箱线图+差异分析
R语言绘制组间比较散点图并自动添加P值信息
查询ggprism包使用时候发现官网给出的一示例图比较常用,这里记录学习一下。
207 3