https://github.com/Coxhuang/django-ip.git
python3.6
Django==2.0.7
from django.shortcuts import HttpResponse
import requests
import re
import time
def get_localhost_ip(request):
response = requests.get("http://" + str(time.localtime().tm_year) + ".ip138.com/ic.asp")
ip = re.search(r"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}", response.content.decode(errors='ignore')).group(0)
print(ip)
return HttpResponse("localhost_ip : %s"%ip)
localhost_ip : 61.145.167.223
ALLOWED_HOSTS = ["*"]
def get_request_ip(request):
try:
x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
if x_forwarded_for:
ip = x_forwarded_for.split(',')[0] # 所以这里是真实的ip
else:
ip = request.META.get('REMOTE_ADDR') # 这里获得代理ip
except:
ip = None
return HttpResponse("request_ip : %s" % ip)
request_ip : 127.0.0.1
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有