fix: 修复获取 city 时可能的报错 (#8294)

Co-authored-by: ibuler <ibuler@qq.com>
This commit is contained in:
fit2bot
2022-05-24 12:31:16 +08:00
committed by GitHub
parent a08dd5ee72
commit 041302d5d2
3 changed files with 21 additions and 21 deletions

View File

@@ -1,7 +1,6 @@
# -*- coding: utf-8 -*-
#
import os
from django.utils.translation import ugettext as _
import ipdb
@@ -11,13 +10,13 @@ ipip_db = None
def get_ip_city_by_ipip(ip):
global ipip_db
if not ip or not isinstance(ip, str):
return _("Invalid ip")
if ':' in ip:
return 'IPv6'
if ipip_db is None:
ipip_db_path = os.path.join(os.path.dirname(__file__), 'ipipfree.ipdb')
ipip_db = ipdb.City(ipip_db_path)
info = ipip_db.find_info(ip, 'CN')
try:
info = ipip_db.find_info(ip, 'CN')
except ValueError:
return None
if not info:
raise None
return {'city': info.city_name, 'country': info.country_name}