python - python 获取ip信息(国家、城市等)

Python代码:

import geoip2.database


def from_ip_get_info(sip):
    reader = geoip2.database.Reader(
        '/opt/work/apt_rules/scripts/GeoLite2-City.mmdb')
    try:
        response = reader.city(sip)
        try:
            city = response.city.names.get(u'zh-CN', response.city.name)
            country = response.registered_country.names.get(u'zh-CN')
            if not city:
                city = response.country.names.get(u'zh-CN')
        except Exception as e:
            logging.debug("read Geolite2-City.mmdb error:%s" % e)
            city = ''
        longitude = response.location.longitude
        latitude = response.location.latitude
        subdivisions = response.subdivisions.most_specific.names.get(
            u'zh-CN', "")
    except Exception as ex:
        logging.debug("from_ip_get_info:%s" % ex)
        return None
    return (
        sip,
        country,
        city,
        subdivisions,
        longitude,
        latitude)

if __name__ == "__main__":
    a = from_ip_get_info('14.215.177.39')
    print a

详解:

参考:https://www.imooc.com/article/38672
安装pip包:pip install geoip2
下载db文件:http://geolite.maxmind.com/download/geoip/database/GeoLite2-Country.tar.gz

使用

import geoip2.database

# 14.215.177.39 ==> 百度的ip地址

# 获取国家信息(GeoLite2-Country.mmdb文件需要提前下载并放置在某一个路径下)
reader = geoip2.database.Reader('./GeoLite2-Country.mmdb')
c= reader.country('14.215.177.39')
print c.country.names   # 国家名

# 如下:
>>> import geoip2.database
>>> reader = geoip2.database.Reader('/mnt/GeoLite2-Country.mmdb')
>>> c = reader.country('14.215.177.39')
>>> print c.country.names
{u'ru': u'\u041a\u0438\u0442\u0430\u0439', u'fr': u'Chine', u'en': u'China', u'de': u'China', u'zh-CN': u'\u4e2d\u56fd', u'pt-BR': u'China', u'ja': u'\u4e2d\u56fd', u'es': u'China'}
>>> print c.country.names.get('zh-CN')
中国
>>> 



# 获取城市信息(GeoLite2-City.mmdb文件需要提前下载并放置在某一个路径下)
reader = geoip2.database.Reader('./GeoLite2-City.mmdb')
c= reader.city('14.215.177.39')
print c.city.names   # 城市名

#如下
>>> import geoip2.database
>>> reader = geoip2.database.Reader('/mnt/GeoLite2-City.mmdb')
>>> c = reader.city('14.215.177.39')
>>> print c.city.names
{u'ru': u'\u0413\u0443\u0430\u043d\u0447\u0436\u043e\u0443', u'fr': u'Canton', u'en': u'Guangzhou', u'de': u'Guangzhou', u'zh-CN': u'\u5e7f\u5dde', u'pt-BR': u'Cant\xe3o', u'ja': u'\u5e83\u5dde', u'es': u'Cant\xf3n'}
>>> print c.city.names.get('zh-CN')
广州
>>> 


# 其他信息看源码

自动更新
在github下载geoipupdate最新包:geoipupdate-3.1.1.tar.gz
解压并安装:

tar -zxvf geoipupdate-3.1.1.tar.gz
./configure
sudo make
sudo make install 

修改配置文件:/usr/local/etc/GeoIP.conf

# The following AccountID and LicenseKey are required placeholders.
# For geoipupdate versions earlier than 2.5.0, use UserId here instead of AccountID.
AccountID 0 
LicenseKey 000000000000 
 
# Include one or more of the following edition IDs: 
# * GeoLite2-City - GeoLite 2 City 
# * GeoLite2-Country - GeoLite2 Country 
# For geoipupdate versions earlier than 2.5.0, use ProductIds here instead of EditionIDs.
EditionIDs GeoLite2-City GeoLite2-Country

执行更新:

/usr/local/bin/geoipupdate

添加crontab实现每周更新:

crontab -e
0 0 * * 0 /usr/local/bin/geoipupdate

 

    原文作者:xuezhangjun
    原文地址: https://blog.csdn.net/xuezhangjun0121/article/details/86367801
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞