gpt4 book ai didi

python - Django : Get user IP from development server

转载 作者:行者123 更新时间:2023-12-04 02:01:43 39 4
gpt4 key购买 nike

我试图将用户 IP 放在我发送给用户的页面中。我试过像解释的那样 here .

所以我的 View 看起来像:

def yourip(request):
return HttpResponse("Your IP is : %s" % request.META.get('HTTP_X_FORWARDED_FOR'))

但是 request.META.get('HTTP_X_FORWARDED_FOR') 返回 None。是否可以从开发服务器执行此操作?还是其他地方的问题?

谢谢。

最佳答案

试试这个:

def get_client_ip(request):
x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
if x_forwarded_for:
print "returning FORWARDED_FOR"
ip = x_forwarded_for.split(',')[-1].strip()
elif request.META.get('HTTP_X_REAL_IP'):
print "returning REAL_IP"
ip = request.META.get('HTTP_X_REAL_IP')
else:
print "returning REMOTE_ADDR"
ip = request.META.get('REMOTE_ADDR')
return ip

关于python - Django : Get user IP from development server,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30558929/

39 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com