Last active
December 29, 2015 12:09
-
-
Save hmkz/7668522 to your computer and use it in GitHub Desktop.
1行に1IPアドレスが記載されているテキストファイルからIPアドレス毎の出現回数を計算する。
IPアドレスからWhoisを通して国名とIPレンジを調べる
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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