如何在 Django 中获取用户 IP 地址?

2025-01-06 08:31:00
admin
原创
101
摘要:问题描述:如何在 Django 中获取用户的 IP?我有这样的观点:# Create your views from django.contrib.gis.utils import GeoIP from django.template import RequestContext from django.sh...

问题描述:

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

解决方案 1:

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)

HttpRequest.META 的 Django 文档

解决方案 2:

您可以使用支持 Python 23并处理IPv4IPv6的 django-ipware

安装:

pip install 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)

高级用法:

  • 自定义标头-ipware 要查看的自定义请求标头:

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'])
  • 代理计数-Django 服务器有固定数量的代理:

i, r = get_client_ip(request, proxy_count=1)
  • 受信任的代理-Django 服务器位于一个或多个已知且受信任的代理后面:

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.'))

注意:请阅读此通知。

解决方案 3:

亚历山大的回答很棒,但缺乏对代理的处理,代理有时会在 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

解决方案 4:

不再困惑在 Django 的最新版本中明确提到,客户端的 IP 地址可在

request.META.get("REMOTE_ADDR")

更多信息请查看Django 文档

解决方案 5:

我想建议改进 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

我希望这能帮助遇到同样问题的 Google 同事。

解决方案 6:

这里有一个简短的一行代码来实现这一点:

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

解决方案 7:

就我而言,以上方法均不起作用,因此我必须检查uwsgi+django源代码并在 nginx 中传递静态参数,看看为什么/如何,下面是我发现的。

环境信息:

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.META[field/param]在 django 应用程序中使用。

现在您可以决定是否添加中间件(拦截器)或仅HTTP_X_FORWARDED_FOR在某些视图中进行解析。

解决方案 8:

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

感谢您提出这个很棒的问题。我的 fastcgi 没有传递 REMOTE_ADDR 元键。我在 nginx.conf 中添加了以下行并修复了问题:fastcgi_param REMOTE_ADDR $remote_addr; – itgorilla

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

解决方案 9:

最初从 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/

你的 Nginxmyhost.example将发送:

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/

解决方案 10:

我在上面的回答中也缺少代理。我使用get_ip_address_from_request了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

解决方案 11:

在 django.VERSION (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'”)

解决方案 12:

只需添加

{{ request.META.REMOTE_ADDR }}

在 Django-Template 中,您希望用户看到他们的 IP 地址。如果您不想将其保存到数据库中。

解决方案 13:

使用此函数获取 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

之后,您可以从该网络应用程序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()

解决方案 14:

在 manage.py 所在的文件夹中,我创建了一个名为 getIP 的文件,其中包含以下代码:

import socket


def getip():
    hostname = socket.gethostname()
    ipddr = socket.gethostbyname(hostname)
    print("HOSTNAME:"+hostname)
    print("IP:"+ipddr)
    return f'{ipddr}'

在 manage.py 中我做了以下修改(Python 3.9 和 Django 4.2.7):

#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import os
import sys

from getIP import getip
from django.core.management.commands.runserver import Command as Crs


def main():
    """Run administrative tasks."""
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'MyApp.settings')
    try:
        from django.core.management import execute_from_command_line

        Crs.default_addr = getip()
    except ImportError as exc:
        raise ImportError(
            "Couldn't import Django. Are you sure it's installed and "
            "available on your PYTHONPATH environment variable? Did you "
            "forget to activate a virtual environment?"
        ) from exc
    execute_from_command_line(sys.argv)


if __name__ == '__main__':
    main()

在 Windows 提示符命令中输出为:

(venv) C:MyProjectMyApp>python manage.py runserver 
HOSTNAME:DESKTOP-xxxxxx
IP:192.168.XX.XX
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
December 08, 2023 - 10:26:31
Django version 4.2.7, using settings 'MyApp.settings'
Starting development server at http://192.168.XX.XX:8000/
Quit the server with CTRL-BREAK.

解决方案 15:

获取 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
相关推荐
  政府信创国产化的10大政策解读一、信创国产化的背景与意义信创国产化,即信息技术应用创新国产化,是当前中国信息技术领域的一个重要发展方向。其核心在于通过自主研发和创新,实现信息技术应用的自主可控,减少对外部技术的依赖,并规避潜在的技术制裁和风险。随着全球信息技术竞争的加剧,以及某些国家对中国在科技领域的打压,信创国产化显...
工程项目管理   1565  
  为什么项目管理通常仍然耗时且低效?您是否还在反复更新电子表格、淹没在便利贴中并参加每周更新会议?这确实是耗费时间和精力。借助软件工具的帮助,您可以一目了然地全面了解您的项目。如今,国内外有足够多优秀的项目管理软件可以帮助您掌控每个项目。什么是项目管理软件?项目管理软件是广泛行业用于项目规划、资源分配和调度的软件。它使项...
项目管理软件   1354  
  信创国产芯片作为信息技术创新的核心领域,对于推动国家自主可控生态建设具有至关重要的意义。在全球科技竞争日益激烈的背景下,实现信息技术的自主可控,摆脱对国外技术的依赖,已成为保障国家信息安全和产业可持续发展的关键。国产芯片作为信创产业的基石,其发展水平直接影响着整个信创生态的构建与完善。通过不断提升国产芯片的技术实力、产...
国产信创系统   21  
  信创生态建设旨在实现信息技术领域的自主创新和安全可控,涵盖了从硬件到软件的全产业链。随着数字化转型的加速,信创生态建设的重要性日益凸显,它不仅关乎国家的信息安全,更是推动产业升级和经济高质量发展的关键力量。然而,在推进信创生态建设的过程中,面临着诸多复杂且严峻的挑战,需要深入剖析并寻找切实可行的解决方案。技术创新难题技...
信创操作系统   27  
  信创产业作为国家信息技术创新发展的重要领域,对于保障国家信息安全、推动产业升级具有关键意义。而国产芯片作为信创产业的核心基石,其研发进展备受关注。在信创国产芯片的研发征程中,面临着诸多复杂且艰巨的难点,这些难点犹如一道道关卡,阻碍着国产芯片的快速发展。然而,科研人员和相关企业并未退缩,积极探索并提出了一系列切实可行的解...
国产化替代产品目录   28  
热门文章
项目管理软件有哪些?
云禅道AD
禅道项目管理软件

云端的项目管理软件

尊享禅道项目软件收费版功能

无需维护,随时随地协同办公

内置subversion和git源码管理

每天备份,随时转为私有部署

免费试用