如何在Django中获取用户的IP ?
我的观点是这样的:
# Create your views
from django.contrib.gis.utils import GeoIP
from django.template import RequestContext
from django.shortcuts import render_to_response
def home(request):
g = GeoIP()
client_ip = request.META['REMOTE_ADDR']
lat,long = g.lat_lon(client_ip)
return render_to_response('home_page_tmp.html',locals())
但是我得到了这个错误:
KeyError at /mypage/
'REMOTE_ADDR'
Request Method: GET
Request URL: http://mywebsite.example/mypage/
Django Version: 1.2.4
Exception Type: KeyError
Exception Value:
'REMOTE_ADDR'
Exception Location: /mysite/homepage/views.py in home, line 9
Python Executable: /usr/bin/python
Python Version: 2.6.6
Python Path: ['/mysite', '/usr/local/lib/python2.6/dist-packages/flup-1.0.2-py2.6.egg', '/usr/lib/python2.6', '/usr/lib/python2.6/plat-linux2', '/usr/lib/python2.6/lib-tk', '/usr/lib/python2.6/lib-old', '/usr/lib/python2.6/lib-dynload', '/usr/local/lib/python2.6/dist-packages', '/usr/lib/python2.6/dist-packages', '/usr/lib/pymodules/python2.6']
Server time: Sun, 2 Jan 2011 20:42:50 -0600
在我的情况下,以上都不行,所以我必须检查uwsgi + django源代码,并在nginx中传递静态参数,看看为什么/如何,下面是我发现的。
Env信息:
Python版本:2.7.5
Django版本:(1,6,6,'final', 0)
Nginx版本:Nginx /1.6.0
uwsgi: 2.0.7
环境设置信息:
Nginx作为反向代理监听端口80
Uwsgi作为上游Unix套接字,将最终响应请求
Django配置信息:
USE_X_FORWARDED_HOST = True # with or without this line does not matter
nginx配置:
uwsgi_param X-Real-IP $remote_addr;
// uwsgi_param X-Forwarded-For $proxy_add_x_forwarded_for;
// uwsgi_param HTTP_X_FORWARDED_FOR $proxy_add_x_forwarded_for;
// hardcode for testing
uwsgi_param X-Forwarded-For "10.10.10.10";
uwsgi_param HTTP_X_FORWARDED_FOR "20.20.20.20";
获取django应用程序中的所有参数:
X-Forwarded-For : 10.10.10.10
HTTP_X_FORWARDED_FOR : 20.20.20.20
结论:
所以基本上,你必须在nginx中指定完全相同的字段/参数名,并使用request。django应用程序中的META[字段/参数]
现在你可以决定是添加一个中间件(拦截器),还是在某些视图中解析HTTP_X_FORWARDED_FOR。
我想对yanchenko的回答提出一个改进。
而不是在X_FORWARDED_FOR列表中取第一个ip,我取第一个不知道内部ip,因为一些路由器不尊重协议,你可以看到内部ip作为列表的第一个值。
PRIVATE_IPS_PREFIX = ('10.', '172.', '192.', )
def get_client_ip(request):
"""get the client ip from the request
"""
remote_address = request.META.get('REMOTE_ADDR')
# set the default value of the ip to be the REMOTE_ADDR if available
# else None
ip = remote_address
# try to get the first non-proxy ip (not a private ip) from the
# HTTP_X_FORWARDED_FOR
x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
if x_forwarded_for:
proxies = x_forwarded_for.split(',')
# remove the private ips from the beginning
while (len(proxies) > 0 and
proxies[0].startswith(PRIVATE_IPS_PREFIX)):
proxies.pop(0)
# take the first ip which is not a private one (of a proxy)
if len(proxies) > 0:
ip = proxies[0]
return ip
我希望这能帮助那些有同样问题的谷歌同事。
在我的情况下,以上都不行,所以我必须检查uwsgi + django源代码,并在nginx中传递静态参数,看看为什么/如何,下面是我发现的。
Env信息:
Python版本:2.7.5
Django版本:(1,6,6,'final', 0)
Nginx版本:Nginx /1.6.0
uwsgi: 2.0.7
环境设置信息:
Nginx作为反向代理监听端口80
Uwsgi作为上游Unix套接字,将最终响应请求
Django配置信息:
USE_X_FORWARDED_HOST = True # with or without this line does not matter
nginx配置:
uwsgi_param X-Real-IP $remote_addr;
// uwsgi_param X-Forwarded-For $proxy_add_x_forwarded_for;
// uwsgi_param HTTP_X_FORWARDED_FOR $proxy_add_x_forwarded_for;
// hardcode for testing
uwsgi_param X-Forwarded-For "10.10.10.10";
uwsgi_param HTTP_X_FORWARDED_FOR "20.20.20.20";
获取django应用程序中的所有参数:
X-Forwarded-For : 10.10.10.10
HTTP_X_FORWARDED_FOR : 20.20.20.20
结论:
所以基本上,你必须在nginx中指定完全相同的字段/参数名,并使用request。django应用程序中的META[字段/参数]
现在你可以决定是添加一个中间件(拦截器),还是在某些视图中解析HTTP_X_FORWARDED_FOR。