分析攻击IP来源地与防御IP攻击的应对策略(2)

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")

饼图效果:

在对网络攻击进行上述分析与识别的基础上,我们应当认真制定有针对性的策略。明确安全对象,设置强有力的安全保障体系。有的放矢,在网络中层层设防,发挥网络的每层作用,使每一层都成为一道关卡,从而让攻击者无隙可钻、无计可使。还必须做到未雨稠缪,预防为主 ,将重要的数据备份并时刻注意系统运行状况。以下是针对众多令人担心的网络安全问题,提出的几点建议: