Skip to content

Instantly share code, notes, and snippets.

@hmkz
Last active December 29, 2015 12:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hmkz/7668522 to your computer and use it in GitHub Desktop.
Save hmkz/7668522 to your computer and use it in GitHub Desktop.
1行に1IPアドレスが記載されているテキストファイルからIPアドレス毎の出現回数を計算する。 IPアドレスからWhoisを通して国名とIPレンジを調べる
#!/usr/bin/env python3
# -*- encoding: utf8 -*-
from ipwhois import IPWhois
from IPy import IP
fobj = open("iplist.txt", 'r')
counter = {}
for line in fobj:
line = line.rstrip()
counter[line] = counter.get(line, 0) + 1
s = [ (k,v) for k,v in sorted(counter.items(), key=lambda x:x[1])]
s.reverse()
for ip,count in s[:30]:
ipw = IPWhois(ip)
result = ipw.lookup(False)
iprange = IP(result['nets'][0]['cidr'])
iprange.WantPrefixLen = 2
print("%s -> %s, range: %s country: %s" % (ip, count, iprange, result['nets'][0]['country']))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment