GeoIP2数据库——根据ip确定国家/地区,过滤某区域ip

GeoIP2下载:https://dev.maxmind.com/geoip/geoip2/downloadable/

GeoIP2数据库使用例子:

from django.conf import settings as django_settings
import geoip2.database

def get(self, request, **kwargs):
    if request.META.get('HTTP_X_FORWARDED_FOR'):
        ip = request.META['HTTP_X_FORWARDED_FOR']
    else:
        ip = request.META['REMOTE_ADDR']
    print(ip)
    ip = '128.101.101.101'

    DIR = os.path.join(str(django_settings.APPS_DIR), 'static', 'mmdb', 'GeoLite2-Country.mmdb')
    reader = geoip2.database.Reader(DIR)

    response = reader.country(ip_address=ip)
    print(response.country.name, '---', response.continent.name)
    if response.continent.name == 'Europe':
        raise Http404
阅读更多
个人分类: Python
上一篇ubuntu中Terminal消失
下一篇pip安装django-iprestrict[geoip] 报错:error: command 'x86_64-linux-gnu-gcc' failed with exit status 1
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭