如何在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

def get_client_ip(request):
    x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
    if x_forwarded_for:
        ip = x_forwarded_for.split(',')[0]
    else:
        ip = request.META.get('REMOTE_ADDR')
    return ip

确保你有反向代理(如果有)配置正确(例如mod_rpaf安装Apache)。

注意:上面使用X-Forwarded-For中的第一项,但你可能想使用最后一项(例如,在Heroku的情况下:在Heroku上获取客户端的真实IP地址)

然后把请求作为参数传递给它;

get_client_ip(request)

Django文档HttpRequest。元


最简单的解决方案(如果你正在使用fastcgi+nignx)是itgorilla评论的:

谢谢你这个好问题。我的fastcgi没有传递REMOTE_ADDR元键。我在nginx.conf中添加了下面的行,并修复了这个问题:——itgorilla

Ps:我添加这个答案只是为了让他的解决方案更明显。


Alexander的回答很好,但是缺少对代理的处理,代理有时会在HTTP_X_FORWARDED_FOR报头中返回多个IP。

真实IP通常在列表的末尾,如下所示:http://en.wikipedia.org/wiki/X-Forwarded-For

解决方案是对Alexander的代码进行简单的修改:

def get_client_ip(request):
    x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
    if x_forwarded_for:
        ip = x_forwarded_for.split(',')[-1].strip()
    else:
        ip = request.META.get('REMOTE_ADDR')
    return ip

我想对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

我希望这能帮助那些有同样问题的谷歌同事。


你可以使用django-ipware,它支持Python 2和3,并处理IPv4和IPv6。

安装:

PIP安装django-ipware

简单的用法:

# In a view or a middleware where the `request` object is available

from ipware import get_client_ip
ip, is_routable = get_client_ip(request)
if ip is None:
    # Unable to get the client's IP address
else:
    # We got the client's IP address
    if is_routable:
        # The client's IP address is publicly routable on the Internet
    else:
        # The client's IP address is private

# Order of precedence is (Public, Private, Loopback, None)

先进的用法:

Custom Header - Custom request header for ipware to look at: i, r = get_client_ip(request, request_header_order=['X_FORWARDED_FOR']) i, r = get_client_ip(request, request_header_order=['X_FORWARDED_FOR', 'REMOTE_ADDR']) Proxy Count - Django server is behind a fixed number of proxies: i, r = get_client_ip(request, proxy_count=1) Trusted Proxies - Django server is behind one or more known & trusted proxies: i, r = get_client_ip(request, proxy_trusted_ips=('177.2.2.2')) # For multiple proxies, simply add them to the list i, r = get_client_ip(request, proxy_trusted_ips=('177.2.2.2', '177.3.3.3')) # For proxies with fixed sub-domain and dynamic IP addresses, use partial pattern i, r = get_client_ip(request, proxy_trusted_ips=('177.2.', '177.3.'))

注:阅读本通知。


在我的情况下,以上都不行,所以我必须检查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。


最初从Django中删除该功能的原因是头文件最终不能被信任。原因是它很容易被恶搞。例如,配置Nginx反向代理的推荐方法是:

add_header X-Forwarded-For $proxy_add_x_forwarded_for;
add_header X-Real-Ip       $remote_addr;

当你这样做时:

curl -H 'X-Forwarded-For: 8.8.8.8, 192.168.1.2' http://192.168.1.3/

你的Nginx在myhost。示例将发送:

X-Forwarded-For: 8.8.8.8, 192.168.1.2, 192.168.1.3

如果盲目地按照说明操作,X-Real-IP将是前面第一个代理的IP。

如果信任你的用户是一个问题,你可以尝试django-xff: https://pypi.python.org/pypi/django-xff/


我在上面的回答中也缺少了代理。我使用get_ip_address_from_request from django_easy_timezones。

from easy_timezones.utils import get_ip_address_from_request, is_valid_ip, is_local_ip
ip = get_ip_address_from_request(request)
try:
    if is_valid_ip(ip):
        geoip_record = IpRange.objects.by_ip(ip)
except IpRange.DoesNotExist:
    return None

这里是方法get_ip_address_from_request, IPv4和IPv6就绪:

def get_ip_address_from_request(request):
    """ Makes the best attempt to get the client's real IP or return the loopback """
    PRIVATE_IPS_PREFIX = ('10.', '172.', '192.', '127.')
    ip_address = ''
    x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR', '')
    if x_forwarded_for and ',' not in x_forwarded_for:
        if not x_forwarded_for.startswith(PRIVATE_IPS_PREFIX) and is_valid_ip(x_forwarded_for):
            ip_address = x_forwarded_for.strip()
    else:
        ips = [ip.strip() for ip in x_forwarded_for.split(',')]
        for ip in ips:
            if ip.startswith(PRIVATE_IPS_PREFIX):
                continue
            elif not is_valid_ip(ip):
                continue
            else:
                ip_address = ip
                break
    if not ip_address:
        x_real_ip = request.META.get('HTTP_X_REAL_IP', '')
        if x_real_ip:
            if not x_real_ip.startswith(PRIVATE_IPS_PREFIX) and is_valid_ip(x_real_ip):
                ip_address = x_real_ip.strip()
    if not ip_address:
        remote_addr = request.META.get('REMOTE_ADDR', '')
        if remote_addr:
            if not remote_addr.startswith(PRIVATE_IPS_PREFIX) and is_valid_ip(remote_addr):
                ip_address = remote_addr.strip()
    if not ip_address:
        ip_address = '127.0.0.1'
    return ip_address

下面是一个简短的一行代码来完成这个任务:

request.META.get('HTTP_X_FORWARDED_FOR', request.META.get('REMOTE_ADDR', '')).split(',')[0].strip()

django。版本 (2,1,1, 'final', 0) 请求处理程序

sock=request._stream.stream.raw._sock
#<socket.socket fd=1236, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=0, laddr=('192.168.1.111', 8000), raddr=('192.168.1.111', 64725)>
client_ip,port=sock.getpeername()

如果你调用以上代码两次,你可能会得到

AttributeError(“_io。BytesIO的对象没有属性“stream”

AttributeError("'LimitedStream'对象没有'raw'属性")


在Django的最新版本中,清楚地提到 客户端的Ip地址为

request.META.get("REMOTE_ADDR")

更多信息请查看Django文档


简单的添加

{{request.META。REMOTE_ADDR}}

在Django-Template中,你想让用户看到他们的IP地址。这是在您不感兴趣将其保存到DB的情况下。


在获得ip地址后,您需要找到位置

# pip install geocoder

import geocoder

def get_client_ip(request):
    x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
    if x_forwarded_for:
        ip = x_forwarded_for.split(',')[0]
        ip_location = geocoder.ip(f"{ip}")
        ip_location = geocoder.ip("me")
        print(ip_location.city)
        # you can get city such as "New York"
    else:
        ip = request.META.get('REMOTE_ADDR')
    return ip

使用下面的函数获取ip地址:

def get_ip_address(request):
    x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
    if x_forwarded_for:
        ip = x_forwarded_for.split(',')[0]
    else:
        ip = request.META.get('REMOTE_ADDR')
    return ip

在此之后,您可以从web应用程序http://www.iplocinfo.com/:获得用户位置数据和其他信息

import requests
def get_ip_data(request):
    ip_address = get_ip_address(request)
    api_key = "your api key"
    endPoint = f'https://www.iplocinfo.com/api/v1/{ip_address}?apiKey={api_key}'
    data = requests.get(endPoint)
    return data.json()