LoginSignup
2
3

More than 5 years have passed since last update.

Python 2.7でGeoIP を使う

Last updated at Posted at 2017-12-11

GeoIP

IPアドレスから国,市区町村,緯度経度などを調べることができる.

手順

GeoIPのデータベースをダウンロード

  • 好きなディレクトリで
wget http://geolite.maxmind.com/download/geoip/database/GeoLite2-Country.mmdb.gz
gunzip GeoLite2-Country.mmdb.gz
wget http://geolite.maxmind.com/download/geoip/database/GeoLite2-City.mmdb.gz
gunzip GeoLite2-City.mmdb.gz

コード

  • GeoIPのデータベースをダウンロードした場所で
    • 自分はとりあえず同じ階層に置いた
# -*- coding: utf-8 -*-
import geoip2.database

ip = raw_input()

reader = geoip2.database.Reader('./GeoLite2-City.mmdb')
data = reader.city(ip)

print ("IP Address: ", ip)
print ("Country:", data.country.name)
print ("Subdivisions: ", data.subdivisions.most_specific.name)
print ("City: ", data.city.name)
print ("Latitude: ", data.location.latitude)
print ("Longitude: ", data.location.longitude)

使い道

  • 正直,使ってはみたものの普通にwhoisコマンドで検索したほうが早いです.
  • 自分はハニーポットの運営をしていたので,IPアドレスを列挙したログに対してGeoIPを使ってました.
2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3