geoip2 获取ip信息

安装第三方库

        pip  install geoip2

GeoLite2-City.mmdb数据库下载

https://cdn.jsdelivr.net/npm/geolite2-city@1.0.0/GeoLite2-City.mmdb.gz

下载成功后,将GeoLite2-City.mmdb文件放在项目根目录下,与manage.py同级

快速开始

1.settings.py 文件的INSTALLED_APPS中注册geoip2

django项目中使用

INSTALLED_APPS=[

'django.contrib.admin',

'django.contrib.auth',

'django.contrib.contenttypes',

'django.contrib.sessions',

'django.contrib.messages',

'django.contrib.staticfiles',

…...

'geoip2',

]

2.配置GeoLite2-City.mmdb数据库路径

        GEOIP_PATH=os.path.join(BASE_DIR,'GeoLite2-City.mmdb')

3.在应用下的ulrs.py 中添加路由

path('ip/',views.IpView.as_view())

4.在应用中的views.py文件中编写获取ip信息的接口

from django.contrib.gis.geoip2 import GeoIP2
import geoip2
from urllib.parse import urlparse
from rest_framework.views import APIView

class IpView(APIView):
    authentication_classes = []

    def details(self, ip):
        g = GeoIP2()
        details = g.city(ip)
        return details

    def get(self, request):
        try:
            ip = request.data.get('ip')
            is_ip = ip.replace('.', '')
            if not ip:
                return Response(data={'message': 'please input ip first'})

            elif is_ip.isdigit():
                if not validate_ip(ip):
                    details = 'address is not valid'
                    return Response(
                        {
                            'data': {
                                'details': details
                            },
                            'success': True,
                            'message': 'Success'
                        }
                    )
                else:
                    details = self.details(ip)
                    return Response(
                        {
                            'data': {
                                'details': details
                            },
                            'success': True,
                            'message': 'Success'
                        }
                    )
            elif 'http' in ip or 'https' in ip:
                ip = urlparse(ip).netloc
                details = self.details(ip)
                return Response(
                    {
                        'data': {
                            'details': details
                        },
                        'success': True,
                        'message': 'Success'
                    }
                )
            else:
                details = self.details(ip)
                return Response(
                    {
                        'data': {
                            'details': details
                        },
                        'success': True,
                        'message': 'Success'
                    }
                )
        except geoip2.errors.AddressNotFoundError:
            details = f'The {ip} address could not be found.'
            return Response(
                {
                    'data': {
                        'details': details
                    },
                    'success': True,
                    'message': 'Success'
                }
            )

5.通过postman代理发送请求

  • 13
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

请叫我小周啊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值