【问题标题】:How do I get user IP address in django?如何在 django 中获取用户 IP 地址?
【发布时间】:2011-06-02 16:18:52
【问题描述】:

如何在 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.com/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

【问题讨论】:

  • 尝试转储 request.META.keys()
  • ['HTTP_COOKIE'、'SCRIPT_NAME'、'REQUEST_METHOD'、'PATH_INFO'、'SERVER_PROTOCOL'、'QUERY_STRING'、'CONTENT_LENGTH'、'HTTP_ACCEPT_CHARSET'、'HTTP_USER_AGENT'、'HTTP_CONNECTION'、' SERVER_NAME','wsgi.url_scheme','SERVER_PORT','wsgi.input','HTTP_HOST','wsgi.multithread','HTTP_CACHE_CONTROL','HTTP_ACCEPT','wsgi.version','wsgi.run_once',' wsgi.errors'、'wsgi.multiprocess'、'HTTP_ACCEPT_LANGUAGE'、'CONTENT_TYPE'、'CSRF_COOKIE'、'HTTP_ACCEPT_ENCODING']
  • 感谢您提出这个好问题。我的 fastcgi 没有通过 REMOTE_ADDR 元键。我在 nginx.conf 中添加了以下行并修复了问题:fastcgi_param REMOTE_ADDR $remote_addr;

标签: python django


【解决方案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

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

注意:以上使用X-Forwarded-For 中的first 项,但您可能希望使用last 项(例如,在 Heroku 的情况下:@987654321 @)

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

get_client_ip(request)

【讨论】:

【解决方案2】:

您可以使用 django-ipware,它支持 Python 2 & 3 并处理 IPv4 & IPv6

安装:

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)
    
  • Trusted Proxies - 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.'))
    

注意:阅读此notice

【讨论】:

  • 看看它的源代码。它处理此处其他答案确定的所有并发症。
  • Thx @Heliodor -- 是的,我已经使模块对于普通用例非常简单,对于复杂用例非常灵活。最起码,您应该先看看它的 github 页面,然后再自己动手。
  • 注意 django-ipware 的设置默认不安全!任何人都可以传递其他变量之一,您的站点将记录该 IP。始终将IPWARE_META_PRECEDENCE_LIST 设置为您使用的变量,或使用pypi.python.org/pypi/WsgiUnproxy 等替代变量
  • @vdboor 你能详细说明一下吗?我在 repo 中找不到 IPWARE_META_PRECEDENCE_LIST。
  • @ThaJay 请注意,从 2.0.0 开始,您应该使用 get_client_ip()get_real_ip 已弃用,将在 3.0 中删除。
【解决方案3】:

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

【讨论】:

  • 是的,ip 位于列表的开头。这是错误的。
  • 实际上,如果用户在代理后面,您将获得用户的内部 IP 地址,即 RFC 1918 地址。在大多数情况下,这根本不是很理想。本方案重点是获取客户端的外部IP地址(代理地址),也就是最右边的地址。
  • 谢谢。通常当我从request.META 请求密钥时,我会包含一个默认值,因为标题经常丢失:request.META.get('REMOTE_ADDR', None)
  • @CarlG 你的代码更透明,但是get方法继承自django.utils.datastructures.MultiValueDict,默认值为None。但是如果你真的希望它不是 None 的话,包含一个默认值绝对是有意义的。
  • 除非您在请求到达您的第一台服务器时清理 X-Forwarded-For,否则该列表中的第一个值是 用户提供的。恶意用户可以轻松欺骗他们想要的任何 IP 地址。您想要的地址是任何服务器之前的第一个 IP,不一定是列表中的第一个。
【解决方案4】:

没有更多的混乱在最近的 Django 版本中,清楚地提到了 客户端的 IP 地址可在

获得
request.META.get("REMOTE_ADDR")

更多信息请查看Django Docs

【讨论】:

  • 当应用程序在反向代理服务器(如 Nginx)后面运行时,这将给出空白值。你需要X_FORWARDED_FOR
【解决方案5】:

我想建议对 yanchenko 的回答进行改进。

我没有采用 X_FORWARDED_FOR 列表中的第一个 ip,而是采用未知内部 ip 中的第一个,因为某些路由器不遵守协议,您可以将内部 ips 视为列表的第一个值.

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

我希望这对遇到同样问题的谷歌同事有所帮助。

【讨论】:

  • 此代码在检查 HTTP_X_FORWARDED_FOR 字段之前不会检查来自 REMOTE_ADDR 的 ip 是否是私有的,因为它可能应该(同样,'127.0.0.1' 或 '127.' 应该一起在 PRIVATE_IPS_PREFIX 中)与 IPv6 等效项。
  • 从技术上讲,这些前缀 (172, 192) 不一定表示私有地址。
  • 分配给专用网络的地址范围为:172.16.0.0–172.31.255.255(16 个“B 类”网络)、192.168.0.0–192.168.255.255(1 个“B 类”网络)和 10.0 .0.0–10.255.255.255(1 个“A 类”或 256 个“B 类”网络)。
  • is_valid_ip 未定义
  • 我喜欢这种方法,但我认为实现有点危险。例如,前缀 10. 下的大多数 IP 地址都是公共 IP。以 T-Mobile 拥有 172.32.0.0 为例。
【解决方案6】:

这里有一个简短的衬线来完成这个:

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

【讨论】:

  • 如果他们都返回 None 那么你会得到一个错误。
【解决方案7】:

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

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

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

【讨论】:

  • nginx(反向代理)和 gunicorn 的可比解决方案是什么? proxy_set_header REMOTE_ADDR $remote_addr; 添加到 nginx.conf 并不能缓解问题。
【解决方案8】:

在我的情况下,以上都不起作用,所以我必须检查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 socket,最终会响应请求

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 中指定完全相同的字段/参数名称,并在 django 应用程序中使用 request.META[field/param]

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

【讨论】:

    【解决方案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/
    

    您在 myhost.com 中的 nginx 将继续发送:

    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】:

      我在上面的答案中也缺少代理。我使用来自django_easy_timezonesget_ip_address_from_request

      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, '最终', 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'")

        【讨论】:

          猜你喜欢
          • 2015-02-26
          • 2019-02-14
          • 1970-01-01
          • 2010-11-06
          • 2014-09-27
          • 1970-01-01
          • 2017-10-15
          • 2014-06-13
          相关资源
          最近更新 更多