# 获取请求IP
def get_request_ip(request):
if request.META.get('HTTP_X_FORWARDED_FOR'):
ip = request.META.get("HTTP_X_FORWARDED_FOR")
else:
ip = request.META.get("REMOTE_ADDR")
return ip
def ip_to_location(ip):
"""
通过ip获取地址
:param ip: 异常
:return: string
"""
try:
url = config.USER.IP_2_ADDR.HOST + str(
ip) + config.USER.IP_2_ADDR.SUFFIX
location_resp_json = requests.get(url).json()
location = location_resp_json['data'][0]['location']
return str(location).split()[0]
except:
return ''